Search code examples
c#xamarinsqlite-net-extensions

Storing automapped object with class properties with sqlite-pcl and a generic repository


The project I am working on is a Xamarin mobile application with a REST api and offline first practices. To achieve this I've setup a generic repository which is supposed to handle the communication to the SQLite database on the device.

Offline first is used only as a fallback mechanism to read data and does not need to handle 'complex scenarios'

We're using automapper to convert http json objects to domain to database tables, the problem we are having problems with storing class properties of the database dto objects.

When mapping the unload and load address are correctly converted but sqlite does not seem able to parse the Address_DTO object to a string.

See the addresses and address blobs of the order dto.

    /// DTO Wrapper for storing order information do the database
    /// </summary>
    /// <seealso cref="CarrierConnectMobile.Core.Repositories.EntityBase" />
    [Table("Orders")]
    public class DTO_DBOrder : EntityBase
    {
        public string? LicensePlate { get; set; }
        public string Quantity { get; set; }
        public string ProductKind { get; set; }
        public string ProductType { get; set; }
        public string Product { get; set; }
        public OrderStatus Status { get; set; }
        public bool ProvidedUnloadData { get; set; }
        public bool ProvidedLoadData { get; set; }
        [TextBlob("UnLoadAddressBlob")]
        public DTO_DBAddress UnloadAddress { get; set; }
        [TextBlob("LoadAddressBlob")]
        public DTO_DBAddress LoadAddress { get; set; }

        public string UnLoadAddressBlob { get; set; }
        public string LoadAddressBlob { get; set; }
    }

The automapper profiles for the database's DTO object's are having the same naming convention for the UnloadAddress and LoadAddress and they are correctly mapped with the following minimal setup.

public DBProfiles()
        {
            CreateMap<DTO_DBAddress, DTO_HttpAddress>().ReverseMap();
            CreateMap<DTO_DBAddress, Address>().ReverseMap();

            CreateMap<DTO_DBOrder, Order>()
                .ReverseMap();
            CreateMap<DTO_DBOrder, DTO_httpOrderListItem>()
                .ForMember(d => d.Status, opt => opt.ConvertUsing(new OrderStatusStringToEnumConverter(), src => src.Status))
                .ReverseMap();
        }

Is anything wrongly configured? As I cannot seem to find out why sqlite is not storing the address object as a string blob and parsing it correctly


Solution

  • Better late than never. Placing this for someone who needs it in the future.

    The problem was caused using the regular sqlLite library methods and not the sqlite extensions library from sqllite-pcl.

    the using STLiteNetExtensionsAsync.Extensions did not get suggested with the autocomplete i relied on. I did not know that without using the extensions, the serialization of the objects will not work.