MVVM-style applications with nRoute – by Anton


As you already know (or just beginning to explore) MVVM is a way to separate data structures from UI. The basic idea of MVVM is that we would like to use data binding to connect our View to our Model, but data binding is often tricky because our model doesn’t have the right properties. So we create a View Model that is perfectly set up to have just the right properties for our View to bind to, and gets its actual data from the Model. You can get full overview of this pattern in official Microsoft MSDN Magazine.

Looking through the description of each part you can see that the Model represents the business domain which includes the model classes used (Customer, Order, etc.), data access code and business rules. In general you can think of the Model as representing the entities that live on the server as well as the objects that are responsible for interacting with the data store your application uses and filling entities with data. While some people feel that the Model represents only the model classes (classes like Customer, Order, etc.) used in the application I personally think of it more broadly and include data access code and business rules in the Model definition. Silverlight applications will call into the Model code through services written using WCF, ASMX, REST or even custom solutions.

The View in MVVM represents the Silverlight screens that you build. This includes the XAML files and the code-beside files that are responsible for showing data to end users. The View’s responsibilities include displaying data and collecting data from end users. A given View isn’t responsible for retrieving data, performing any business rules or validating data.

The ViewModel acts as the middle-man between the View and the Model. It’s responsible for aggregating and storing data that will be bound to a View. For example, a ViewModel may contain a List property and a Listproperty that may be bound to two ComboBox controls in a View. The ViewModel will retrieve the values held by these two properties from the Model. By using a ViewModel the View doesn’t have to worry about retrieving data and knows nothing about where data comes from.

A lot of information about the MVVM model is in the Internet and there sure a huge projects count with sources.

But let’s get back to the nRoute.

Technically speaking nRoute is a composite application framework that targets Silverlight, Windows Presentation Foundation (WP7), and Windows Phone 7 (WP7). However, from a more practical/developmental point of view, the idea here is to break your application into various independent constructs and bring them together at runtime as one seamless whole.

The idea to decompose and recompose your application into various parts is rooted in the concept of separation of concerns – which Wikipedia describes as “the process of separating a computer program into distinct features that overlap in functionality as little as possible”. Obviously it is what we’ve saw at the begining. With nRoute, we bring alive the separation of concerns concept in client-side applications by means of various constructs, such as:

  • Modules
  • Services
  • ViewServices
  • View-ViewModels
  • Navigable Views and Resources
  • Packaged/Externalized Resources
  • Messaging Channels

nRoute is now powered by NDepend. That will increase implementation of new features and more testing of current releases. In documentation area of the nRoute we can see that only part written methods are described, but existing ones are very interesting.

Just take a look at the Inversion of Control and Dependency Injection.

public interface IMessageService
{
void ShowMessage(string message);
}
//The interesting part
[MapResource(typeof(IMessageService))]
public class MessageBoxService : IMessageService
{
public void ShowMessage(string message)
{
MessageBox.Show(message);
}
}

In this case, we are mapping the MessageBoxService as being a resource of type IMessageService – this way, we can now look for IMessageService and nRoute will substitute it (at runtime) with the mapped implementation.

Also, this frame work is popular with it’s Navigation Contatiners, which helps to Navigate to an Url, Take an Action and use an ICommand interface.

To simplify the development, there are nRoute Templates for Visual Studio.

Official website: nRoute