Unity Container - Lazy injection

Viewed 6049

Lets say i have a clas

class Foo : FooBase {

  public Foo(Settings settings, IDbRepository db)
    : base(settings) {
      this.db = db;
  }

  ...

}

Basically FooBase receives settings via constructor and loads some config from configuration file.

Then i have the class MySQLRepository which implements IDbRepository

class MySQLRepository : IDbRepository {

  ...

  public MySQLRepository(IConfigurationRepository config) {
    conn = new MySQLConnection(config.GetConnectionString());
  }

  ...

}

in Program.cs i have:

Foo foo = container.Resolve<Foo>();

The problem is that the constructor of FooBase is called only after all other dependencies were loaded. but the configuration isn't loaded until the FooBase constructor is called.

My idea is to create a lazy implementation of IDbRepository and any other interfaces that require configuration.

Is this a good idea? How do i implement it with Unity container?

1 Answers
Related