Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Castle Windsor problem

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.

like image 741
k1. Avatar asked Dec 05 '25 01:12

k1.


1 Answers

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.

like image 61
Mark Seemann Avatar answered Dec 07 '25 15:12

Mark Seemann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!