dotnet-starter-kit icon indicating copy to clipboard operation
dotnet-starter-kit copied to clipboard

How I Use ApplicationUser in Domain layer?

Open urmatgit opened this issue 3 years ago • 5 comments

Hello, thank for this project. Very usefull and helping for me learn programmer skills. I have a question, How I use ApplicationUser in Domain layer?

public class Customer { ... public ApplicationUserId {get;set;} public virtual ApplicationUser ApplicationUser {get;set;} }

urmatgit avatar Apr 18 '22 12:04 urmatgit

I had same requirement #138.

pashaie avatar Apr 19 '22 08:04 pashaie

ApplicationUser should not be used in the Domain layer as it's an Application specific class. The domain shouldn't care about users or authentication.

Instead, you pass the user ID to the domain as part of entities to denote record ownership for example.

hades200082 avatar Jun 15 '22 19:06 hades200082

@hades200082 Thanks for your response to this, but I'm still unsure on how to do this - how do I enforce a foreign key to the user table from a model in the Domain layer? Any code snippets would be really helpful, thanks!

3ur3ka avatar Aug 26 '22 16:08 3ur3ka

@hades200082 Thanks for your response to this, but I'm still unsure on how to do this - how do I enforce a foreign key to the user table from a model in the Domain layer? Any code snippets would be really helpful, thanks!

@3ur3ka, In your Application Layer, you can take a dependency to the ICurrentUser interface. Then when you interact with your Domain Entities in your Application layer, you can set values as appropriate if that meets your requirements.

var userId = _currentUser.GetUserId(): and then assign that to end Domain entities that might need this as a ForeignKey value.

Note, the Infrastructure layer in the FSH Boilerplate will set the values for the Auditable columns on your Domain Entities if they Inherit AuditableEntity for you automatically (e.g. CreatedBy, LastModifiedBy, DeletedBy).

Send your use case here about what you're using the UserId.

chrislangston avatar Aug 27 '22 11:08 chrislangston

Thanks a lot! I'll give that a try. As you say perhaps I can use the AuditableEntity instead.

3ur3ka avatar Aug 29 '22 09:08 3ur3ka

What if I want to return from the API a paginated list of products along with first and last name of the user who owns them? I have to make a repository call to get the products and then call N times the user service to get data of the related users. This because there is a missing foreign key between the Users table and the Products table (in which I have the "Owner" property). I can add a domain entity called "User" and relate it to the Product table, but now how can I connect the ApplicationUser entity with my User domain model?

michelebenolli avatar May 02 '23 07:05 michelebenolli