Arrrgh! I am pulling my hair over here. I have been trying to use IoC containers for a little bit, and all seems fine and dandy until you hit some issue that you think would be very basic, like passing parameters into constructors.
Say I have a class somewhere with a mix of reference classes that can be resolved by IoC and value types (or some other types) that can only be resolved at runtime:
public NFLFeedUnitOfWork(NFLFileType fileType, object feed, IConverterMappings<NFLFileType> nflConverterMappings, IDbContext context)
: base(fileType, feed, nflConverterMappings, context, ContextType.NFL)
{
//new NFLContext(connstringname, setAutoDetectChanges)
}
In this particular example I pass in Enum (NFLFileType), object instance, 2 interface parameters and pass in one extra hardcoded property into the base constructor (ContextType.NFL)
How in the name of all gods can I do this in any IoC container?
The problem is actually 2-fold:
1.) How to pass in an object that is only known at runtime? Say for example the calling code looks like this at the moment:
protected override IFeedUnitOfWork GetUnitOfWork(NFLFileType fileType, object feed, string connectionString)
{
return new NFLFeedUnitOfWork(fileType, feed, new NFLConverterMappings(), new NFLContext(connectionString));
}
How can I convert this code to be using IoC? Perhaps to something like this?
protected override IFeedUnitOfWork GetUnitOfWork(NFLFileType fileType, object feed, string connectionString)
{
return IFLFeedUnitOfWork(fileType, feed);
}
Where the last 2 parameters are automatically resolved, and the 1st 2 I supply by myself?
2.) How can i pass in Enum, object, value types into constructor using IoC? (or maybe refrain from using it in this particular instance?)
Anyway, any help is greatly appreciated, especially on the 1st point. I am using Unity at the moment, but any other IoC container is fine as well.
I don't want to pass in an IoC container into the code either, I only want to specify it in one place at the top level.