I've started using Microsoft's Prism framework with Unity for a WPF application, mainly to teach myself some new concepts.
I've been trying to understand dependency injection and how to use Unity with my viewmodels, but I don't have a firm enough grasp on what I'm doing to even really ask what I'm doing wrong.
Consequently, I'll present the scenario I'm in, and I hope someone can help me understand where I'm going wrong.
Let's consider an EventAggregator scenario, where ModuleA publishes and ModuleB subscribes. In my ModuleA MainWindowViewModel, I would have a class constructor like so:
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's Bootstrapper.cs, I would create my 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 it via Dependency Injection, registering my ViewModel with an IUnityContainer and then injecting my View as necessary. Furthermore, I keep seeing that I should actually be using an interface for my ViewModel (i.e. IMainWindowViewModel) to seperate concerns.
Could someone point me to a resource that might be able to clear the obvious confusion I'm having? I've read over MSDN's Prism QuickStarts, including Advanced MVVM Scenarios, but I don't understand how to contextualize the instructions.