Search code examples
c#asp.net-coreasp.net-identitydomain-driven-design

Use Asp.Net core identity and DDD(CQRS) together


I'm refactoring my project in CQRS and DDD, and I wanted to use Asp.Net core Identity.

So in aggregate root implementations we'll gonna have Entities inheriting from a class called Entity and the aggregates are gonna inherit from an interface called IAggregate in addition of Entity class, which defines the aggregate model in the aggregate root (As explained in Microsoft docs).

Here is the question:

In order to tell the identity what are your Identity classes your classes should inherit the identity classes provided by the Identity (such as IdentityUser<TKey>). In case of having an aggregate root called User, this class is gonna have two inheritances :

   public class User : Entity, IAggregate
   {
       //Entity and model Codes
   }

Now for implementing the identity user, the User class should inherit from IdentityUser<int> but it is not possible to inherit multiple classes. I was thinking of a way which I could make a generic abstract class that implemented the abstract class Entity base class for the DDD and the generic type T that can be the Identity classes required by different entities, but it is not working as I expected and its not possible to do so in that way. So any one knows a way which could make it possible to inherit the identity class and DDD aggregation classes?


Solution

  • So after a long time I just returned to this article and tried to check if I can handle it, then just realized that the whole title was written wrong.

    The title should've been: How to use Asp.Net Core Identity and DDD(Aggregate roots) together.

    prev Title: Using Identity 4 and Aggregate root (DDD) at the same time

    And if someone like me was looking up for a solution, here is what I came up as a solution:

    Instead of using class 'Entity' (the class defined in DDD and CQRS design for entities) for my identity classes, I created 'IdentityEntity' classes(these classes where implemented per identity requirements like User, UserRole, UserLogin and etc..., which their implementations were same as 'Entity' class), with a minor difference, they inherit from Identity classes(IdentityUser, IdentityRole, etc...), so I could follow DDD rules and Implement Asp.Net Core Identity at the same time.

    If we say we have some models named User, Lesson, School, Role models implementations would be something like that:

    public class User : IdentityUserEntity, IAggregate
    {
       //Model stuff here
    }
    
    public class IdentityUserEntity : IdentityUser<int>
    {
       public int Id { get; set; }
       //Entity Codes
    }
    
    public class Role : IdentityRoleEntity //Model placed under User Aggregate
    {
       //Model stuff here,
    }
    
    public class IdentityRoleEntity : IdentityRole<int>
    {
       public int Id { get; set; }
       //Entity Codes
    }
    
    public class School : Entity, IAggregate
    {
       //Model stuff
    }
    
    public class Lesson : Entity //Model under School aggregate
    {
       //Model stuff
    }
    
    public class Entity
    {
       //Entity Codes
    }
    

    I still am looking forward better ways to handle domain models in way better clean style.