Separation of ASP.NET Core Identity ApplicationUser with relationship to Domain Model Entity
Asked Answered
P

2

10

I'm trying to achieve good separation and architecture between ASP.NET Core Identity and entities in my domain model.

I'm using an Onion architecture in a web application built with ASP.NET Core. In the core project, Domain, I have domain models, and one of them being a Member entity.

Then in another project, Infrastructure, I have the built in ASP.NET Core Identity setup which contains an ApplicationUser class that inherits from IdentityUser.

    public class ApplicationUser : IdentityUser
    {
    }

The idea of having the identity user and member entity separated is good. However, I want to relate them but using Entity Framework Core this seems to be a challenge. I cannot get around this without adding navigation properties in my domain models, which I'd like to avoid.

I was hoping I could do it entirely using Fluent API without having to add properties but that does not seem to be the case.

Any recommendations on how to approach this? Considering the above how could I create a relationship between the identity user and a domain entity without adding dependencies from the core project?

My question is somewhat a duplicate or related to some of these posts here e.g.

and some other posts as well. However, the answers to these questions seems to be workarounds. Maybe that is the only way?

I'm using ASP.NET Core 3.1 and Entity Framework 3.1. for this project.

Psychobiology answered 29/12, 2019 at 11:40 Comment(3)
I think what you call "domain layer" actually is the EF persistence layer. Anyway, I'm a great fan of separating both models entirely and only creating a 1:1 association in the database without changing the Identity table structure. When an IdentityUser logs in by Identity you can use its ID to work with the Member entity in the actual application. This will keep authentication/authorization and the "real" business nicely decoupled.Tarim
Hi @GertArnold, thanks for the feedback it's appreciated. I somehow need to relate the two and I'm exploring the options with EF Core but I haven't found a way to do it without introducing it into the domain layer. I would somehow need to have a foreign key in the Identity table so I could relate it to a member. The Infrastructure project contains the EF persistence logic and Identity as external concerns whereas the Domain contains the entities and related domain logic.Psychobiology
@Psychobiology Have you eventually solved this issue? I am experiencing it right now and went through all the questions you linked to, without much success.. Basically I don't know what to do with AppUser in my Core project which has to inherit from IdentityUser but Core project should not have Identity dependencyBrady
S
1

I Had same business need, I need to separate Identity from other domain models, I decided to follow the following pattern:

1- when create/register new user, we will add identity data in identity table

2- we will add other business data in our domain models lets say Teachers/Students Tables

3- now we need to build the relation between both we can do that by add claims to user in 'UsersClaims' identity table, which by default have the following properties (UserId, ClaimType, ClaimValue) so we will add the our domain model id with identity id

by doing that we have build a virtual relation between Identity-domain

this is a way, you can do this in other side in domains model build mapping table hold same data (identityUserId & domainsUserId)

you can then separate them in different databases if you want

Soloman answered 30/11, 2020 at 19:35 Comment(0)
F
0

How about domain models as interfaces?

So u have some IMember in Domain project (and other types...) and in DAL layer you got DataAccessDbContext with set of Member : IMember (and all other types, ie. Blog : IBlog etc.).

And create separate project for Identity, with IdentityDbContext where MemberIdentiy : IdentityUser, IMember.

Moreover,

as your Domain has IMemberRepository, ISomeEntityRepository etc, and ISomeEntity has IMember prop, you can separately implement SomeEntityRepository in DAL and MemberRepository in Identity project.

You can decide if both context use same database or not.

Frown answered 19/1, 2020 at 18:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.