How to get CreatedBy name at querying? #412
-
Let's say I want to display the name of the user who created a record (Ex: Products, Brands, Customers, etc..), How do I achieve this? Since Identity-related entities are in Infrastructure and my GetAllProductsQuery feature is in the Application layer. How do I add the created user's name to the response object of the GetAllProductsQuery feature? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Did you look at the template provided here? There is a ICurrentUserService.cs. This is used in the SaveChangesAsync method in infrastructure, so that an entity that extends from AuditableEntity get's the current user assigned. If you query an entity that is an AuditableEnity you also have access to the CreatedBy and/or LastModifiedBy prop and can use this to display the user that created/modified the entity. Update: If you need the current user in any other command/query you can simply ask for an ICurrentUserService and for an IIdentityService using dependency injection and can access the user info with this. |
Beta Was this translation helpful? Give feedback.
Did you look at the template provided here? There is a ICurrentUserService.cs. This is used in the SaveChangesAsync method in infrastructure, so that an entity that extends from AuditableEntity get's the current user assigned.
If you query an entity that is an AuditableEnity you also have access to the CreatedBy and/or LastModifiedBy prop and can use this to display the user that created/modified the entity.
Update: If you need the current user in any other command/query you can simply ask for an ICurrentUserService and for an IIdentityService using dependency injection and can access the user info with this.