ctodx

This Blog

News

Favorite Posts

Archives

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

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.

Published Dec 02 2016, 12:41 AM by
Filed under: , , , , , ,
Bookmark and Share

Comments

Noufal Aboobacker 1

In my openion we already have prism which is well tested and highly modular and above all open source and part of dot net foundation . Instead of reinventing the wheel the better approach would have been to embrace prism and make devexpress wpf more easy to work with this already established framework.

December 1, 2016 11:54 PM

John01

Who asked for this feature? No interested comment and zero fb likes, as of now.

December 2, 2016 3:41 AM

Petr Vojtechovsky 1

Hi Julian, is the winform platform supported?

December 2, 2016 7:02 AM

Norbert K.

Hi Julian! Could you please explain what are the advantages of your framework over PRISM?

December 2, 2016 12:50 PM

developer_paul

When I tested all the different MVVM frameworks I found that the devexpress MVVM was the best and most practical. I think if your software is simple it won't matter which you MVVM framework you use. But if your making complex software the open source solutions all seem to have problems and deficits.

December 3, 2016 3:07 PM

Noufal Aboobacker 1

@developer_paul, I complete agree with you about the fact that DevExpress MVVM is the best out there. I am using all the features of the MVVM framework quite seamlessly. But when it comes to Modular development, in my openion Prism is the best Period. So creating a new Injection framework was totally a waste of time. instead DevExpress should have provided more and more feature on the MVVM Framework itself.

December 3, 2016 9:34 PM

Alexander (DevExpress Support)

Hi guys,

Let me answer to your question by points.

1. Why MIF and not PRISM.

We seriously considered the possibility to support PRISM.

There is a problem with versioning: how to manage different PRISM versions and a version of our components? If we create a library with PRISM region adapters for our components, it should support all versions of our components and all versions of PRISM (at least, starting with DX version 16.2 and PRISM version 6.2).

This means that we should create a repository on GitHub with many branches - for every version of our components and the PRISM version. It will be hard to sort out all branches.

So, we decided to go another way - to create a solution that is qualitatively integrated with our control from the inside.

2. What are the advantages of your framework over PRISM?

Perhaps the main advantage is the application's state serialization. MIF provides the capability to restore previously opened documents and their layout.

3. Is the winform platform supported?

No, but we will consider it in the future. Prospects are very optimistic in the sense that there should be not serious problem in porting to WinForms.

December 4, 2016 10:49 PM

Noufal Aboobacker 1

Hi Alexander,

I understand your point. But there are lots of projects which are already using prism and being a wpf control vendor its a must that you should support prism because its part of Microsoft .net foundation. There is no problem if DevExpress has its own solutions but those who work with Prism should also have some better options.

I think you can still create those GitHub repositories and since it will be open sourced I am sure many will contribute so all the users will get best of both worlds.

Please consider this.

December 4, 2016 11:49 PM

Alexander (DevExpress Support)

Yes, this makes sense.

Let's see if we will support PRISM in the future.

December 5, 2016 12:11 AM

Marvin Kruse

We have plans to modularize an application we're currently working on in Q1 2017 (we're already using DX) - but with this approach we do not need write a lot of code ourselfs and just use MIF. Nice one!

December 22, 2016 4:14 AM

Fouss

More documentation are needed ....!

December 23, 2016 8:25 AM

Valdimar Halldorsson 1

I agree with Noufal, please create a Gitbub repo DX-Prism support as it's the framework I've used in the past and will be working with in the future.

December 23, 2016 4:20 PM

PeerlessProgrammer

MIF is a great addition to your MVVM framework. I feel like that's the piece that's been missing. REALLY need some good documentation on it though.

I understand why you are doing your own because you can have tighter integration with your controls and not everyone uses Prism. However, I would agree that if you could maintain an open source project for Prism adapters, etc. that would be great!

February 22, 2017 5:10 AM

About Julian Bucknall (DevExpress)

Julian is the Chief Technology Officer at Developer Express. You can reach him directly at julianb@devexpress.com. You can also follow him on Twitter with the ID JMBucknall.
LIVE CHAT

Chat is one of the many ways you can contact members of the DevExpress Team.
We are available Monday-Friday between 7:30am and 4:30pm Pacific Time.

If you need additional product information, write to us at info@devexpress.com or call us at +1 (818) 844-3383

FOLLOW US

DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, along with high-performance HTML JS Mobile Frameworks for developers targeting iOS, Android and Windows Phone. Whether using WPF, ASP.NET, WinForms, HTML5 or Windows 10, DevExpress tools help you build and deliver your best in the shortest time possible.

Copyright © 1998-2017 Developer Express Inc.
All trademarks or registered trademarks are property of their respective owners