Castle Windsor IoC: How to Initialize Service and Repository Layer
Configuration:
component id="customerService" service="MyApp.ServiceLayer.ICustomerService`1[[MyApp.DataAccess.Customer, MyApp.DataAccess]], M开发者_如何学编程yApp.ServiceLayer" type="MyApp.ServiceLayer.CustomerService, MyApp.ServiceLayer"
Controller:
private ICustomerService _service;
public CustomerController()
{
WindsorContainer container = new WindsorContainer(new XmlInterpreter());
_service = container.Resolve>("customerService");
}
Service Layer:
private ICustomerRepository _repository;
public CustomerService(ICustomerRepository repository)
{
_repository = repository;
}
Error:
Can't create component 'customerService' as it has dependencies to be satisfied. customerService is waiting for the following dependencies: Services: - MyApp.Repository.ICustomerRepository`1[[MyApp.DataAccess.Customer, MyApp.DataAccess, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] which was not registered.
You need some kind of initalization of your application, where you can register all your components, you should try not have to have any of your classes from using the container directly.
Your problem seems to be because, while you have registered the ICustomerService, it uses ICustomerRepository which you have not registered, so cannot create the ICustomerService for you.
I forgot to add the repository component:
<component id="customerRepository" service="MyApp.Repository.ICustomerRepository`1[[MyApp.DataAccess.Customer, MyApp.DataAccess]], MyApp.Repository" type="MyApp.Repository.CustomerRepository, MyApp.Repository"/>
It all work now..
精彩评论