问题描述
我的项目中有一个通用存储库.考虑以下控制器片段
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.
我在 Startup.cs
中为 ConfigureServices
方法中的每个表添加一个 AddScoped
行
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.
我的问题:
在
Startup.cs
的ConfigureServices
方法中为每个查找表设置 services.AddScoped 真的是最佳实践吗?
Is it really best practice to have a services.AddScoped for each and every lookup table in the
ConfigureServices
method ofStartup.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
对象的地方.)然后提供 开放泛型类型 你的接口和实现:
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 行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!