IOC Container Runtime Resolution
I'm trying to find an IOC container that will allow me to have mapping data for a field stored in a database and resolve the interface or object that needs resolved via a string value pulled from the database.
Most of the examples I have seen are using interfaces hard coded in code, I want the interface that needs to be resolved to be dynamic.
This is what I usually see:
var taskController = container.Resolve<ITaskController>();
Th开发者_如何转开发is is what I would like to see:
var strTaskController = "ITaskController";
var taskController = container.Resolve(strTaskController);
I'm sure I could look through the documentation for all the IOC containers but I am hoping this is an easy question for someone with more IOC experience.
Using Unity you can do what you're looking for. Basically, if you know the full type name, you can do this first:
var type = Type.GetType("Fully.Qualified.Type.Name");
var resolvedInstance = container.Resolve(type);
EDIT: Based on the comment, here's another approach:
string typeName = "MyTypeName";
var type = container.Registrations.FirstOrDefault(r => r.RegisteredType.Name == typeName);
if(type != null)
{
var resolvedInstance = container.Resolve(type.RegisteredType);
}
I think this is the answer I am going with.. Managed Extensibility Framework http://msdn.microsoft.com/en-us/library/dd460648.aspx
Got to love it when you find a new framework to find the exact solution to your problem.
You can use the IOC container from the Castle project.
精彩评论