【问题标题】:Castle windsor : how to not to load all components in a castle windsor xml configuration when "Resolve" is called温莎城堡:调用“解决”时如何不加载城堡温莎 xml 配置中的所有组件
【发布时间】:2010-03-04 09:33:58
【问题描述】:

我想将“策略模式”用于 2 个 Web 项目、我的配置文件(并且仅用于配置文件):

<?xml version="1.0"?>
<configuration>
    <components>
      <!-- container ORM -->
      <component id="DALReseauContainer"  type="ReseauRules.Db.BLL.DbReseauWorkingContextContainer, ReseauRules"                       service="ReseauConnector.BLL.IReseauWorkingContextContainer, ReseauConnector">        
      </component>

      <!-- remote controler (Web Service) -->
      <component id="remoteReseauManager"
                 type="ReseauConnector.BLL.RemoteReseauManager, ReseauConnector"
                 service="ReseauConnector.BLL.IReseauManager, ReseauConnector"
                 lifestyle="transient">        
      </component>

      <!-- local controler  -->
      <component id="localReseauManager"
                 type="ReseauRules.Db.BLL.DbReseauManager, ReseauRules"
                 service="ReseauConnector.BLL.IReseauManager, ReseauConnector"
                 lifestyle="transient">
        <parameters>
          <container>${DALReseauContainer}</container>
        </parameters>
      </component>
    </components>
</configuration>

项目A使用“remoteReseauManager”,只引用ReseauConnector,可以调用

container.Resolve<IReseauManager>("remoteReseauManager");

项目B使用“localReseauManager”并引用ReseauConnector AND ReseauRules,可以调用

container.Resolve<IReseauManager>("localReseauManager");

当我打电话时

IWindsorContainer container = new WindsorContainer(new Castle.Windsor.Configuration.Interpreters.XmlInterpreter()); 

Windsor 尝试解析每个组件,并且在项目 A 中,ReseauRules 不存在,Windsor 无法加载 ReseauRules(如预期的那样)。

如何告诉 Windsor 不要在此上下文中加载组件(配置文件)?

非常感谢。

【问题讨论】:

    标签: xml configuration castle-windsor ioc-container lazy-loading


    【解决方案1】:

    您应该每个应用程序有一个容器,因此在这种情况下,每个应用程序都有一个 Windsor 配置也是最有意义的。显然,这两个应用程序有不同的依赖要求。

    每个应用程序必须已经有自己的 web.config,因此每个应用程序都有单独的 Windsor 配置。

    如果可维护性是一个问题,XML 无论如何都不是解决方案。如果你有很多共享容器配置,写一个 IWindsorInstaller 实现,只保留 XML 中不同的部分配置。

    您可以混合 XML 配置和配置即代码

    【讨论】:

    • 谢谢,但每个应用程序(即:在每个 web.config 中)可以有相同的城堡配置文件,在 我将研究一个 IWindsorInstaller 实现
    • 是的,我明白这一点,但我的意思是,这不会让你的生活变得更轻松,而是让你的生活更难。
    猜你喜欢
    • 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
    相关资源
    最近更新 更多