0

I'm refactoring and redesigning the domain objects of my application which uses MVVM to some extent. Is there anything that speaks against making all Domain objects (POCOs) inherit from INotifyPropertyChanged, so anyone can observe the objects as they wish.

In combination with https://stackoverflow.com/a/1316566/448357 this does not even have to be very ugly.

On the other hand, what about polluting my domain object with stuff that might not be required at all, because there will be a separate View-Model anyway? Margabit points out: UI Model != Domain Model

Community
  • 1
  • 1
Alexander Pacha
  • 9,187
  • 3
  • 68
  • 108
  • You know what's polluting? Having domain/UI models that you constantly have to switch back/forth from. I've seen this, and it isn't pretty. Nothing wrong with creating a base class that implements INPC and whatever else is convenient for your models. Your belly button, it doesn't require you to stare at it so much. Move on. –  Jul 10 '14 at 14:00

3 Answers3

1

IMO, Domain objects shouldn't implement INotifyPropertyChanged. The one who should be implementing it is your ViewModel.

The reasons for that is:

  1. You would probably mostly need to raise a PropertyChanged event inside your viewmodel which holds your POCOs
  2. You would be implementing it only once.
  3. If your POCO wants to raise an event and notify that something has occured inside it, im not sure PropertyChanged would be the most meaningful event to raise.
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
1

I think it depends a little bit on the scope of the project: if this is a small project, where Domainmodels are also used as UI-mmodels, sure go ahed an do so if you like.

But if you frequently NEED UI-models, e.g. because there are a lot of properties / methods which are not part of your domain model, don't bother - you create overhead for little or no reason.

When do you need a UI-model? My rule of thumb: If you are introducing a property with an[NotMapped] (Entity Framework) Attribute, go ahead and make a UI-model with this property.

Alsoif there is a chance that parts of this project are used in another context( Webapp, phone etc. pp) , I would advise against it- you will need UI models anyway.

Christian Sauer
  • 10,351
  • 10
  • 53
  • 85
0

To avoid to insert code in your classes you can make a transparent proxy. You can use Castle http://www.castleproject.org/dynamicproxy/index.html

The only limitation is that you have to create instances of your classes via factory.

You could also use System.Runtime.Remoting.Proxies.RealProxy class but your base class must derive from MarshalByRef (is still POCO? :) ).

http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=IT-IT&k=k(SYSTEM.RUNTIME.REMOTING.PROXIES.REALPROXY)%3bk(TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION%3dV4.0%22)%3bk(DevLang-CSHARP)&rd=true

bubi
  • 6,414
  • 3
  • 28
  • 45