I have Unity 2.0 working well within the App.xaml.cs to register and resolve within that class.
The question I have is regarding a best practice.
I have a number of User Controls and other classes that also need to resolve some of the same and new Interface <-> implementations. The problem is theres no way to access the Unity container I created in the App.xaml.cs.
I cannot use constructor or property injection to pass on the container reference.
- Just too many (its a large project)
- The user controls are added via xaml
- There are several very loosely related "modules" in the project that can share the same container config.
I would rather not re-create the container from a config file in each object that needs access to the container.
Any best practice suggestions when the same container is needed as a service in various "modules" of the same assembly?
Thanks.
I believe bringing together
Controls
andIoC
is pain in the ... in the code at least. Probably somebody will argue but IMO the best practice to avoid this pain isMVVM
. You will have viewModels which you can freely construct usingUnity
and inject everything you need into them. You will have views with bindings to viewModels with no reason to know anything abound inversion of control.UPDATE: Based on comment:
App.xaml.cs:
Shell XAML example:
MainViewModel:
In this way your views will be unaware of IoC and everything you want in viewModels will be successfully injected.
UPDATE2 DataTemplating which brings
Views
andViewModels
together:App.xaml