C
C#17mo ago
Avalari

✅ [DDD] Reference IdentityUser in Domain model class

Hi, I'm starting my jurney with DDD so please take it easy on me 😄 Any pointers on how to reference AppUser : IdentityUser (that's in Persistence layer) in a domain model class? I have some entities that have:
public int OwnerId { get; set; }
public virtual AppUser Owner { get; set; }
public int OwnerId { get; set; }
public virtual AppUser Owner { get; set; }
and I want to use .Include(u => u.Owner) when getting the data using EF Core. Problem is that AppUser : IdentityUser is defined in the Persistence layer, which Domain Layer has no access to, nor does it have the EF package. What would be the best approach to deal with this?
5 Replies
Omnissiah
Omnissiah17mo ago
either you move AppUser in domain or you create a surrogate class for AppUser in domain that will be mapped to AppUser or, vertical-slice-speaking, you put a domain layer in the persistence layer so that you can create a module that will be used by module that has that class with OwnerId and Owner
Avalari
AvalariOP17mo ago
I tried making a User surrogate class but on DB creation a separate "User" table is created instead of using the default IdentityUser table. I also tried using a IUser interface in the Domain:
public virtual IUser User { get; set; }
public virtual IUser User { get; set; }
and then in the Persistance:
ApplicationUser : IdentityUser<string>, IUser
ApplicationUser : IdentityUser<string>, IUser
but EF does not let this go because:
The specified type 'AppName.Domain.Contracts.IUser' must be a non-interface reference type to be used as an entity type.
The specified type 'AppName.Domain.Contracts.IUser' must be a non-interface reference type to be used as an entity type.
am I missing something here?
Omnissiah
Omnissiah17mo ago
types in ef can't be interfaces, they have to be concrete also you should look at your configuration, for example if you are using table per hierarchy or what have you
Avalari
AvalariOP17mo ago
I was able to make it work by casting the interface:
entity.HasOne(typeof(ApplicationUser), "User")
.WithMany()
.HasForeignKey("UserId")
.IsRequired();
entity.HasOne(typeof(ApplicationUser), "User")
.WithMany()
.HasForeignKey("UserId")
.IsRequired();
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server