3
votes

I have a problem with castle core, i'm trying to inject two different database connection to specific repositories.

public class Repository1 { 
  public Repository1(System.Data.Common.DbConnection conn) { } 
}
public class Repository2 { 
  public Repository2(System.Data.Common.DbConnection conn) { } 
}

Now for example im would like to inject Mysql connection to Repository1 and Oracle connection to repository2.

1

1 Answers

5
votes

Something like this:

container.Register(Component
    .For<DbConnection>()
    .ImplementedBy<MysqlConnection>()
    .Named("mysql"));
container.Register(Component
    .For<DbConnection>()
    .ImplementedBy<OracleConnection>()
    .Named("oracle"));

container.Register(Component
    .For<Repository1>()
    .ServiceOverrides(new { conn = "mysql" }));
container.Register(Component
    .For<Repository2>()
    .ServiceOverrides(new { conn = "oracle" }));

You may need to tweak the DbConnection registrations, since I don't know what the exact class names might be, or whether they require other configuration settings.