5

I am using Castle to create my database context based on a given interface. I have the following code in my Installer class and this works fine at the moment.

private ConfigureDelegate ConfigureContext()
{
    return p => p.Named(p.ServiceType.Name)
        .LifeStyle.PerWebRequest
        .DependsOn(new { connectionString = ConfigurationManager.ConnectionStrings["conStringName"].ConnectionString });
}

However i now have a scenario where this installer will find more than one concrete implementation of my interface, where each one should have a different connection string supplied.

Is this possible - if so, could someone point me in the right direction.

TIA

bkaid
  • 51,465
  • 22
  • 112
  • 128
Adam Stewart
  • 1,983
  • 1
  • 18
  • 25

1 Answers1

6

Yes, it's possible if you can write a piece of code that provides the connection string name for the service. Perhaps something like this:

private ConfigureDelegate ConfigureContext()
{
    return p => p.Named(p.ServiceType.Name)
        .LifeStyle.PerWebRequest
        .DependsOn(new
        {
            connectionString =
                ConfigurationManager
                    .ConnectionStrings[GetConnectionName(p.ServiceType.Name)]
                    .ConnectionString
        });
}

private string GetConnectionName(string serviceName)
{
    // return the connection name
}
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736