Castle Windsor IoC:如何初始化服务和存储库层

时间:2009-11-22 07:17:14

标签: castle-windsor

配置:


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. 

2 个答案:

答案 0 :(得分:0)

您需要对应用程序进行某种初始化,在这里您可以注册所有组件,您应该尝试不必让任何类直接使用容器。

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

答案 1 :(得分:0)

我忘了添加存储库组件:

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

这一切现在都有效..