开发者

Castle Windsor using wrong component to satisfy a dependency

开发者 https://www.devze.com 2023-01-02 17:17 出处:网络
I have the following component mapping in Windsor xml: <component id=\"dataSession.DbConnection\" service=\"System.Data.IDbConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken

I have the following component mapping in 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>

However, when I ask the container for a DataContext, it actually uses the constructor requi开发者_如何学Pythonring a connection string, despite the ${dataSession.DbConnection} being an IDbConnection.

Why is this, and how to I make Windsor use the correct constructor?


As far as I'm aware, there's no way to make Windsor resolve between different constructors that have the same number of arguments with the same names but different types. The problem here is that all of the DataContext constructors have a single argument with the same name.

Where I work, we solved this issue by deriving a class from DataContext which only had one constructor that Windsor could satisfy thus eliminating the problem.

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

精彩评论

暂无评论...
验证码 换一张
取 消