views:

91

answers:

3

I am thinking of making all my entities in my domain model implement INotifyPropertyChanged . My main reason behind this would be:

If some entity gets changed in the domain the presentation layer would immediately know about it and change accordingly (I am trying to implement the MVPVM pattern and the presentation objects are not the same as domain objects). I think should make it easier for me.

What do you think are the drawbacks ?

+1  A: 

You answered it yourself. Single responsibility principle is the key.

I mean - your domain objects should NOT contain presentation layer stuff (or any other layer your pattern you follow has). That is not their job.

You can't be a professional boxer and ballet dancer at the same time.

Arnis L.
+1 Presentation layer stuff only belongs in the presentation layer - however, see my answer for a pragmatic view.
Mark Seemann
+3  A: 

Domain Models should mainly model the domain, so that is its primary responsibility. As Arnis L. put it in another answer, the domain model should not contain presentation layer stuff, and I can only agree.

However, pragmatically, I consider INotifyPropertyChanged (INPC) one of the more benevolent interfaces available. Although its main purpose is to support UI frameworks (both Windows Forms and WPF use it), it is mainly a signaling mechanism. Apart from the amount of work involved in implementing it, it doesn't do much harm.

By default I wouldn't implement it in my domain objects, but if I end up needing some kind of signaling mechanism (perhaps required by my View Models), I wouldn't hesitate to apply it. After all, even if the SRP states that a class should only have a single responsibility, such a class is still the type best suited for the task. No other class knows better when its state changes than the class itself.

So if you need a signaling mechanism in the Domain Model, then go ahead and implement INPC - there's no reason to invent a new one. The interface is defined in System.ComponentModel, so that also gives you a pretty good idea that it isn't tied to a particular UI framework.

The main drawback of implementing INPC is the amount of work involved, so I would only implement it as needed.

Mark Seemann
Later on - you will add countless 'Serializable' attributes, bunch of code access security related stuff, will implement 3rd party interfaces and then it will be too late - a big ball of mud. Anyway - good luck. :)
Arnis L.
Have to agree with Mark. Although INotifyPropertyChanged is used heavily for UI binding, in certain cases it could be used to detect domain events.As for the verbose nature of implementing it, you could encapsulate the handling into a base class, use T4, use a dynamic proxy, or possibly ActiveSharp (or a combination of all!)
Vijay Patel
+1  A: 

I'm currently having the same discussion with my colleagues. One of the suggestions is indeed to implement NotifyPropertyChanged in the domain model, and use these object to bind to the UI.
Another suggestion is to leave the domain model alone, and create a presentation layer which wraps or decorates the domain model. The question with this approach is: how do you propagate business rules implemented in the domain model to the UI?
For example, a business rule is: "When a location changes, the status has to be cleared". This can easily be implemented in the domain model, but if you use a separate objects to bind the UI, this rule needs to be implemented again. At the moment at least I don't see another way of doing this.

Any thoughts?
(btw I'm talking about a fairly simple domain model which is populated by a webservice)

Roel