I would like to be able to use ninject to inject all instances of a particular generic type into a class. For example I have a bunch of custom extractors of a format similar to:
public interface IExtract<TEntity>
{
TEntity ExtractFrom(MyBulkExportedEntity exportedEntity);
}
and I want to inject all instances of these extractors into a class responsible for processing this file using ninject multiple binding.
ie
public class ProcessDataExtract
{
/*This isn't valid c# but demonstrates the intent of what i would like to do*/
public ProcessDataExtract(IEnumerable<IExtract<>> allExtractors)
{
}
public void Process(MyBulkExportedEntity exportedEntity)
{
/*loop through all of the extractors and pull relevant data from the object*/
}
}
In the past i have done this by having a management class (IProvideExtractors) which accesses the kernel directly but i don't like this method and was wondering if anyone knows of a better way to do this. With ninject multiple binding I can then get all of the instances im interested in using kernel.GetAll(typeof(IExtract<>))
Process
method is it needed for theIExtract<TEntity>
to be generic? Because if not then I would create a non genericIExtract
andIExtract<TEntity>
would inherit fromIExtract
. And with the proper registration in yourProcessDataExtract
constructor you would depend onIEnumerable<IExtract> allExtractors
– Casiano