I'm attempting to use Ninject to inject repositories into controllers of my MVC project.
我正在尝试使用Ninject将库注入到MVC项目的控制器中。
public class HomeController : Controller
{
private readonly ICustomerRepository _customerRepository;
//
// GET: /Home/
public HomeController(ICustomerRepository customerRepository)
{
_customerRepository = customerRepository;
}
}
This all makes sense and is easy enough, but when the view gets more complicated and needs to display a master detail scenario, do I inject both repository interfaces in? Does that change if it gets 4 or 5 levels deep? (e.g. User picks customer, project, group, division, and then gets a list of people)
这一切都是有意义的,并且非常简单,但是当视图变得更加复杂并且需要显示一个主细节场景时,我是否将两个存储库接口都注入其中?如果它的深度是4或5级,它会改变吗?(例如,用户选择客户、项目、团队、部门,然后获取人员列表)
There were three things I came up with.
我想到了三件事。
- Inject all the necessary repositories via the constructor.
- 通过构造函数注入所有必要的存储库。
- Create the concept of a super repository, or
- 创建超级存储库的概念。
- Create partial views and controllers for each of repositories.
- 为每个存储库创建部分视图和控制器。
Is there a best-practice on the pattern I should use for this? Any insight would be great.
对于我应该使用的模式是否有最佳实践?任何洞察力都是伟大的。
2 个解决方案
#1
1
Option 3 is the best - option 1 increases the number of dependencies for HomeController
which will make it overly complicated as the number of data sources increases. Option 2 creates a similar problem for your 'super-repository' as it will need a large number of methods to support all the queries in your system.
选项3是最好的——选项1增加了HomeController的依赖项数量,随着数据源的增加,这会使其变得过于复杂。选项2为您的“超级存储库”创建了一个类似的问题,因为它需要大量的方法来支持系统中的所有查询。
Creating a controller each for a number of smaller forms will allow each one to be fairly simple and easier to reason about.
为许多较小的表单创建一个控制器,将使每一个控制器都变得相当简单和容易推理。
#2
0
I've been learning toward thinking of the controller as just that --a controller of everything. If you take that approach then I think its totally fine to have it know about multiple repositories/services.
我一直在学习如何把控制器看成是所有东西的控制器。如果您采用这种方法,那么我认为完全可以让它了解多个存储库/服务。
The master repository approach works as well as there's less instantiation needed on the controller side, but its a bit less explicit on what you're doing and makes the controller rely more on internal knowledge of the repository/service.
主存储库方法工作得很好,而且在控制器端需要的实例化更少,但是它对您正在做的事情的显式更少,使控制器更依赖于存储库/服务的内部知识。
#1
1
Option 3 is the best - option 1 increases the number of dependencies for HomeController
which will make it overly complicated as the number of data sources increases. Option 2 creates a similar problem for your 'super-repository' as it will need a large number of methods to support all the queries in your system.
选项3是最好的——选项1增加了HomeController的依赖项数量,随着数据源的增加,这会使其变得过于复杂。选项2为您的“超级存储库”创建了一个类似的问题,因为它需要大量的方法来支持系统中的所有查询。
Creating a controller each for a number of smaller forms will allow each one to be fairly simple and easier to reason about.
为许多较小的表单创建一个控制器,将使每一个控制器都变得相当简单和容易推理。
#2
0
I've been learning toward thinking of the controller as just that --a controller of everything. If you take that approach then I think its totally fine to have it know about multiple repositories/services.
我一直在学习如何把控制器看成是所有东西的控制器。如果您采用这种方法,那么我认为完全可以让它了解多个存储库/服务。
The master repository approach works as well as there's less instantiation needed on the controller side, but its a bit less explicit on what you're doing and makes the controller rely more on internal knowledge of the repository/service.
主存储库方法工作得很好,而且在控制器端需要的实例化更少,但是它对您正在做的事情的显式更少,使控制器更依赖于存储库/服务的内部知识。