views:

410

answers:

2

Hello, I've recently started develping an application using WPF and I'cant really wrap my mind around following thing:

I have a Domain Model of my application which is simple POCO objects serialized to/from harddisk. Then I have the WPF application and I'd like to bind it to various parts of the model. I need to be able to notify UI of changes to underlying model (eg. implement INotifyPropertyChanged) BUT I want to do that without interfeering with my model (read without modifying current implementation of the model). How can I implement changes notification other than modifying the model? Reason why I want to do that is that I share the model among multiple projects, only one being WPF, and I don't want to add unncecessary code to the model. One thing that came to my mind was to create "copy" of the model (with INotifyPropertyChanges and BindingLists etc.) but that seems to be hard to maintainn... Thanks in advance.

Ondrej

A: 

I see two possible solutions here:

  1. Use a separate model for WPF screens only (MVVM pattern). This will require maintaining two different models, also be prepare for lots of mapping code.
  2. Use PostSharp to "enhance" your model with all necessary boilerplate code. Here you can find example of automatic INotifyPropertyChanged implementation. Remember that introducing PostSharp to the project is an important decision, so I suggest getting familiar with it first.
bbmud
+2  A: 

Check this out MVVM

Download its source code see the hierarchy.

Basically you still keep simple POCO objects as your models. Then you create a ViewModel around the model like this:

public class CustomerViewModel : INotifyPropertyChanged
{
        readonly Customer _customer;
        public CustomerViewModel(Customer customer)
        {
            _customer = customer;
        }

        public string FirstName
        {
            get { return _customer.FirstName; }
            set
            {
                if (value == _customer.FirstName)
                    return;

                _customer.FirstName = value;

                OnPropertyChanged("FirstName");
            }
        }
        ...
}
Kai Wang