MVC 3:将多个存储库注入单个控制器
我的表示层位于 MVC 3 中。
我有一个返回存储库的数据访问层。每个存储库都映射到一个表。
例如,我有以下存储库接口:
- ICustomerRepository
- ICustomerTypeRepository
我有这些存储库的实体框架实现。这些实现在运行时通过 Ninject 注入。
我有两个控制器:
- CustomerController
- CustomerTypeController
每个控制器都有一组 CRUD 例程。客户必须有一个类型。
为了创建客户记录,我需要使用 EFCustomerTypeRepository。
我现在在 CustomerController 中有一个构造函数,如下所示:
public class CustomerController: Controller
{
private ICustomerRepository customerRepository;
private ICustomerTypeRepository customerTypeRepository;
public CustomerController(ICustomerRepository customerRepository,
ICustomerTypeRepository customerTypeRepository)
{
this.customerRepository = customerRepository;
this.customerTypeRepository = customerTypeRepository;
}
...
开发处于早期阶段,如果我采用这种方法,那么在接下来的几天内,该构造函数将具有 5 个或更多参数。这看起来不对劲。
我可以将所有这些逻辑存储在 EFCustomerRepository 中,但是,这将打破单一职责原则。所以我不会那样做。
最后,我可以包装这些存储库并将单个 RepositoryWrapper 类型对象传递给我的控制器。
我不想介绍更多的框架、工具。我的目标是实现松散耦合和易于扩展,但目前我陷入了向构造函数传递更多参数或为存储库编写包装器之间的困境。
我正在使用 MVC 3、EF 4.1、Ninject、Moq 和 Automapper。谢谢
My presentation layer is in MVC 3.
I have a data access layer which returns repositories. Each repository is mapped to a single table.
For example, I have the following repository interfaces:
- ICustomerRepository
- ICustomerTypeRepository
I have Entity Framework implementations of these repositories. These implementations get injected at a runtime with Ninject.
I have two controllers:
- CustomerController
- CustomerTypeController
Each controller has a set of CRUD routines. Customer must have a type.
In order to create a customer record, I need to use EFCustomerTypeRepository.
I now have a constructor within a CustomerController which looks this:
public class CustomerController: Controller
{
private ICustomerRepository customerRepository;
private ICustomerTypeRepository customerTypeRepository;
public CustomerController(ICustomerRepository customerRepository,
ICustomerTypeRepository customerTypeRepository)
{
this.customerRepository = customerRepository;
this.customerTypeRepository = customerTypeRepository;
}
...
Development is in early phase, and if I take this approach, then within next few days this constructor will have 5 or more parameters. This doesn't look right.
I could store all this logic in EFCustomerRepository, however, this will break single responsibility principle. So I'm not going to do that.
Finally, I can wrap these repositories and pass a single RepositoryWrapper type object to my controller.
I don't want to introcude any more frameworks, tools than I have to. I'm aiming to achieve loose coupling and easy extensibility, but currently I'm stuck between passing more parameters to a constructor, or writing a wrapper for repositories.
I'm using MVC 3, EF 4.1, Ninject, Moq and Automapper. Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想说你必须注意代码中的聚合(从领域驱动设计的角度来看)。因此,如果您可以使一个类封装许多其他小型类,而这些类本身没有必要存储它们,那么您可以创建一个漂亮的对象图,并且只有一个 ICustomerRepository 来封装对例如客户类型的内部引用。
I would say you have to watch for aggregates in your code (from Domain Driven Design point of view). So if you can make a class encapsulate a lot of small other classes for which there's no use storing them by themselves you can create a nice objectgraph and having just a single ICustomerRepository which encapsulates the internal references to for example customer type.
无论如何,将多个存储库注入控制器构造函数不一定是坏习惯。它被称为“构造函数过度注入”,请参阅 Mark Seemann 关于此主题的书。
Anyway, injecting multiple repositories into a controller constructor isn't necessarily a bad practice. It is called "constructor over-injection", see Mark Seemann's book on this topic.