2

I am using MVVM pattern in WPF to create a program.

I was peacefully implementing INPC on my model classes .... until a few days ago when I decided to use EntityFramework. Now I dont know how I can tell the EntityFramework to implement INPC on the generated entity classes and raise the property changed event every time a property changes.

Other questions such as this simply suggest to implement INPC on ViewModel, but I really want the Model classes to implement INPC to reduce the amount of work.

I am using Model-first approach to generate my model.

Community
  • 1
  • 1
Joe Slater
  • 2,483
  • 6
  • 32
  • 49
  • You can change the generated T4 Template for the model to implement INPC. This is quite a long process as there is a lot of things to change (generate the backing fields and Getter / Setter, mainly). – Simon Belanger Jun 29 '13 at 19:45
  • @SimonBelanger Can you post a link to a walkthrough, tutorial or a example which can show me how to do this using T4 Template? Thx very much for helping. – Joe Slater Jun 29 '13 at 19:51
  • This seems like a good start: http://pjgcreations.blogspot.ca/2012/12/enabling-mvvm-inotifiypropertychanged.html – Simon Belanger Jun 29 '13 at 19:54
  • Oh great! Thx. I will try and report back. – Joe Slater Jun 29 '13 at 19:55

2 Answers2

2

The recent release of Microsoft Prism's "Unity Container" addresses this question straight on with its 'Behaviour Interception' feature. Using the feature can make any class BEHAVE as though it inherits from INotifyPropertyChanged.

The most compelling attraction to the feature is that your EF classes can remain blissfully unaware of what's happening and do not need manual coding modifications in order to work. As you might expect, there's a performance hit which you have to measure against your requirements.

You can download Prism here. Prism

There's a walk-through that you SHOULD be able to cut-and-paste into your code here. Interception Behaviour You can wire up the code from this walkthrough in your app's start up phase, and Unity will handle the rest...

Having some code all ready is a great advantage because it can get hairy if you haven't worked with Type Injection before, so I recommend the walkthrough.

Gayot Fow
  • 8,710
  • 1
  • 35
  • 48
  • Either a class implements `INotifyPropertyChanged`, or it doesn't. There is no way a class can behave "as though" it implements `INotifyPropertyChanged` without actually implementing it, because other libraries test specifically for that interface using `is`/`as`. But if I'm reading it correctly, classes get created dynamically that really do implement `INotifyPropertyChanged`. –  Jun 29 '13 at 23:32
  • When a given class has been wired up, other classes that do interface testing using "is" or "as" will get a "True" response. Otherwise you should contact the Unity team and let them know that there's "no way a class can behave as though it..." because they are saying otherwise. – Gayot Fow Jun 29 '13 at 23:52
  • "without actually implementing it". Like I said, it seems like types get created dynamically that don't just behave as if they implement the interface, they *do* implement the interface. –  Jun 29 '13 at 23:54
  • What word would you have us use that gives semantic elevation to the one currently used, which is 'behavour'? – Gayot Fow Jun 30 '13 at 00:08
  • I don't think hvd has an issue with the word "behave". It is with the phrase "as though". With Prism you define an interceptor - such as the NotifyPropertyChangedBehavior class described in your link. Then you use the unity container to retrieve the Model class. It then creates a proxy object that wraps or inherits the original object AND implements the interface. – Colin Jul 01 '13 at 08:56
  • Why not provide that as an answer? – Gayot Fow Jul 01 '13 at 09:24
2

You can do this with a few lines of code using a NuGet package called PropertyChanged.Fody. The documentation is on GitHub. See my CodeProject tip "Adding INotifyPropertyChanged to Entity Framework Classes".

markltx
  • 689
  • 8
  • 16