Sony Arouje

a programmer's log

Scalable Silverlight app using MEF and Caliburn Micro

leave a comment »

In this post I am going to give a brief insight of my experiment with MEF and Caliburn Micro. I am always a big fan of plug-in application model. When I go through the MEF I was so impressed and want to try as it is similar to my favorite area (plugin model). As usual I used caliburn micro here as well.

The app I developed is just for demo purpose and there is no business value. In my app I display couple of user controls and all are loaded by MEF. The app also demonstrate the powerful event handling provided by Caliburn micro. With the help of MEF and Caliburn micro we can build a very powerful scalable silverlight app.

Below is the screen shot of my app

image 

As you can see my UI is not very fancy, because my intention was to try how MEF will work in conjunction with caliburn micro. The first two expanders wont do any job other than displaying some thing to user. The Display Text and Font selection will do some demonstration of Event publishing. The user selected Font properties will get applied to the Text in the Display Text expander.

I implemented an interface called IView to make all the view models satisfy MEF exports. Below is the interface.

namespace SilverlightClassLib.Common
{
    [InheritedExport]
    public interface IView
    {

    }
}

 

A blank interface decorated by and Attribute called InheritedExport. Which ever the class implements this interface will be considered for Exporting. You can get more details about this approach from Brad Abrams blog. In this demo all my viewmodels implemented IView interface. MEF will gather all my Exported ViewModels and Caliburn micro will intern load the View for the view model.

I compose my UI in MainPageViewModel.cs as shown below. To compose the ui you have to add reference to assembly System.ComponentModel.Composition and add using’s to System.ComponentModel.Composition.Hosting and System.ComponentModel.Composition

namespace MEFCaliburnMicroTracer.ViewModels
{
    public class MainPageViewModel:PropertyChangedBase
    {
        public MainPageViewModel()
        {
            var catalog = new PackageCatalog();
            catalog.AddPackage(Package.Current);
            var container = new CompositionContainer(catalog);
            container.ComposeParts(this);
        }

        [ImportMany(AllowRecomposition=true)]
        public ObservableCollection<IView> importedViews { get; set; }
    }
}

In the constructor we load the current xap file to MEF container. The exported part will get added to property which is decorated with ImportMany attribute. In this case it’s ImportedViews property. I set AllowRecomposition to true to load exported parts if we do any async loading of xap’s. In this case there is no parameter required as we are not doing any async loading.

A Listbox In the MainPageView is binded to importedViews property. This listbox is responsible of displaying all my Views.

The greatest advantage of MEF is, we can add or remove new functionality without affecting the existing modules. And the powerful EventAggregator in Caliburn micro will help to enable loosely coupled interaction between the modules.

Download Source

Written by Sony Arouje

October 20, 2010 at 7:38 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: