I am having difficult figuring out when a dependency should be injected. Let's just work with a simple example from my project:
class CompanyDetailProvider : ICompanyDetailProvider {
private readonly FilePathProvider provider;
public CompanyDetailProvider(FilePathProvider provider) {
this.provider = provider;
}
public IEnumerable<CompanyDetail> GetCompanyDetailsForDate(DateTime date) {
string path = this.provider.GetCompanyDetailFilePathForDate(date);
var factory = new DataReaderFactory();
Func<IDataReader> sourceProvider = () => factory.CreateReader(
DataFileType.FlatFile,
path
);
var hydrator = new Hydrator<CompanyDetail>(sourceProvider);
return hydrator;
}
}
(Not production quality!)
ICompanyDetailProvider
is responsible for providing instances of CompanyDetail
s for consumers. The concrete implementation CompanyDetailProvider
does it by hydrating instances of CompanyDetail
from a file using a Hydrator<T>
which uses reflection to populate instances of T
sourced from an IDataReader
. Clearly CompanyDetailProvider
is dependent on DataReaderFactory
(which returns instances of OleDbDataReader
given a path to a file) and Hydrator
. Should these dependencies be injected? Is it right to inject FilePathProvider
? What qualities do I examine to decide if they should be injected?