Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 402
Mark Avatar asked Jul 20 '26 00:07

Mark


1 Answers

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);
like image 162
Mark Heath Avatar answered Jul 21 '26 15:07

Mark Heath