First experiments using MEF, MVVM and Silverlight 4 Beta
Note – this is a multi part post:
MEF (Managed Extensibility Framework) is a new library available in Silverlight 4 Beta which permits to build applications that can be incrementally extended in a declarative way using three simple concepts (read this post by Glenn Block for more infos):
-
Export an object;
-
Import it;
-
Compose it.
Mixing MEF and commanding, Silverlight has now a great support for structuring projects using a ViewModel approach, so I decided to use these new characteristics to build a simple solution.
Given an existing project, I added a ViewModel class implementing INotifyPropertyChanged:
[Export("MainPageViewModel"))]
public class MainPageViewModel : ViewModelBase
{
public MainPageViewModel()
{
aViewModelProperty = "This is the content of a ViewModel property";
aSampleCommand = new AViewCommand();
}
public string aViewModelProperty { get; set; }
public ICommand aSampleCommand {get; set;}
}
The problem I’ve solved using MEF is the binding of the DataContext property of the UserControl to a ViewModel class instance, so I’ve exported it using the MEF [Export] attribute.
The [Import] attribute and the PartInitializer.SatisfyImports(this); are then used in the code behind of the xaml view in order to import and compose the ViewModel instance with the DataContext:
public MainPage()
{
InitializeComponent();
PartInitializer.SatisfyImports(this);
this.DataContext = mainPageViewModel;
}
[Import("MainPageViewModel")]
public object mainPageViewModel { get; set; }
And this is the result:
As usually the source code is available for download here.
Happy Silverlighting!




December 2nd, 2009 at 00:38
[...] This post was mentioned on Twitter by Davide Zordan, Arun Mahendrakar. Arun Mahendrakar said: RT @DavideZordan: blogged: First experiments using MEF, MVVM and Silverlight 4 Beta – http://bit.ly/5mPXJI #silverlight #mef #mvvm #in #fb [...]
December 2nd, 2009 at 00:49
Great article mate. Thanks
December 2nd, 2009 at 16:31
Can you elaborate on why MEF is needed to to data binding? I thought one of the new features of the BING map control was data bound info. Which I saw the PDC demo using collections…
December 2nd, 2009 at 16:57
Interesting. Can you elaborate on what this buys you over “newing up” an instance in the View’s constructor?
December 3rd, 2009 at 00:11
[...] This post was Twitted by brian_henderson [...]
December 3rd, 2009 at 12:32
@DrYSG @David
I’ve tried this approach to marry the view with the VW using a declarative style permitted by MEF (example: multiple ViewModels for a same View)
December 4th, 2009 at 06:47
Davide nice post. Just out of curiousity, why are you using a string import for the VM as opposed to an actual type.
Are you expecting to reuse the VMs across multiple types that have no common interface? In general we recommend using types as the contract rather than loose strings with strings being the exception to the rule.
Why not do this, which uses the VM type as the export/import?
[Export] //will export MainPageViewModel
public class MainPageViewModel {
…
}
public class MainPage {
[Import]
public MainPageViewModel mainPageViewModel { get; set; }
}
December 4th, 2009 at 06:49
Note, you can also do this if you had the need, though ViewModels do not usually need to be mocked. However just for illustration…..
[Export] //will export MainPageViewModel
public class MainPageViewModel : IMainPageViewModel {
…
}
public class MainPage {
[Import]
public IMainPageViewModel mainPageViewModel { get; set; }
}
December 4th, 2009 at 07:44
Sorry my above comment was incorrect, to export IMainPageViewModel, it should be [Export(typeof(IMainPageViewModel))]
Oops
Glenn
December 4th, 2009 at 11:59
Hi Glenn, thanks for the suggestions! Yes, my idea was to marry the view with the viewmodel using a declarative style without any “using” clause in the view, so I had to define the VM property as “object” type.
I agree with you for the usage of types in contracts instead of strings, in this case however the “object” type declaration has forced me to use them.
I’ll further investigate this approach, thanks again!!
December 5th, 2009 at 14:44
[...] the last post, I’ve used the new MEF framework available in Silverlight 4 Beta to build a simple MVVM [...]
December 5th, 2009 at 21:51
[...] experiments using MEF, MVVM and Silverlight 4 [...]
December 8th, 2009 at 16:36
[...] This post was Twitted by SilverlightNews [...]
December 11th, 2009 at 01:25
[...] First experiments using MEF, MVVM and Silverlight 4 Beta [...]
December 13th, 2009 at 21:48
[...] First experiments using MEF, MVVM and Silverlight 4 Beta [...]
January 7th, 2010 at 01:14
[...] First experiments using MEF, MVVM and Silverlight 4 Beta [...]
January 7th, 2010 at 01:17
[...] First experiments using MEF, MVVM and Silverlight 4 Beta [...]
January 20th, 2010 at 13:29
Hi, MEF looks nice. Is it possible to put extensions in seperate dll’s?
e.g.:
1. Put the modelview in a seperate dll, which is then easy to update.
2. Using seperate dll’s, on can let MEF import new features. This is out of the modelview context, just generaly speaking.
thanks,
Atam
January 20th, 2010 at 23:21
Hi Atam,
these posts can help you:
- http://channel9.msdn.com/posts/mtaulty/MEF–Silverlight-4-Beta-Part-5-the-PackageCatalog/
- http://codebetter.com/blogs/glenn.block/archive/2010/01/03/mef-and-prism-exploration-mef-module-loading.aspx
- http://codebetter.com/blogs/glenn.block/archive/2009/12/15/building-hello-mef-part-iii-xap-partitioning-with-the-host-s-permission-and-the-sweetness-of-recomposition.aspx
HTH.