The View Model

Context

The DevForce Application Framework integrates MEF with Caliburn Micro. This allows for wiring the ViewModels and its dependencies through dependency-injection for loose-coupling of all the parts that make up the application, ease of extending an application and ease of maintenance. At this point we assume that you are somewhat familiar with MVVM and Caliburn Micro. For more information on Caliburn Micro, please visit the Caliburn Micro Documentation.

Basic ViewModel

Caliburn Micro offers a number of base classes to build ViewModels with certain attributes and behavior. In it’s most basic form, though, a ViewModel is simply a POCO.

using System.ComponentModel.Composition;
using Caliburn.Micro;

[Export]
public class BasicViewModel : Screen
{
    // Add Data Properties and Actions
}

In the above example we are using the Caliburn Micro Screen class as the base. The only other thing is the required [Export] attribute in order to make the ViewModel available to MEF and allow MEF to create instances as required by the application as well as injecting the necessary dependencies into it. The above example doesn’t have any dependencies.

ViewModel with Dependencies

The most common dependency of a ViewModel is the Repository, so that we can retrieve and save data. Just as the ViewModels, the Repositories are exported through MEF. Most often, though, the Repositories are exported not by the concrete type, but by an interface, so that they can be substituted for unit testing for example.

using System.ComponentModel.Composition;
using Caliburn.Micro;

[Export]
public class BasicViewModel : Screen
{
    private readonly ICustomerRepository _repository;

    [ImportingConstructor]
    public BasicViewModel(ICustomerRepository repository)
    {
        _repository = repository;
    }

    // Add Data Properties and Actions
}

ViewModel Composition

More complex ViewModels are often composed together of multiple smaller ViewModels. This can be achieved through the same dependency injection mechanisms. Communication between the composed ViewModels is often done through messaging with the help of an event aggregator. Caliburn Micro offers such an event aggregator and the DevForce Application Framework makes it available through MEF.

using System.ComponentModel.Composition;
using Caliburn.Micro;

public class DoSomethingMessage
{
}

[Export]
public class MenuViewModel : Screen
{
    private readonly IEventAggregator _eventAggregator;

    [ImportingConstructor]
    public MenuViewModel(IEventAggregator eventAggregator)
    {
        _eventAggregator = eventAggregator;
    }

    public void MenuItemClicked()
    {
        _eventAggregator.Publish(new DoSomethingMessage());
    }
}

[Export]
public class WorkspaceViewModel : Screen, IHandle<DoSomethingMessage>
{
    [ImportingConstructor]
    public WorkspaceViewModel(IEventAggregator eventAggregator)
    {
        eventAggregator.Subscribe(this);
    }

    public void Handle(DoSomethingMessage message)
    {
        // Do something
    }
}

[Export]
public class Shell : Screen
{
    [ImportingConstructor]
    public Shell(MenuViewModel menuViewModel, WorkspaceViewModel workspaceViewModel)
    {
        MenuViewModel = menuViewModel;
        WorkspaceViewModel = workspaceViewModel;
    }

    public IScreen MenuViewModel { get; private set; }
    public IScreen WorkspaceViewModel { get; private set; }

    // Add Data Properties and Actions
}

Last edited Oct 23, 2011 at 6:40 AM by marcelgood, version 6

Comments

No comments yet.