Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/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# 在.NET核心MVC中,如何在控制器外部执行缓存和依赖项注入_C#_Asp.net_Asp.net Core_Dependency Injection_Asp.net Core Mvc - Fatal编程技术网

C# 在.NET核心MVC中,如何在控制器外部执行缓存和依赖项注入

C# 在.NET核心MVC中,如何在控制器外部执行缓存和依赖项注入,c#,asp.net,asp.net-core,dependency-injection,asp.net-core-mvc,C#,Asp.net,Asp.net Core,Dependency Injection,Asp.net Core Mvc,我使用DI纯粹是为了在控制器内进行测试,奇怪的是,在控制器外使用DI真的很困难。我有一个名为caching engine的静态缓存类,但显然DI和静态类不能很好地混合,所以我决定将其改为非静态。然而,我不能让它很好地工作,我不确定什么是最好的方法。我有一个控制器,我需要传递产品并将其发送到视图。然而,为了提高速度,我想使用内存缓存,但我对这里的最佳设计感到困惑。我想知道最好的方法 1) 如果不传递依赖项,实例化新类如何与DI一起工作 2) 我是否应该将memorycache和产品存储库注入控制器

我使用DI纯粹是为了在控制器内进行测试,奇怪的是,在控制器外使用DI真的很困难。我有一个名为caching engine的静态缓存类,但显然DI和静态类不能很好地混合,所以我决定将其改为非静态。然而,我不能让它很好地工作,我不确定什么是最好的方法。我有一个控制器,我需要传递产品并将其发送到视图。然而,为了提高速度,我想使用内存缓存,但我对这里的最佳设计感到困惑。我想知道最好的方法

1) 如果不传递依赖项,实例化新类如何与DI一起工作

2) 我是否应该将memorycache和产品存储库注入控制器,然后将它们传递给cachingengine构造函数?这似乎有很多不必要的参数传递,所以我不喜欢这样

3) 我是否应该在缓存引擎中实例化一个MemoryCache类,而不用担心DI

4) 我应该把cachingene切换回静态类吗

谢谢你的帮助和建议。非常感谢

这是Startup.cs

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        //Add Dependencies
        services.AddTransient<IProductRepository, ProductRepository>();

        //Extention method that sets up the shared objects used in MVC apps
        services.AddMvc();
        services.AddMemoryCache();
        ....
    }
}
下面是缓存类:

public class CachingEngine
{
    private readonly IMemoryCache memoryCache;
    private IProductRepository prodRepo;

    public CachingEngine(IMemoryCache memory, IProductRepository rep)
    {
        memoryCache = memoryCache;
        prodRepo = rep;
    }

    public List<Product> GetProducts()
    {
        var cacheKey = "Products";
        List<Product> prods;
        if (memoryCache.TryGetValue(cacheKey, out prods))
        {
            return prods;
        }
        else
        {
            memoryCache.Set(cacheKey, prodRepo.Products);
            return prods;
        }
    }
}
公共类cachingene
{
私有只读IMemoryCache memoryCache;
私人iprepo;
公共缓存引擎(IMemoryCache内存,IPProductRep)
{
memoryCache=memoryCache;
prodRepo=rep;
}
公共列表产品()
{
var cacheKey=“产品”;
列出产品清单;
if(memoryCache.TryGetValue(cacheKey,out prods))
{
返回刺激;
}
其他的
{
memoryCache.Set(cacheKey、prodRepo.Products);
返回刺激;
}
}
}

首先,要澄清的是,静态类不能实例化,所以如何使用依赖注入框架将实例化注入到其构造函数中。这并不是说静态类不能很好地与DI一起工作,它们根本不能工作,并且在依赖注入的上下文中毫无意义

您的控制器需要一个cachingene,因此您需要注入它,在您的软件中设置DI的一个简单规则是:不要使用
new
操作符

每当您使用
new
操作符时,您都会将代码紧密地耦合到特定的类型,而依赖注入正试图解决这个问题

public class Startup
{

   public void ConfigureServices(IServiceCollection services)
   {

    //Add Dependencies
    services.AddTransient<IProductRepository, ProductRepository>();

    //configure DI for IMemoryCache and CachingEngine
    services.AddTransient<IMemoryCache, MyMemoryCacheClass>();
    services.AddTransient<MyICachingEngineInterface, CachingEngine>();

    //Extention method that sets up the shared objects used in MVC apps
    services.AddMvc();
    services.AddMemoryCache();
    ....
   }
}

public class MainController : Controller
{

    private readonly MyICachingEngineInterface _cachingEngine;

    public MainController(MyICachingEngineInterface cachingEngine)
    {

        _cachingEngine = cachingEngine;
    }

    public IActionResult Index()
    {
        var products = _cachingEngine.GetProducts();
        ....
    }
}
公共类启动
{
public void配置服务(IServiceCollection服务)
{
//添加依赖项
services.AddTransient();
//为IMemoryCache和CachingEngine配置DI
services.AddTransient();
services.AddTransient();
//设置MVC应用程序中使用的共享对象的扩展方法
services.AddMvc();
services.AddMemoryCache();
....
}
}
公共类主控制器:控制器
{
私有只读MyICachingEngineInterface\u cachingene;
公共主控制器(Myicachingengine接口cachingEngine)
{
_cachingEngine=cachingEngine;
}
公共IActionResult索引()
{
var products=_cachingene.GetProducts();
....
}
}

谢谢。我知道这可能是一个愚蠢的问题,但老实说,我只是想在典型的控制器\单元测试情况之外,把我的头绕到DI上,思考如何在我的软件的所有方面应用它,如果这样做是有意义的。那么,如果我想在控制器之外做这件事,最好的方法是什么?这个问题太模糊了,这在任何地方都是一样的,依赖注入将把您需要的注入构造函数中,并且您不使用new关键字,如果您自己实例化类,您将使用DI服务为您创建它“如果您自己实例化类,您将使用DI服务为您创建它”是,我想问另一个问题,所以我想问你这个问题
public class Startup
{

   public void ConfigureServices(IServiceCollection services)
   {

    //Add Dependencies
    services.AddTransient<IProductRepository, ProductRepository>();

    //configure DI for IMemoryCache and CachingEngine
    services.AddTransient<IMemoryCache, MyMemoryCacheClass>();
    services.AddTransient<MyICachingEngineInterface, CachingEngine>();

    //Extention method that sets up the shared objects used in MVC apps
    services.AddMvc();
    services.AddMemoryCache();
    ....
   }
}

public class MainController : Controller
{

    private readonly MyICachingEngineInterface _cachingEngine;

    public MainController(MyICachingEngineInterface cachingEngine)
    {

        _cachingEngine = cachingEngine;
    }

    public IActionResult Index()
    {
        var products = _cachingEngine.GetProducts();
        ....
    }
}