【发布时间】:2021-04-08 02:58:10
【问题描述】:
我想将此服务转移到逻辑上以便在任何地方使用,但我无法成功,因为它来自控制器。
我有两个服务。有读取缓存,我在验证时在控制器层使用它们。
我的第一个逻辑是读取缓存中的 companyId
public virtual int GetCompanyIdFromCache(int id)
{
_memCache.TryGetValue(id, out int companyId);
return companyId;
}
我的第二个服务也在控制器上。 (帮助我找到用户的 id)
[HttpGet]
[Route("GetCompanyId")]
public int GetCompanyPublicId()
{
if (User.Identity is ClaimsIdentity claimsIdentity)
{
var userId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value;
var companyId = _userService.GetCompanyIdFromCache(Convert.ToInt32(userId));
return companyId;
}
throw new ArgumentException("Can't be found Company");
}
我想在任何地方都使用这种方法,所以我想将第二个服务完全移动到逻辑层,但用户字段来自 ControllerBase(我猜是在 HttpContext 上),我无法将它移动到逻辑层
if (User.Identity is ClaimsIdentity claimsIdentity)
我应该如何重构逻辑层?
【问题讨论】:
标签: c# asp.net .net asp.net-core asp.net-web-api