代码之家  ›  专栏  ›  技术社区  ›  Neil Barnwell

Castle Windsor使用错误的组件来满足依赖关系

  •  0
  • Neil Barnwell  · 技术社区  · 14 年前

    我在windsor xml中有以下组件映射:

    <component
      id="dataSession.DbConnection"
      service="System.Data.IDbConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
      type="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
      lifestyle="custom"
      customLifestyleType="MyCompany.Castle.PerOperationLifestyle.PerOperationLifestyleManager, MyCompany.Castle">
      <parameters>
        <connectionString>server=(local);database=MyCompany;trusted_connection=true;application name=OperationScopeTest;</connectionString>
      </parameters>
    </component>
    
    <component
      id="dataSession.DataContext"
      service="System.Data.Linq.DataContext, System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"
      type="MyCompany.BusinessLogic.MyCompanyDataContext, MyCompany.BusinessLogic"
      lifestyle="custom"
      customLifestyleType="MyCompany.Castle.PerOperationLifestyle.PerOperationLifestyleManager, MyCompany.Castle">
      <parameters>
        <connection>${dataSession.DbConnection}</connection>
      </parameters>
    </component>
    

    但是,当我要求容器 DataContext ,它实际上使用了需要连接字符串的构造函数,尽管 ${dataSession.DbConnection} 作为一个 IDbConnection .

    为什么会这样,以及如何让温莎使用正确的构造器?

    1 回复  |  直到 14 年前
        1
  •  3
  •   Damian Powell    14 年前

    据我所知,无法在具有相同数量、相同名称但不同类型的参数的不同构造函数之间进行windsor解析。这里的问题是 DataContext 构造函数有一个同名的参数。

    在我工作的地方,我们通过从 数据上下文 它只有一个构造器,温莎可以满足,从而消除了这个问题。

    public class MyCompanyDataContextAdapter : MyCompanyDataContext
    {
        public MyCompanyDataContextAdapter(IDbConnection connection)
            : base(connection)
        { }
    }