jasontaylordev / NorthwindTraders

Northwind Traders is a sample application built using ASP.NET Core and Entity Framework Core.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Accessing ApplicationUser's additional properties in Application layer

morgrowe opened this issue · comments

I don't know if this is best practice, but I've added a custom property to Application User:

namespace Northwind.Infrastructure.Identity
{
    public class ApplicationUser : IdentityUser
    {
        public string SomeProp { get; set; }
    }
}

I'd like to get the value of SomeProp. Using Mediator to do this seemed like a good idea, so I started creating a query handler::

namespace Northwind.Application.AccountManagement.Queries
{
    public class GetAppUserQueryHandler : IRequestHandler<GetAppUserQuery, AppUserViewModel>
    {
        private readonly IApplicationDbContext _context;

        public GetAppUserQueryHandler(IApplicationDbContext context)
        {
            _context = context;
        }

        public async Task<AppUserViewModel> Handle(GetAppUserQuery request, CancellationToken cancellationToken)
        {
           // do a query
        }
    }
}

However, I noticed there wasn't an IApplicationDbContext. I took another look through this repo and couldn't see one. I also can't see any examples of Mediator queries that queried the ApplicationDbContext.

Should I simply create an IApplicationDbContext (in Northwind.Application.Common.Interfaces) interface and use it the same way I would INorthwindDbContext? Or am I looking this the wrong way? Maybe I should be storing user data in my NorthwindDbContext and linking it to their Identity account?

Thanks for all the hard work, absolutely love this project structure.

Cheers
Morgan

Are you looking for UserManagerService.cs?

Could also look at registering it as a claim if it is not a sensitive property. You would have to dig into the IdentityServer documentation for that.

Hi @Patrick-Ullrich

Thanks for taking the time to suggest this. In the end, I decided to create a separate entity for storing my custom user properties (I called it Employees, just like this repo). Fortunately, each user has a unique email address, so I can store this email address in my Employees table and use it to map the current user to the correct row in the Employees table to get their info.

Cheers
Morgan