Search code examples
entity-frameworkxamarinarchitectureasp.net-coreasp.net-identity

Project Architecture with Xamarin and Asp.Net Core Identity


I have a project with a web api, a web app and Xamarin Apps. They all should use the same data model stored in a AspNetCore Library. I want to use EF Core for the database and I have a Db Managing project (also a AspNetCore Library) referencing EF Core containg the context and setup. The web api is the only project referencing this Db Managing project, because it handles all the database access. All other apps are contacting the web api for data interaction.

My problem: I want to use AspNetCore.Identity for my user management, but the Xamarin Apps cannot reference the data model, when its using AspNetCore.Identity. How can I solve this?


Solution

  • Your Xamarin code should be totally decoupled from server business logic and data access. Why would you reference AspNet Identity from Xamarin? Those are two completely different layers.

    Your server Web API has to expose REST methods to handle authentication/authorization and other stuff so a client can consume them (web client, mobile client, desktop client, whatever).

    Xamarin should consume those remote methods with a REST client, in the same fashion a javascript client would.

    I understand from your question that you want to reuse business objects / models in your xamarin project just because they are written in C#. But if those objects have dependencies on asp.net identity, you can´t. In that case you should map your business objects (BO) to data transfer objects (DTO) that web API will use to communicate with clients. DTO´s are completely agnostic from any data access layer and should be simple POCO´s. They are meant to be serialized/deserialized to/from json/xml.

    On the other hand, you don´t want to serialize user models "as is" through a web api because that implies sending sensitive data over the internet like user hashed passwords, etc. Your DTO´s should just have the fields needed in your client, and the mapping will do the rest.