DevExpress MVVM for WPF - New Module Injection Framework Coming Soon in v16.2

ctodx
02 December 2016

Wait, what? What’s a Module Injection Framework – or MIF – and how is it used? The mile-high overview is that a MIF makes it easier to develop, test, maintain, and deploy applications built with loosely-coupled modules. Such a modular application is one that is divided up into a set of functional units, which are independent from each other. Although viewed as separate, these modules can, if needed, communicate with each other through well-defined contracts that you define. This separation of concerns means that modules can be developed and tested independently of each other.

1. Common Concepts

Using MIF terminology, a module is a set of Views and ViewModels (which can in turn contain submodules) that are injected into regions. A region is merely a placeholder in the application's UI.

MVVM MIF regions and modules

Using the DevExpress MIF, you can create modules and register them via the ModuleManager:

ModuleManager.Register(
    regionName: "RegionA", 
    module: new Module(
        key: "Module1", 
        viewModelFactory: () => new Module1ViewModel(), 
        viewType: typeof(Module1View)
    )
);

A region is a control that is marked by an attached property:

<TabControl dxmvvm:UIRegion.Region="RegionA" .../>

Modules are injected to regions when you call the Inject() method:

ModuleManager.Inject(regionName: "RegionA", key: "Module1");

2. Navigation

You can perform navigation tasks anywhere/anywhen in your application, only knowing the key of the module and the name of the target region:

ModuleManager.Navigate(regionName: "RegionA", key: "Module1")

You can also define navigation logic globally. For example, suppose you have two regions in your app: a navigation region and a document region, such as in the image previously shown. An end-user uses the navigation control in its region to navigate to a document, and this is then shown in the document region (which could be a TabControl, for example).

With MIF, you can say:

“When a module in the navigation region becomes active – activate the corresponding module in the document region.”

And also:

“When a document in the document region becomes active – activate the corresponding module in the navigation region.”

ModuleManager.GetEvents(regionName: "NavigationRegion").Navigation += OnNavigationRegionNavigation;

void OnNavigationRegionNavigation(object sender, NavigationEventArgs e) {
    ModuleManager.Navigate(regionName: "DocumentsRegion", key: ((MyNavigationViewModel)e.ViewModel).DocumentKey);
}

//...

ModuleManager.GetEvents(regionName: "DocumentsRegion").Navigation += OnDocumentsRegionNavigation;

void OnDocumentsRegionNavigation(object sender, NavigationEventArgs e) {
    ModuleManager.Navigate(regionName: "NavigationRegion", key: ((MyDocumentViewModel)e.ViewModel).NavigationKey);
}

3. Tests

MIF makes it really easy for you to write unit tests for your navigation logic.

ModuleManager.Navigate(regionName: "NavigationRegion", key: "NavigationItem1");
Assert.AreEqual("Document1", ModuleManager.GetRegion(regionName: "DocumentsRegion").SelectedKey);

ModuleManager.Navigate(regionName: "DocumentsRegion", key: "Document2");
Assert.AreEqual("NavigationItem2", ModuleManager.GetRegion(regionName: "NavigationRegion").SelectedKey);

4. Save and Restore Application State

Like it or not, our users expect our applications to save its state and to restore it when restarted. There are several types of state an application is expected to manage in this manner:

  • The state of visual controls. Examples are, the selected grouping and column order of a GridControl, or the position of panels in a DockLayoutManager.
  • Dynamically injected modules, which can be automatically injected on startup. One example of this are the open tabs in a browser.
  • The state of particular View Models.

The DevExpress MIF refers to the first type of state as visual state. The second and third types are grouped into logical state.

The ModuleManager.Save() method allows you to query the current application state.

void Save(string regionName, out string logicalState, out string visualState); 

The ModuleManager.Restore() method restores the saved application state when called.

bool Restore(string logicalState, string visualState); 

5. MVVM MIF Template

To get you started, we’ve added a new project template in our Template Gallery. It generates a simple MIF application.

MVVM MIF app template


Are you using our MVVM with our WPF controls? What do you think of this new feature? Do you think you’ll be trying out this new MIF? Please let us know either below or by emailing us at info@devexpress.com.

Free DevExpress Products - Get Your Copy Today

The following free DevExpress product offers remain available. Should you have any questions about the free offers below, please submit a ticket via the DevExpress Support Center at your convenience. We'll be happy to follow-up.
No Comments

Please login or register to post comments.