5

Given the following project structure

 App.Domain - .NET Standard Library

     User.cs


 App.Web - .NET Core Web Application MVC

The App.Web project references the App.Domain project. Is it possible to use ASP.NET Identity having as the user App.Domain.User without it inheriting from IdentityUser?

Why?

Following the DDD guidelines, I should keep the domain layer as technology-independent, that's why I wouldn't like to add a reference to the ASP.NET Identity in the domain layer and inherit my User class from IdentityUser.

Vinicius Gonçalves
  • 2,514
  • 1
  • 29
  • 54
  • 5
    It is possible but then you would have to implement all the data store interfaces yourself. IdentityUser is a base class in the EntityFramework implementation of the data store interfaces needed for Identity. – Joe Audette Apr 10 '18 at 12:22
  • 1
    You can look at https://github.com/aspnet/Identity/tree/rel/2.0.0/src/Microsoft.AspNetCore.Identity.EntityFrameworkCore to see how the EF Core store is implemented. You'd just need to implement your own DbContext and types and refer these in your store implementations, then register it like https://github.com/aspnet/Identity/blob/rel/2.0.0/src/Microsoft.AspNetCore.Identity.EntityFrameworkCore/IdentityEntityFrameworkBuilderExtensions.cs. – Tseng Apr 10 '18 at 12:29
  • 3
    However, I would **strongly** advise **against** using domain models for identity. In 95% of all cases Identity is not part of a model and when you start treating is that you may get into cross-domain concerns (bounded context A depending on bounded context B). Identity in 95% of the cases is infrastructure which helps with determining the identity of the user and hence strongly depends on the framework and platform used. i.e a change of the identity framework could affect all your domains depending on it, something you want avoid (that infrastructure changes force change of your domain) – Tseng Apr 10 '18 at 12:36
  • On a side note: You may want to go through the **comments** of [this question](https://stackoverflow.com/questions/46034870/good-practice-to-handle-a-use-case-requiring-data-from-another-bounded-context#comment79098869_46034870) to understand my reasoning for "Identity not being part of domain". – Tseng Apr 10 '18 at 13:04
  • Tks @Tseng, specially for the DDD tips. – Vinicius Gonçalves Apr 10 '18 at 16:54
  • The DDD points are valid, but don't be scared away by having to write persistence yourself, it's very simple stuff. I'm not a fan of Entity Framework, so I've done this before; adding the initial bare-bones user-data and Identity Server 4 persistence wound up requiring only about 13 SQL statements. – McGuireV10 Apr 10 '18 at 18:25
  • 2
    Personally, I don't see the issue. Your "user" entity is your "user" entity, regardless of the framework. Identity is very lightweight in it's implementation, merely requiring simple inheritance. If you need to switch it out later, you can simply change your "user" entity to not inherit from that or inherit from something else. Any other entities that reference your user class, don't need to change. – Chris Pratt Apr 10 '18 at 18:25

0 Answers0