views:

55

answers:

1

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 requiring a connection string, despite the ${dataSession.DbConnection} being an IDbConnection.

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

+3  A: 

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)
    { }
}
Damian Powell