我有以下的项目布局:
当控制器无法访问数据访问层项目时,如何正确地说出
MVC UI
|...CustomerController (ICustomerRepository - how do I instantiate this?)
Data Model
|...ICustomerRepository
DAL (Separate Data access layer, references Data Model to get the IxRepositories)
|...CustomerRepository (inherits ICustomerRepository)
当控制器无法访问数据访问层项目时,如何正确地说出
ICustomerRepository repository = new CustomerRepository();
?或者我完全错了吗?
ICustomerRepository
的具体实例,因此它向容器请求。但我们可以更进一步,我们也可以让容器管理控制器的实例化。使用IoC容器的优点之一是,在实例化控制器时,容器将意识到它依赖于ICustomerRepository
并实例化该依赖项(称为依赖注入)。 - Russ Cam