我正在开发一个基于 ASP.NET Core 6.0 WebAPI 的干净架构。https://github.com/jasontaylordev/CleanArchitecture
这个架构中有 4 个项目:WebApi、Infrastructure、Application 和 Domain。
- Domain 是核心。
- Application 层依赖于 Domain 层。
- Infrastructure 层依赖于 Application 层。
- WebApi 层依赖于 Application 层和 Infrastructure 层。
而查询和命令(CQRS)应该写在 Application 层内。
我想使用 Asp.Net Core Identity。
ApplicationUser.cs(位于 Infrastructure/Identity 目录下)
namespace Infrastructure.Identity;
public class ApplicationUser : IdentityUser
{
// removed
}
IIdentityService.cs(位于应用程序/公共/接口中)
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(位于 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
}
所有工作都很好。
我的问题是,我必须在应用程序层中使用UserManager<ApplicationUser> userManager
来编写查询。
我收到错误消息:不必要的using指令。[Application] 命名空间“Infrastructure”中不存在类型或命名空间。您是否缺少程序集引用?)[Application]
Query.cs(位于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
}
}
}
如何在应用层调用userManager和ApplicationUser(:IdentyUser)而不出错?
请帮帮我。
注意:我不想自己编写类似useManager包的函数。我想使用userManager。
例如:像这样
private readonly UserManager<ApplicationUser> _userManager;
_userManager.GetUserAsync(_httpContextAccessor.HttpContext.User);
有人有解决这个问题的想法吗?