Lets say I have a class:
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?
IConfigurationRepositoryis a dependency ofMySQLRepositorywhich is resolved asIDbRepository, which is a dependency ofFoo- the dependency graph is pretty straight forward, which is why I don't get the issue you are facing ... (btw:IDbRepositoryis not connected toFooBase.ctor)