Yes I know, it is better to use Constructor Injection and I do use it most of the time but, there is this one scenario where I like to use Property Injection:
I have a repository base class which has dependencies of a database factory and also a service to read app settings (abstraction of ConfigurationManager)
public abstract class RepositoryBase(){
public IDatabaseFactory DatabaseFactory { get; set; }
public IAppConfigService AppConfig { get; set; }
protected Database Db
{
get
{
var db = DatabaseFactory.Get();
... not relevant code ...
return db;
}
}
}
I don't want to use constructor dependencies here because this will force me to add constructors to all my repositories, I could do it, but I don't want to. Even worse issue is if for some reason I add a log service to the RespositoryBase class I'll have to add this new dependency to the constructor of all my repository classes, this is not cool.
I could use some pattern like service aggregation to avoid modifying the RepositoryBase class, but this just seems too complicated and I really like to keep things simple so everybody can understand what is going on (including me).
So, is it possible to have these dependencies injected by Nancy / TinyIOC?