Click here to Skip to main content
15,886,810 members
Articles / Desktop Programming / WPF

Blendable MVVM ViewModelLocator using MEF

Rate me:
Please Sign up or sign in to vote.
4.43/5 (6 votes)
6 Oct 2010CPOL3 min read 42.1K   1.5K   16   11
New take on the ViewModel locator using dynamics and ITypedList & TypeDescriptionProvider

Introduction

Recently I entered the wonderful world of MVVM. There is a lot to read on this subject (which I did) and there are several very good frameworks available. So I 'collected' handy bits and pieces from these frameworks in order to create my first MVVM application.

During this process, I found out that the ViewModel locator was debated a lot and that all the various solutions had their pros and cons (see: Nikhil Kothari Options for Hooking a View to its Model). Since the introduction of MEF in .NET Framework 4, a new possibility came into reach which was described by (johnpapa: Simple ViewModel Locator for MVVM: The Patients Have Left the Asylum).

Basically what it comes down to is how to you 'wire' your ViewModel to your view without specifying it in multiple places and with the preservation of designtime binding in Blend and VS2010.

Since code says more than an article, I included a simple project which demonstrates how it works.

DynamicObject

The backbone of my solution is the new DynamicObject which was introduced in .NET Framework 4. This object can be extended runtime, if you want more information on how these things work, you can find it here: A Multi-level C# 4.0 Dynamic Object. When a property is used from a DynamicObject, the TryGetMember is called with the name of the property which is requested. When a DataContext is specified, the property name can be specified like this:

C#
DataContext="{Binding Demo1, Source={StaticResource Locator}}"

Since my Locator is a DynamicObject, the TryGetMember is called with the parameter name "Demo1". Initially, I then created the corresponding ViewModel based on this name like this:

C#
public override bool TryGetMember(GetMemberBinder binder, out object result) 
{ 
    string name = binder.Name; 
    if (!dictionary.TryGetValue(name, out result)) 
    { 
        switch (name) 
        { 
            case "Demo1": 
                dictionary[name] = (result = new Demo1ViewModel()); 
                return true; 
        } 
        return false; 
    } 
    return true; 
} 

But when I read the article by JohnPapa, I decided to add his MEF implementation as well! Also a lot of articles on this subject can be found here: .NET 4.0 MEF FAQ. In order to find the ViewModel, an ExportViewModel attribute is introduced:

C#
[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
public class ExportViewModel : ExportAttribute
{
	public string Name { get; private set; }
	public ExportViewModel(string name, bool isStatic)
		: base("ViewModel")
	{
		Name = name;
	}
}

Which can be used on a ViewModel like this:

C#
[ExportViewModel("Demo1", false)]
class Demo1ViewModel : ViewModel
{
	public Demo1ViewModel()
	{
		EnDisable = new RelayCommand(() =>
		{
			isEnabled = !isEnabled;
			Action.RaiseCanExecuteChanged();
		});

		Action = new RelayCommand(() => 
			MessageBox.Show("demo1 test"), () => isEnabled);
	}

	private bool isEnabled = false;
	public RelayCommand EnDisable { get; set; }
	public RelayCommand Action { get; set; } 
}

Basically what happens here is that with the help of MEF, the ViewModel gets a name "Demo1" and can be found by using a CompositionContainer like this:

C#
[ImportMany("ViewModel", AllowRecomposition = true)]
private IEnumerable<Lazy<object, IViewModelMetadata>> ViewModels { get; set; }
 

var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(typeof(ViewModelLocator).Assembly));
CompositionContainer _container = new CompositionContainer(catalog);
var compositionContainer = new CompositionContainer(catalog);
compositionContainer.ComposeParts(this);

The big advantage of this approach is that there is no maintenance of the link between the View and the ViewModel other than this, the locator is generic for all the ViewModels in your project! The TryGetMember will then look like:

C#
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
	string name = binder.Name;
	if (!dictionary.TryGetValue(name, out result))
	try
	{
		if (ViewModels == null)
		{
			var catalog = new AggregateCatalog();
			catalog.Catalogs.Add(new AssemblyCatalog
				(typeof(ViewModelLocator).Assembly));
			CompositionContainer _container = 
					new CompositionContainer(catalog);
			var compositionContainer = 
					new CompositionContainer(catalog);
			compositionContainer.ComposeParts(this);
		}
		dictionary[binder.Name] = (result = ViewModels.Single
					(v => v.Metadata.Name.Equals(name)).Value);
		return result != null;
	}	
	catch (Exception ex)
	{
		Console.WriteLine(ex);
	}
	return true;
}

Designtime Binding

There are a lot of Locators available which can also handle the same functionality like my locator but most of those have problems with blendability where the properties of the ViewModel do not show up in Blend or VS2010. Since I like that a lot, I tried to add some stuff I had used back in the days where all the database classes we had were DataSet and DataTable. The ITypedList is used to specify which properties are 'not really there' - see: Implementing ITypedList for Virtual Properties. Together with the TypeDescriptionProvider which can be used to support dynamic run-time properties, all the ViewModels which are created this way can be bindable.

blend1.png

Points of Interest

I learned a lot by reading all the various articles about MVVM. I hope this article can help others.

History

  • 6th October, 2010: First version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionMEF 2 for .NET4.5 & Win Store apps Pin
Adamaka5-Feb-14 5:56
Adamaka5-Feb-14 5:56 
QuestionIs this still a good approach for binding view models to views ? Pin
Derek_Ewing27-Dec-12 10:55
Derek_Ewing27-Dec-12 10:55 
BugAnother link does not work Pin
Clifford Nelson4-Dec-12 12:47
Clifford Nelson4-Dec-12 12:47 
BugLink no longer works Pin
Clifford Nelson4-Dec-12 12:46
Clifford Nelson4-Dec-12 12:46 
QuestionViewModel is static Pin
Member 784113521-Sep-12 4:26
Member 784113521-Sep-12 4:26 
i used this nice ViewModelLocator in my application.
now i see a VERY big problem... Frown | :(
please help!!!

the problem is:
i created a user control - view, and i use it a few times, but the view model loactor dose not
create a new insatnce of view model each time i use the usercontrol.
the view model is static?!?

i saw that you alredy added a property in the exported attribute "is static".
but you never use it in the view model.

how do i make it works?
that each view will have a new instance of view model??

i would very appriciate to get an answer as soon as possible.
before my boss will kill me Sniff | :^)

thanks,
El
Bugmulti instances of views Pin
Member 784113530-Aug-12 1:25
Member 784113530-Aug-12 1:25 
QuestionFragile [modified] Pin
kman22-Jul-11 5:19
kman22-Jul-11 5:19 
AnswerRe: Fragile Pin
kman22-Jul-11 5:50
kman22-Jul-11 5:50 
GeneralMy vote of 5 Pin
aaltaya11-Mar-11 4:22
aaltaya11-Mar-11 4:22 
QuestionViewModel parameters? Pin
dvoxsis2-Dec-10 23:36
dvoxsis2-Dec-10 23:36 
GeneralMy vote of 4 Pin
tec-goblin11-Oct-10 11:41
tec-goblin11-Oct-10 11:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.