17

I have an SQL DB and am implementing a WPF UI to update it. If I use EF5 to generate the classes from the DB, how can I implement INotifyPropertyChanged on the generated classes and properties so I can easily bind to them with the UI? Is there an easy way to achieve this?

Thanks

Steve
  • 1,584
  • 2
  • 18
  • 32

5 Answers5

14

If you follow the recommended MVVM pattern for WPF, you can treat your generated classes as the Model, and then write ViewModel wrappers that implement INotifyPropertyChanged. The ViewModel classes would access your DB classes and expose properties that you can bind to your UI in XAML.

As noted in your comment, this can result in a lot of work writing boilerplate code, but there are some ways to deal with that. See this question for some ideas.

While more work at first, the MVVM pattern can definitely pay off in the long run if you ever need to do any intermediate formatting or processing, or if you need to change your database classes without affecting the UI.

Community
  • 1
  • 1
WildCrustacean
  • 5,896
  • 2
  • 31
  • 42
  • 1
    For example if I had generated `class Asset { public int id {get;set;} }` I would write `class AssetWrap : Asset, INotifyPropertyChanged { public int idNotify { get { return id; } set{ id=value; NotifyPropertyChanged();} }}` If so, that seems _very_ time-consuming. – Steve Jan 03 '13 at 03:01
  • 2
    Usually the ViewModel doesn't inherit from the Model, but instead has a field with the Model object, so something like `class AssetWrap : INotifyPropertyChanged { private Asset _asset; public int idNotify { get { return _asset.id; } set{ _asset.id=value; NotifyPropertyChanged();} }` (this isn't quite complete). And yes, sometimes you end up with a lot of tedious boilerplate with the MVVM pattern, but it does pay off if you need to do some intermediate processing or change how the model is displayed. I put a link to another question that has some ideas for reducing this workload. – WildCrustacean Jan 03 '13 at 03:07
  • I can see the benefit of MVVM, but they really need some MVVM generators don't they, as a lot of it is so often following the same template. – Steve Jan 03 '13 at 04:08
  • 1
    I know there are some MVVM frameworks out there, and some one-off generators (like [this](http://viewmodelgenerator.codeplex.com/)) but I can't really speak to their quality. – WildCrustacean Jan 03 '13 at 04:20
  • 1
    A problem with the standard MVVM approach is that if the model is updated from the VM, the UI won't get updated because it has no knowledge of the model changing. – jamesSampica Sep 18 '13 at 04:04
  • agreed with shoe, it's a pain to inherit from model to create a new viewmodel. – user1841243 Jan 08 '14 at 15:45
9

There's a NuGet package called PropertyChanged.Fody that makes adding INotifyPropertyChanged to a classes properties really simple. Once you've installed the package just add the [ImplementPropertyChanged] attribute to any class or partial class and the package will add INotifyPropertyChanged for you.

Here's a simple example of it's use;

using PropertyChanged;

[ImplementPropertyChanged]
public partial class Order
{
}

See GitHub for more information.

mark_h
  • 5,233
  • 4
  • 36
  • 52
  • 4
    Update: `ImplementPropertyChanged` has been superseded by `AddINotifyPropertyChangedInterface` – Midas Jun 03 '20 at 09:30
6

I needed to do the same recently but with Winforms. If you don't want to follow the MVVM pattern as suggested by bde you can modify the t4 template to implement INotifyPropertyChanged on your generated entities.

This answer helped me: https://stackoverflow.com/a/12192358/1914530

Community
  • 1
  • 1
2

Make all your EF generated classes inherit from a class which implements the INotifyPropertyChanged interface (using the non-generated partial classes you probably already defined). Add a method to this base class, which raises the PropertyChanged event with an empty string as PropertyName. Then, every time you call this method on an EF generated class instance, all its modified properties will be refreshed in your WPF UI.

Yet Another Code Maker
  • 1,625
  • 1
  • 10
  • 5
1

You can edit the EF template (.tt file) to generate the propertyChanged stuff on your properties, or (a bit risky:)) to edit the generated classes. The last is a bit risky, because if you regenerate the model, all changes will be lost. So maybe the variant with the wrapper classes or the template editing (a bit hard :S) are the best.