Understanding Unity and Injection Dependencies with MVVM and Prism
I started using the Microsoft Prism framework with Unity for a WPF application, mainly to teach me new concepts.
I'm trying to understand dependency injection and how to use Unity with my view modes, but I don't have a close enough understanding of what I'm doing to even really ask what I'm doing wrong.
Hence, I will go over the scenario I am in and hope someone can help me figure out where I am going wrong.
Consider an EventAggregator script that publishes ModuleA and subscribes ModuleB. In my ModuleA MainWindowViewModel
, I would have a class like this:
private IEventAggregator _eventAggregator;
public MainWindowViewModel(IEventAggregator eventAggregator) {
_eventAggregator = eventAggregator;
...
}
Now when I register my ModuleA MainWindowView
, I would do something like this:
public class ModuleA {
private readonly IRegionManager _regionManager;
public ModuleA(IRegionManager regionManager) {
_regionManager = regionManager;
}
public void Initialize() {
_regionManager.RegisterViewWithRegion("SomeRegion", typeof(MainWindowView));
}
}
Then, in my project Bootstrapper.cs
, I would create mine ModuleCatalog
:
public class Bootstrapper {
...
protected override void ConfigureModuleCatalog() {
base.ConfigureModuleCatalog();
ModuleCatalog moduleCatalog = (ModuleCatalog)this.ModuleCatalog;
moduleCatalog.AddModule(typeof(ModuleA.ModuleA));
...
}
}
Now I could use ServiceLocator
to instantiate my eventAggregator
in my ViewModel, but I'm trying to do this through Dependency Injection by registering the ViewModel with IUnityContainer
and then adding my View as needed. Also, I see all the time that I have to use an interface for my ViewModel (i.e. IMainWindowViewModel
) to separate concerns.
Can anyone point me to a resource that could clear up the apparent confusion I have? I've read MSDN Prism QuickStarts, including Advanced MVVM Scenarios, but I don't understand how to contextualize the instructions.
source to share
-
Now I could use
ServiceLocator
to instantiateeventAggregator
in mineViewModel
, but I'm trying to do this through Dependency Injection, registering mineViewModel
withIUnityContainer
and then injecting mineView
as needed.Two different mechanisms were mentioned:
Service locator and dependency injection
The fundamental choice is service locator and dependency injection. The first point is that both implementations provide a fundamental decoupling that the naive example lacks — in both cases, the application code is independent of the particular implementation of the service interface. An important difference between the two patterns is how this implementation is exposed to the application class. Using a service locator, the application class requests it explicitly by sending a message to the locator. When entered, there is no explicit request, the service appears in the application class - hence the inverse of the control.
- Control Container Inversion and Dependency Injection Pattern, Martin Fowler .
Conclusion. There should be an architectural decision: "What mechanism should I use?"
-
Also, I see all the time that I have to use an interface for mine
ViewModel
(i.e.IMainWindowViewModel
) to share concerns.This is how the Dependency Inversion Principle applies.
Dependency Inversion Principle:
and. High level modules should not depend on low level modules. Both should depend on abstraction.
B. Abstractions should not depend on details. The details should depend on the abstraction.
Where "Details" are concrete types, "Abstractions" are interfaces.
Please refer to the links provided for more information.
source to share