I have one interface: IFoo
Two classes implementing that interface: FooOne
and FooTwo
And two classes ClassOne
and
Have a look at the Unity documentation.
For a more readable config file you should define type aliases for IFoo
, FooOne
, FooTwo
, ClassOne
and ClassTwo
. Then you need to register the mappings from IFoo
to your implementations. You need to set a name
for the mappings.
For the consumers of IFoo
you need to register an InjectionConstructor
.
Your config will look something like this:
That's the corresponding test that shows how it works.
UnityConfigurationSection config =
(UnityConfigurationSection) ConfigurationManager.GetSection("unity");
IUnityContainer container = new UnityContainer();
container.LoadConfiguration(config);
ClassTwo two = container.Resolve();
Assert.IsInstanceOfType(two.Foo, typeof(FooTwo));
Update
At runtime you can do it like this
IUnityContainer container = new UnityContainer();
container.RegisterType("One");
container.RegisterType("Two");
container.RegisterType(new InjectionConstructor(
new ResolvedParameter("One")));
container.RegisterType(new InjectionConstructor(
new ResolvedParameter("Two")));