Firestrand
Firestrand

Reputation: 1315

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

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.

Upvotes: 1

Views: 1233

Answers (3)

user179209
user179209

Reputation: 63

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.

Upvotes: 0

BFree
BFree

Reputation: 103760

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

Upvotes: 2

Klaus Byskov Pedersen
Klaus Byskov Pedersen

Reputation: 121007

You can use the IOC container from the Castle project.

Upvotes: -1

Related Questions