2009-11-22 49 views
0

配置:温莎城堡IOC:如何初始化服务和库层


component id="customerService" service="MyApp.ServiceLayer.ICustomerService`1[[MyApp.DataAccess.Customer, MyApp.DataAccess]], MyApp.ServiceLayer" type="MyApp.ServiceLayer.CustomerService, MyApp.ServiceLayer" 

控制器:


     private ICustomerService _service; 

     public CustomerController() 
     { 
      WindsorContainer container = new WindsorContainer(new XmlInterpreter()); 
      _service = container.Resolve>("customerService"); 
     } 

服务层:


     private ICustomerRepository _repository; 

     public CustomerService(ICustomerRepository repository) 
     { 
      _repository = repository; 
     } 

错误:

 
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. 

回答

0

你需要某种类型的应用程序的initalization,在那里你可以将所有的组件注册,你应该尽量不用有任何类中直接使用的容器。

您的问题似乎是因为,虽然您注册了ICustomerService,但它使用了您尚未注册的ICustomerRepository,因此无法为您创建ICustomerService。

0

我忘了补充资源库组件:

<component id="customerRepository" service="MyApp.Repository.ICustomerRepository`1[[MyApp.DataAccess.Customer, MyApp.DataAccess]], MyApp.Repository" type="MyApp.Repository.CustomerRepository, MyApp.Repository"/> 

现在所有的工作..