Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# IMemoryCache控制器外部依赖项注入_C#_Asp.net Core_Asp.net Core Mvc - Fatal编程技术网

C# IMemoryCache控制器外部依赖项注入

C# IMemoryCache控制器外部依赖项注入,c#,asp.net-core,asp.net-core-mvc,C#,Asp.net Core,Asp.net Core Mvc,我有一个带API的ASP.NET核心MVC项目 然后在同一个解决方案中有一个类库,名为Infrastructure。 我的API在类库基础结构中调用一个存储库方法,在ClassUserRepository 如果我在API控制器中使用: private static IMemoryCache _memoryCache; public Api(IMemoryCache cache) //Constructor { _memoryCache = cache; } 我可以使用缓存进入控制器。

我有一个带API的ASP.NET核心MVC项目

然后在同一个解决方案中有一个类库,名为Infrastructure。

我的API在类库基础结构中调用一个存储库方法,在Class
UserRepository

如果我在API控制器中使用:

private static IMemoryCache _memoryCache;
public Api(IMemoryCache cache) //Constructor
{
    _memoryCache = cache;
}
我可以使用缓存进入控制器。 但是我希望ASP.NET注入相同的引用,以便在基础结构库中的
UserRepository
类中使用

这样我就可以从API调用

UserRepository.GetUser(Id);
在UserRepository类中:

namespace Infrastructure
{
    public class UserRepository
    {
        public static User GetUser(Id)
        {
            **//I want to use the Cache Here**
        }
    }
}

我如何告诉ASP.NET将
IMemoryCache
注入到
UserRepository
类中,即使它不是控制器?

依赖项注入和
静态
不能很好地结合在一起。选择其中一个,否则你将继续面临这样的困难。我建议您将
UserRepository
添加到依赖项注入容器中,将
IMemoryCache
添加到构造函数中,并将存储库注入控制器中


关键是在应用程序的所有层中实现依赖项注入,而不仅仅是在Web API层中。

避免所有(静态单例、活动记录模式和静态类)的具体解决方案:

public class ApiController : Controller
{
    private readonly UserRepository_userRepository;
    public ApiController(UserRepository userRepository)
    {
        _userRepository = userRepository;
    }

    public Task<IActionResult> Get() 
    {
       // Just access your repository here and get the user
       var user = _userRepository.GetUser(1);

       return Ok(user);
   }
}

namespace Infrastructure
{
    public class UserRepository
    {
        public readonly IMemoryCache _memoryCache;

        public UserRepository(IMemoryCache cache)
        {
            _memoryCache = cache;
        }

        public User GetUser(Id)
        {
            // use _memoryCache here
        }
     }
}

// Startup.cs#ConfigureServices
services.AddMemoryCache();
公共类ApiController:控制器
{
私有只读用户存储库\用户存储库;
公共ApiController(用户存储库用户存储库)
{
_userRepository=userRepository;
}
公共任务Get()
{
//只需在此处访问您的存储库并获取用户
var user=\u userRepository.GetUser(1);
返回Ok(用户);
}
}
命名空间基础结构
{
公共类用户存储库
{
公共只读IMemoryCache\u memoryCache;
公共用户存储库(IMemoryCache缓存)
{
_memoryCache=cache;
}
公共用户GetUser(Id)
{
//在这里使用内存缓存
}
}
}
//Startup.cs#配置服务
services.AddMemoryCache();

请注意,静态类(工具方法和扩展方法除外)、单例(创建自身实例而不是通过DI)活动记录模式在C#world中非常不受欢迎,所有这些模式都被认为是反模式和不良做法,因为它们会导致创建难以维护甚至难以测试的代码我认为这是一条路,但当我尝试代码时,我出现了一个错误:在尝试激活时无法解析“Infrastructure.UserRepository”类型的服务。。。我应该为它设置一个接口吗?当然,您需要在启动时注册它,
services.AddScoped()
services.AddScoped()
,这取决于您是否将它拆分为一个接口。您可以并且应该将其拆分为一个接口,这提高了代码的可测试性,尽管您也可以注入具体的类,但这样您就失去了在单元测试中轻松模拟它的能力