3
votes

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.

1

1 Answers

0
votes
  1. 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.

    Two diffent mechanisms have been mentioned above:

    Service Locator vs Dependency Injection

    The fundamental choice is between Service Locator and Dependency Injection. The first point is that both implementations provide the fundamental decoupling that's missing in the naive example - in both cases application code is independent of the concrete implementation of the service interface. The important difference between the two patterns is about how that implementation is provided to the application class. With service locator the application class asks for it explicitly by a message to the locator. With injection there is no explicit request, the service appears in the application class - hence the inversion of control.

    -- Inversion of Control Containers and the Dependency Injection pattern, Martin Fowler.

    Conclusion. There must be an architectural decision: "Which mechanism to use?".

  2. Furthermore, I keep seeing that I should actually be using an interface for my ViewModel (i.e. IMainWindowViewModel) to separate concerns.

    This is how the Dependency Inversion Principle is applied.

    The Dependency Inversion Principle:

    A. High level modules should not depend upon low level modules. Both should depend upon abstraction.

    B. Abstractions should not depend upon details. Details should depend upon abstractions.

    -- The Dependency Inversion Principle, Robert C. Martin, 1996.

    Where "Details" are the concrete types, "Abstractions" are the interfaces.

Please refer to the cited references for more information.