MVVM - Bind directly to Model object exposed from VM or implement a separate property in VM to access the Model properties

Sorry if the title is confusing but I don't really know how to shorten my question. Anyway, here it goes.

I'm using WPF, Entity Framework and MVVM.

Currently, in my ViewModel, I have a property

public Model.Document Document {get;set;} //Model.Document is an EF Entity

Then, in XAML, I bind to

<TextBox Text={Binding Path=Document.Title}/>

Title is of course a Property on the Model.

Now following question came to my mind: To separate the Model from the View, wouldn't it be better if I added a property to the ViewModel like so

    public string Title
        get { return Document.Title; }
        set { Document.Title = value; }

and then bind like this:

<TextBox Text={Binding Path=Title}/>

Which way is recommended?


If you take a look at How Data Binding References are Resolved, you can see that there can be performance issues to consider when deciding how to expose the property you are going to bind to.

Does the Model.Document implement the INotifyPropertyChanged interface? If not, I would recommend adding a Title property to your view model and implement INotifyPropertyChanged on your view model such that when the Title is changed the PropertyChanged event is raised to notify the view.

Another approach would be to expose the Title on your view model as a DependencyProperty as the binding and render time is faster.

The latter is the better way to do it, at least IMO. I haven't really seen in done the other way. The viewmodel serves as a mediator between the view and model, so those two shouldn't really have any knowledge of each other.

Here's a simple example.

It really depends on your situation. In the truest sense, the View should only interact with the View Model. But both approaches above have their advantages.

By exposing the Document property, you don't have to duplicate all the properties that you need to access on it. So if you have numerous properties, or are using some sort of reflection (such as displaying Document in a PropertyGrid), then this way is probably better.

The latter approach gives you more control over the value exposed to the View. So you can change the title (i.e. remove extra spaces) before it's committed to the model or various other actions. You can even swap out of the Document, without the View being aware of it (or even caring).

You might have a look at the BookLibrary sample application of the WPF Application Framework (WAF). It uses the first approach and still follows the MVVM pattern (separation of concerns, unit testable).

This simplifies the ViewModels because the Binding uses the INotifyPropertyChanged implementation of the Entities. Otherwise, you have to synchronize changes manually between the ViewModels.

I don't see any problem with read-only bindings to model properties whose values don't change.

Those are two really important qualifications, though. Read/write binding to model properties is so fraught with potential trouble that rather than soberly analyze the situation to see if this is one of the cases that doesn't have any, I'd just implement a property in the view model and be done with it. And unless my model supports change notification, if I need it, which I probably do, I'm going to need to implement it in the view model.

Need Your Help

C# Putting the required DLLs somewhere other than the root of the output

c# visual-studio dll dependencies

I am using EmguCV for a project and when our program runs it needs some dlls like "cxcore.dll" etc. (or it throws runtime exceptions). At the moment, I put the files in the root of the output folder (

How to get the focused element with jQuery?

javascript jquery cursor element caret

Using jQuery, how can I get the input element that has the caret's (cursor's) focus?