I am working a asp.net core 6.0 WebAPI in a clean Architecture.https://github.com/jasontaylordev/CleanArchitecture
There are 4 projects in this architecture. WebApi, Infrastructure, Application and Domain.
- Domain is the Core.
- Application Layer is dependent on Domain Layer.
- Infrastructure Layer is dependent on Application Layer.
- WebApi Layer is dependent on Application Layer and Infrastructure Layer.
And The Queries and command (CQRS) should be written inside Application Layer
I want to use Asp.Net Core Identity.
ApplicationUser.cs ( in Infrastructure/Identity )
namespace Infrastructure.Identity;
public class ApplicationUser : IdentityUser
{
// removed
}
IIdentityService.cs (in Application/Common/Interfaces)
namespace Application.Common.Interfaces;
public interface IIdentityService
{
Task<string> GetUserNameAsync(string userId);
Task<bool> IsInRoleAsync(string userId, string role);
Task<bool> AuthorizeAsync(string userId, string policyName);
Task<(Result Result, string UserId)> CreateUserAsync(string userName, string password);
Task<Result> DeleteUserAsync(string userId);
// and so on
}
IdentityService.cs ( in Infrastructure/Identity )
namespace Infrastructure.Identity;
public class IdentityService : IIdentityService
{
private readonly UserManager<ApplicationUser> _userManager;
public IdentityService(
UserManager<ApplicationUser> userManager )
{
_userManager = userManager;
}
//implementations of Interface by using userManager
}
above all work fine.
My Issue is, I have to UserManager<ApplicationUser> userManager
to write a query (in APllication Layer )
I got error, Unnecessary using directive. [Application] The type or namespace name 'Infrastructure' does not exist in the namespace are you missing an assembly reference?) [Application]
Query.cs ( in Application/SiteCodes/Queries/GetAll )
using Infrastructure.Identity; // error
namespace Application.SiteCodes.Queries.GetAll
{
public class GetAllQueryHandler : IRequest<List<SiteCode>>
{
}
public class GetAllQueryHandlerHandler : IRequestHandler<GetAllQueryHandler,List<SiteCode>>
{
private readonly UserManager<ApplicationUser> _userManager; // error
private readonly IHttpContextAccessor _httpContextAccessor;
// constructor removed
public async Task<List<SiteCode>> Handle(GetAllQueryHandler request,
CancellationToken cancellationToken)
{
var user = await
_userManager.GetUserAsync(_httpContextAccessor.HttpContext.User); // By using usermanger. I have to call more function like this from useManager
// removed rest
}
}
}
How can I do this without any error?. How can I call userManager and ApplicationUser ( : IdentyUser) inside Application Layer
Please help me.
Note : I don't want to write function by myself like useManager package. I want to use userManager.
Eg: Like this
private readonly UserManager<ApplicationUser> _userManager;
_userManager.GetUserAsync(_httpContextAccessor.HttpContext.User);
Anyone have idea to solve this issue?