0
votes

We have a WPF application split into a few C sharp projects. Our Business layer needs to also register some Service objects with the Container. We believe the way to do this with Unity is

 Container.RegisterType<ISomeClass,SomeClass>();

in the Business layer C sharp library and then in the main UI application

 UnityContainer.AddNewExtension<T>(); 

The above is detailed here https://www.c-sharpcorner.com/article/dependency-injection-using-unity-resolve-dependency-of-dependencies/

If we are using Prism what are the equivalent methods? Specifically we were thinking of just using Unity (not Prism )in the business library so that we can port it to Xamarin Mac and Linux. We would use Prism in the WPF UI application project only. Could this work?

Thanks

2

2 Answers

1
votes

Technically the answer from Haukinger is correct as you can always access the underlying container when/if you need to. In this case though, it probably is not the best approach here. Both the Bootstrapper and PrismApplication have a virtual method CreateContainerExtension. This is where Prism.Unity does:

// new UnityContainer() is created by default here...
return new UnityContainerExtension();

What I would suggest is that you configure the container upfront

protected override IContainerExtension CreateContainerExtension()
{
    var container = new UnityContainer();
    container.AddExtension<SomeExtension>();
    return new UnityContainerExtension(container);
}
1
votes

If we are using Prism what are the equivalent methods?

There are some methods in IContainerRegistry, but their behavior isn't well documented (besides "they do what the container's method they're wrapping does") and they only cover the most common use cases.

Good news is, you can get the container back by using the GetContainer extension method for your container:

using Prism.Unity.Ioc;

containerRegistry.GetContainer().AddExtension<TheNeatExtension>();