Forum Xamarin Xamarin.Forms

Objects with Foreign key best practises Xamarin Forms and ASP.NET Azure backend

I have a Xamarin Forms MVVM application with an ASP.NET azure backend using offline Sync.

In the ASP.NET backend I have the following DataObjects

public class Claim: EntityData
{

    public string CarReg { get; set; }
    public int StartMileage { get; set; }
    public int? EndMileage { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime? EndDate { get; set; }
    public DateTime? SubmittedDate { get; set; }

    public string UserId { get; set; }
    public virtual ICollection<Petrol> Petrols { get; set; }

}

public class Petrol: EntityData
{

    public int Mileage { get; set; }
    public DateTime PurchaseDate { get; set; }
    public float Quantity { get; set; }
    public Decimal Cost { get; set; }
    public string Station { get; set; }
    public string Claim_Id { get; set; }
    [ForeignKey("Claim_Id")]
    public virtual Claim Claim { get; set; }

}

To post a record to the Petrol table I have been using the following JSON post call. All works well.
{
"station": "testpetrol", "cost": 50, "quantity": 55,"date": "2017-07-26T00:00:00Z",
"mileage": 100, "claim_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}

In my Xamarin Forms project I am have the models as below you see that the Claim_id is just a string rather than a Claim object as in the backend.
public class Claim: EntityData
{

    public string CarReg { get; set; }
    public int StartMileage { get; set; }
    public int? EndMileage { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime? EndDate { get; set; }
    public DateTime? SubmittedDate { get; set; }
    public string UserId { get; set; }
}
public class Petrol: EntityData
{
    public int Mileage { get; set; }
    public DateTime PurchaseDate { get; set; }
    public float Quantity { get; set; }
    public Decimal Cost { get; set; }
    public string Station { get; set; }

    [JsonProperty(PropertyName = "Claim_Id")]
    public string ClaimId { get; set; }
}

My Question is, is it best practice to keep my model in the PCL and the DataObjects in the ASP.NET backend project the same (regarding the foreign key set up)? Should I be passing the Claim object when adding a Petrol instance or is it fine to just pass use the string for the foreign key?
Thanks

Sign In or Register to comment.