xamarin.formsmvvmcastingmodeldata-access

Implicit casting error occurs between data access library and xamarin models classes


I am developing a Xamarin.Forms app for Android that stores data at google-firebase.
I have a dotNet standard library for data access and a XamarinForms Shared library.

I have implemented models in both libraries, the data access models are responsible for uploading and downloading data from google-firebase, and the shared library models are for data binding. While I am fetching data by data access models, I can't put them in shared library models to bind to UI. It shows error Cannot implicitly convert type 'DataAccess.Models.Child' to 'VaxinApp.Models.Child' - CS0029

Note
I know why the error occurs but don't know how to handle it. I used explicit casting, but it doesn't help.
Update
I might be violating design patterns or principles like SOLID, DRY while implementing the access layer point me them as well.


Solution

  • Due to Wendy's comment, we can't cast reference types, but we can do using a method in the model or doing its properties one by one, Like in the code snippet below.

    var area = await firebaseHelper.GetArea();
                Area = new AreaModel
                {
                    ClusterName = area.ClusterName,
                    CHWName = area.CHWName,
                    SocialMobilizerId = area.SocialMobilizerId,
                    TeamNo = area.TeamNo
                };  
    

    See, I have initialized the class by adding properties of the other Class one by one.