C
C#ā€¢12mo 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
boiled goose
boiled gooseā€¢12mo 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
Avalariā€¢12mo 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?
boiled goose
boiled gooseā€¢12mo 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
Avalariā€¢12mo 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
Accordā€¢12mo 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.