Mark
Mark

Reputation: 6081

Unity dynamic mapping

I'm new to Unity Dependency Injection and have a question thats probably very straight forward..

I would like to register type mappings based on configuration pulled from my database model. How and what's the best way to do this?

For example would I do something like this?

myContainer.RegisterType<IMyType, /*My dynamic config value*/>();

Thanks in advance

Upvotes: 3

Views: 693

Answers (1)

Mark Heath
Mark Heath

Reputation: 49482

You could specify target types in your database using assembly qualified names

IUnityContainer container = new UnityContainer();
//container.RegisterType<IFoo,Foo>();
Type to = Type.GetType("TestApp.Foo, TestApp");
container.RegisterType(typeof(IFoo),to);
var foo = container.Resolve<IFoo>();
Assert.IsInstanceOf<Foo>(foo);

Upvotes: 2

Related Questions