views:

53

answers:

2

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>();

This 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.

+1  A: 

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);
}
BFree
I may not know the fully qualified type name. Using reflection I may still be able to get a Type object for an interface from an assembly, but I was hoping that was something the container could do as it already has meta-data about what types is has loaded for resolution.
Firestrand
Fair enough. The only other option (using Unity) is to loop through the Registrations collection of the UnityContainer which gives you a collection of ContainerRegistration objects that has a RegisteredType property on it. You can match the type name of that type to what you already have, and if they match, you can use that type to resolve using the method I described. I'll edit my post with a sample...
BFree
A: 

You can use the IOC container from the Castle project.

klausbyskov
Link to an example? I'll look through the documentation, but that is what I was hoping to avoid.
Firestrand
Put some thought into your answer, instead of just hoping to get points for saying Castle Project.
Nix
@Firestrand: You were asking for a container that can resolve a type given a string. Windsor can do that. But you will have to initialize the mappings yourself somehow. It's not 100% clear to me what kind of data you have stored in your tables. I suppose you have some kind of mapping from name to fully qualified type name? Or what? It's hard for me to help you with an example without knowing this.
klausbyskov
@Nix practice what you preach yourself or stop preaching.
klausbyskov
@klausbyskov Actually, the method in Windsor that allows resolving with a string is just like in Unity. It just allows you to resolve the type if the string was used as a key to register that type. It won't allow you to pass in a Type name as a string and resolve that type.
BFree
@BFree, correct. But the types have to be registered at some point? Otherwise how will it ever work
klausbyskov
@klausbyskov You're right, but you don't need to know that to answer the question. I agree that it seems like an odd request, but you still have information to answer the question.
BFree
@klausbyskov what would you recommend that I practice?
Nix