How I Use ApplicationUser in Domain layer?
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;} }
I had same requirement #138.
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 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!
@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.
Thanks a lot! I'll give that a try. As you say perhaps I can use the AuditableEntity instead.
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?