【问题标题】:Castle Windsor IoC: How to Initialize Service and Repository LayerCastle Windsor IoC:如何初始化服务和存储库层
【发布时间】:2009-11-22 07:17:14
【问题描述】:

配置:


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;
        }

错误:

无法创建组件“customerService”,因为它需要满足依赖关系。 customerService 正在等待以下依赖项: 服务: - MyApp.Repository.ICustomerRepository`1[[MyApp.DataAccess.Customer, MyApp.DataAccess, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] 未注册。

【问题讨论】:

    标签: castle-windsor


    【解决方案1】:

    您需要对您的应用程序进行某种初始化,您可以在其中注册所有组件,您应该尽量不要让任何类直接使用容器。

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

    【讨论】:

      【解决方案2】:

      我忘记添加存储库组件了:

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

      现在一切正常..

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多