ASP.NET Core 中的通用存储库,而 Startup.cs 中的每个表没有单独的 AddScoped 行? [英] Generic repository in ASP.NET Core without having a separate AddScoped line per table in Startup.cs?

查看:20
本文介绍了ASP.NET Core 中的通用存储库,而 Startup.cs 中的每个表没有单独的 AddScoped 行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中有一个通用存储库.考虑以下控制器片段

I have a generic repository in my project. Consider the following controller snippet

public class Lookup1Controller : Controller
{
    readonly MyDbContext _db;

    public Lookup1Controller(MyDbContext dataContext)
    {
        _db = dataContext;
    }

    public async Task<IActionResult> Index()
    {

        IGenericRepository<Lookup1> _repository = new GenericRepository<Lookup1>(_db);
        var lookup1s = await _repository.SelectAll();

        return  View(lookup1s);
    }

我认为不需要在我的通用存储库和每个控制器中都有我的数据库引用.

I don't see the need to have my Database reference both in my Generic repository as well as each of my controllers.

我将其重构为:

public class Lookup1Controller : Controller
{
    private IGenericRepository<Lookup1> _repository;

    public Lookup1Controller(IGenericRepository<Lookup1> repository)
    {
        _repository = repository;
    }

    public async Task<IActionResult> Index()
    {
        var lookup1s = await _repository.SelectAll();

        return  View(lookup1s);
    }

}

从我读到的内容来看,这是更简洁的 ASP.NET 5 最佳实践.但如果我在浏览器中访问该控制器路由,则会出现以下错误:

which is much neater and ASP.NET 5 best practice from what I read. but I will get the following error if I access that controller route in my browser:

InvalidOperationException: Unable to resolve service for type 'MyProject.Data.IGenericRepository`1[MyProject.Models.Lookup1]' while attempting to activate 'MyProject.Controllers.Lookup1.

因为我没有注入GenericRepository来使用接口.

because of I haven't injected the GenericRepository to use the interface.

我为 ConfigureServices 方法中的每个表添加了 AddScoped 行到我的 Startup.cs

I add to my Startup.cs an AddScoped line for each and every of my tables in the ConfigureServices method

services.AddScoped<IGenericRepository<Lookup1>,GenericRepository<Lookup1>> ();
services.AddScoped<IGenericRepository<Lookup2>,GenericRepository<Lookup2>> ();
services.AddScoped<IGenericRepository<Lookup3>,GenericRepository<Lookup3>> ();
services.AddScoped<IGenericRepository<Lookup4>,GenericRepository<Lookup4>> ();
etc

这样我的代码就可以运行而不会抛出异常.

so that my code runs without throwing an exception.

但是我的数据库有大约 100 个简单的查找表.当我查看上面的 100 行代码时,它看起来并不正确.

However my database has about 100 simple lookup tables. When I look at the above 100 lines of code it just doesn't look right.

感觉就像复制和粘贴代码.每次我通过添加新模型和带有视图的控制器来添加新表时,我的代码将编译而不会出错.但是,如果我运行该程序并转到该视图,如果我忘记将 AddScoped 行添加到我的 Startup.cs,我可能会得到控制器运行错误.不太利于维护.

It feels like copy and paste code. Each time I add a new table by adding a new model and controller with view my code will compile without giving me an error. But if I run the program and go to that view I could get the controller run error if I forgot to add the AddScoped line to my Startup.cs. Not really good for maintainability.

我的问题:

  1. Startup.csConfigureServices 方法中的每个查找表设置一个 services.AddScoped 真的是最佳实践吗?

  1. Is it really best practice to have a services.AddScoped for each and every lookup table in the ConfigureServices method of Startup.cs?

这是一个通用存储库,所以没有办法将这 100 行复制和粘贴行写在一行中吗?

It is a generic repository so isn't there be a way to write those 100 copy and paste lines in one line?

如果不是,那么使用我的代码执行此操作的最佳实践方法是什么?

If not then what is the best practice way to do this using my code?

推荐答案

只需使用非通用注册重载(您需要传递 2 个 Type 对象的地方.)然后提供 <接口和实现的 href="https://stackoverflow.com/questions/2173107/what-exactly-is-an-open-generic-type-in​​-net">开放泛型类型:

Just use the non-generic registration overloads (the ones where you need to pass the 2 Type objects.) Then provide the open generic types of both your interface and the implementation:

services.AddScoped(typeof(IGenericRepository<>), typeof(GenericRepository<>));

在您的控制器中,为特定类型(封闭的泛型类型)的存储库添加依赖项:

In your controller, add a dependency for a repository of a specific type (a closed generic type):

public HomeController(IGenericRepository<Lookup1> repository)
{
    ...
}

这篇关于ASP.NET Core 中的通用存储库,而 Startup.cs 中的每个表没有单独的 AddScoped 行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆