2009-11-22 47 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"/> 

現在所有的工作..