6

I'm developing a WinForm application and I'm using Repository Pattern , I'm using Servicestack OrmLite for data access and Simple Injector for IoC.

In my program.cs I have 2 register 2 different OrmLiteConnectionFactory but currently I'm not able to since I can't register named... with FunQ I can do this with

container.Register<IDbConnectionFactory>("db1", _ => new OrmLiteConnectionFactory(
    ConfigurationManager.ConnectionStrings["db1"].ConnectionString, 
    SqlServerDialect.Provider));
container.Register<IDbConnectionFactory>("db2", _ => new OrmLiteConnectionFactory(
    ConfigurationManager.ConnectionStrings["db2"].ConnectionString, 
    SqlServerDialect.Provider));

here is mine program.cs Bootstrap method

private static void Bootstrap()
{
    // Create the container as usual.
    Container = new Container();

    string conn1 = ConfigurationManager.ConnectionStrings["dbconn1"].ConnectionString;
    string conn2 = ConfigurationManager.ConnectionStrings["dbconn2"].ConnectionString;

    OrmLiteConnectionFactory connectionFactory = new OrmLiteConnectionFactory(conn1,
        ServiceStack.OrmLite.SqlServer.SqlServerOrmLiteDialectProvider.Instance); 
    Container.RegisterSingle<OrmLiteConnectionFactory>(connectionFactory1);

    OrmLiteConnectionFactory connectionFactory2 = new OrmLiteConnectionFactory(conn2,
        ServiceStack.OrmLite.SqlServer.SqlServerOrmLiteDialectProvider.Instance); 

    // how do I register this one without a name?
    Container.RegisterSingle<OrmLiteConnectionFactory>(connectionFactory2);

    Container.Register<MainForm>();
}

Thanks in advance

4
  • How is the application using those OrmLiteConnectionFactory instances? How should the application distinct between the two? Who are you intend to get one or the other? Do these factories point at the databases with the same schema?
    – Steven
    Dec 19, 2014 at 9:22
  • yes the OrmLiteConnectionFactory are used inside different repositories... so I expect on my Repo1 to resolve connectionFactory and on another Repo2 to resolve connectionFactory 2
    – advapi
    Dec 19, 2014 at 10:36
  • Do these repositories connect to databases that have the same schema, or do these schemas differ?
    – Steven
    Dec 19, 2014 at 10:49
  • yes ,I have to point to different Databases thats why I need to register 2 different instance of a OrmLiteConnectionFactory
    – advapi
    Dec 19, 2014 at 11:04

1 Answer 1

16

Since you have have two different databases with each a different schema, the different OrmLiteConnectionFactory aren't interchangable. This means that if you swap them around, your system will stop working. That's an indication that you are breaking the Liskov Substitution Principle. So the general solution in that case is to give each factory its own interface and let each repository depend on that particular interface. That allows you to register them uniquely and let the container resolve the object graph without doubt.

If giving each factory its own abstraction is not an option, or convenient, another option is to register your repositories without auto-wiring. By registering your repository with a delegate, you are completely in control over what to inject into your repository. For instance:

container.Register<IOrderRepository>(() => new SqlOrderRepo(connectionFactory1));

container.Register<IUserRepository>(() => new SqlUserRepo(
    connectionFactory2,
    container.GetInstance<ILogger>()));

A third option is to use context based injection, were you do the following:

var f1 = Lifestyle.Singleton.CreateRegistration<IDbConnectionFactory>(
    () => new OrmLiteConnectionFactory(
            conn1,
            ServiceStack.OrmLite.SqlServer.SqlServerOrmLiteDialectProvider.Instance), 
        container);

var f2 = Lifestyle.Singleton.CreateRegistration<IDbConnectionFactory>(
    () => new OrmLiteConnectionFactory(
            conn2,
            ServiceStack.OrmLite.SqlServer.SqlServerOrmLiteDialectProvider.Instance), 
        container);

container.RegisterConditional(typeof(IDbConnectionFactory), f1, InjectedInto<SqlOrderRepo>);
container.RegisterConditional(typeof(IDbConnectionFactory), f2, InjectedInto<SqlUserRepo>);

// Helper method.
static bool InjectedInto<T>(PredicateContext c) =>
    c.Consumer.ImplementationType == typeof(T);

Out of the box Simple Injector does not support registering keyed registrations, but there are several ways to do this as explained on this page. That page also explains why there is no built-in support for this. This is a deliberate design decision.

2
  • But this register a Single instance or each time a new one is created?
    – advapi
    Dec 22, 2014 at 16:08
  • @advapi: That completely depends. The RegisterWithContext<OrmLiteConnectionFactory> always returns the same instances, because the connectionFactory1 and connectionFactory2 are created once. If that's not what you want, you can create new instances in the lambda.
    – Steven
    Dec 22, 2014 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.