我的表示层在 MVC 3 中。
我有一个返回存储库的数据访问层。每个存储库都映射到一个表。
例如,我有以下存储库接口(interface):
- ICustomerRepository
- ICustomerTypeRepository
我有这些存储库的 Entity Framework 实现。这些实现在运行时使用 Ninject 注入(inject)。
我有两个 Controller :
- 客户总监
- 客户类型 Controller
每个 Controller 都有一组 CRUD 例程。客户必须有一个类型。
为了创建客户记录,我需要使用 ECFustomerTypeRepository。
我现在在 CustomerController 中有一个构造函数,它看起来是这样的:
public class CustomerController: Controller
{
private ICustomerRepository customerRepository;
private ICustomerTypeRepository customerTypeRepository;
public CustomerController(ICustomerRepository customerRepository,
ICustomerTypeRepository customerTypeRepository)
{
this.customerRepository = customerRepository;
this.customerTypeRepository = customerTypeRepository;
}
...
开发处于早期阶段,如果我采用这种方法,那么在接下来的几天内,此构造函数将具有 5 个或更多参数。这看起来不对。
我可以将所有这些逻辑存储在 ECFustomerRepository 中,但是,这将打破单一责任原则。所以我不会那样做。
最后,我可以包装这些存储库并将单个 RepositoryWrapper 类型对象传递到我的 Controller 。
我不想介绍任何不必要的框架和工具。我的目标是实现松散耦合和易于扩展,但目前我在将更多参数传递给构造函数或为存储库编写包装器之间陷入困境。
我正在使用 MVC 3、EF 4.1、Ninject、Moq 和 Automapper。谢谢
最佳答案
我会说您必须注意代码中的聚合(从领域驱动设计的角度来看)。因此,如果您可以让一个类封装许多其他小类,而这些类单独存储它们是没有用的,您就可以创建一个漂亮的对象图,并且只有一个 ICustomerRepository,它封装了对例如客户类型的内部引用。
关于asp.net-mvc-3 - MVC 3 : Injecting multiple repositories into a single controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7080539/