Table definition vs Dto AutoMapping choice

I try to sync data, from my Xamarin Apps and my database…

I read my record dietResult from the Db then try

        dietResult.PopulateWith(request.DietDto);
        dietResult.PopulateInstance(request.DietDto);
        dietResult = request.Audit.ConvertTo<Table.DietResult>();

        dietResult is a reference in audit table.

        Db.Save(audit, true);

I don’t find the correct method that make sens…
When Save Insert no issues, but when Save do an update I loose my DietId.

So my question does PopulateWith or PopulateInstance or ConvertTo will assign to zero my DietId on Update ?

My table definition:

[Alias("REIBeef_DietResult")]
public class DietResult : IBackOfficeField, IDietResult
{
	private Guid? rowId;

	[Unique, Sequence("DietId"), ReturnOnInsert]
	public long DietId { get; set; }
	public int AccessFiber { get; set; }
	public int MainFiber { get; set; }
	public int FiberQuality { get; set; }
	public int FiberRegularity { get; set; }
	public bool ValidCategory { get; set; }

	[Default(OrmLiteVariables.SystemUtc)]
	public DateTime? CreatedDateUtc { get; set; }

	[Default(OrmLiteVariables.SystemUtc)]
	public DateTime? UpdatedDateUtc { get; set; }

	[PrimaryKey]
	public Guid? RowId
	{
		get => rowId = rowId ?? Guid.NewGuid();
		set => rowId = value;
	}
}

My Dto:

public class DietDto : IBackOfficeField, IDietResult
{
	public int AccessFiber { get; set; }
	public int MainFiber { get; set; }
	public int FiberQuality { get; set; }
	public int FiberRegularity { get; set; }
	public bool ValidCategory { get; set; }
	public DateTime? CreatedDateUtc { get; set; }
	public DateTime? UpdatedDateUtc { get; set; }
	public Guid? RowId { get; set; }
}

They’re all effectively using the same API underneath to do the mapping. PopulateInstance is effectively an alias for PopulateWithNonDefaultValues whilst ConvertTo<T> creates a new instance instead of populating an existing one.

As with any custom mapping the approach, use the built-in Mapping APIs to copy as much as can be inferred by convention then use a custom extension method to manually copy anything the built-in Mapping doesn’t copy across.

I don’t understand the DietId issue here as DietDto doesn’t have a DietId property to copy to.

DietDto is coming from from my SQLite local Db, that’s why DietId is not in the Dto.
So in my service I can access the record with RowId and then assign my Dto info to my record on server.

RowId is generated from the Xamarin App. With that GUID I can find my record in both DB, local on device and on the server.
The DietId will be use for a human to identify the record. On the server database (Back office application)

AutoMapping API’s don’t initialize non-matching properties and ConvertTo<T> creates a new instance so it obviously wont create an instance with the DietId property since it doesn’t exist on the source DTO.

Check out the different AutoMapping Extension methods for mapping with different behavior. E.g. you can update your data model with DTO properties containing non default values with:

var audit = db.SingleById<DietResult>(request.RowId);
audit.PopulateWithNonDefaultValues(request);
Db.Save(audit, references:true);

Primary Keys should never be null so your RowId property should be a non-nullable Guid. Also checkout the new [AutoId] attribute if you want OrmLite to populate it for you.

There’s some additional background info on the different approaches for maintaining separate DTOs and Data Models vs reusing the same DTO for your DataModel and use attributes to ignore the properties you don’t want populated in your DTOs or DataModel.

1 Like