MVVM - Bind directly to the Model object accessible by the VM, or create a distinct property in the VM to access the Model properties.

entity-framework mvvm wpf


I apologize if the title is unclear; I'm not sure how to make my inquiry more concise. Anyway, let's get started.

I make use of MVVM, Entity Framework, and WPF.

I currently have a property in my ViewModel.

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

Afterward, I bind to in XAML.

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

Of course, Title is a Property on the Model.

Now, the following thought crossed my mind: Wouldn't it be better if I added a property to the ViewModel like so to isolate the Model from the View?

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

and then bind as follows:

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

Which approach is suggested?

4/27/2011 3:00:10 PM

Accepted Answer

If you look at How to Resolve Data Binding References, you can see that choosing how to expose the property you're going to bind to might include performance considerations.

The INotifyPropertyChanged interface is implemented by the Model.Document, right? If not, I would advise adding a Title property to your view model and implementing INotifyPropertyChanged so that the PropertyChanged event is triggered to alert the view whenever the Title is modified.

Another strategy would be to expose the Title as a DependencyProperty on your view model since binding and rendering take less time.

4/27/2011 3:24:31 PM

Popular Answer

Consider looking at the Application Framework for WPF (WAF) example application, which uses the BookLibrary. It employs the first strategy while continuing to utilize the MVVM paradigm (separation of concerns, unit testable).

Because the Binding utilizes the INotifyPropertyChanged implementation of the Entities, the ViewModels are made simpler. If not, you must manually synchronize changes between the ViewModels.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow