Unity dynamic mapping

I am new to Unity Dependency Injection and ask a question that is probably very straightforward.

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

For example, would I do something like this?

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

Thanks in advance

+3
source share
1 answer

You can specify target types in your database using a qualified name assembly

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);
+2
source

Source: https://habr.com/ru/post/1774289/


All Articles