JiBéDoublevé
JiBéDoublevé

Reputation: 4269

Do I need to implement INotifyPropertyChanged when using DTO and WPF?

My question is fairly simple and already asked in the title.

Here's the context: I've got a domain with entities and repositories. The result of a query is mapped into DTO and sent to the GUI.

The GUI is implemented with WPF and for the mapping, I need classes that implement INotifyPropertyChanged.

My first idea is to have DTO that implement this interface because I foresee a lot of work to map again my DTO into items that implement INotifyPropertyChanged.

Is it a good practice? Has it pitfalls I haven't seen? What is the "official" good practice for this situation?

Upvotes: 6

Views: 3018

Answers (3)

jrwren
jrwren

Reputation: 17938

Even though it is a DTO, there isn't much reason to not implement INPC.

INPC is in every .net impl that I can think of, so you aren't taking extra dependencies that you might want to avoid at both ends of a connection (usually why you would use a DTO)

Using NotifyPropertyWeaver you can do it with very little code.

Just because your DTO implements that interface, I don't think it makes it any less of a DTO.

The wikipedia definition of DTO says that there is no behavior in a DTO. You have now added behavior in the form of the PropertyChanged event, but given that the whole reason to use a DTO is for remote objects (http://msdn.microsoft.com/en-us/library/ms978717.aspx) I am still convinced it is OK.

Fowler states that the point of a DTO is to reduce the number of parameters in a remote call. (http://martinfowler.com/eaaCatalog/dataTransferObject.html) This doesn't even say that you can't add behavior.

INPC away!

Upvotes: 2

Rachel
Rachel

Reputation: 132618

DTOs are supposed to be very simple, lightweight, data transfer objects. Because of this, I wouldn't implement anything on them other than their data. Also, I believe if serializing the class to/from a WCF server, the properties need to all be public, so you can't make things like the Id read-only

I would create Model classes that implement INotifyPropertyChanged and IDataErrorInfo for property changed notification and validation purposes, and have them accept a DTO in the Constructor. Using something like AutoMapper will make mapping a DTO to a Model pretty simple

Upvotes: 10

Strillo
Strillo

Reputation: 2982

The best practice with WPF would be to use the MVVM pattern (Model-View-ViewModel). In this instance, your DTO is the Model. You should not pass the Model directly to the View but instead wrap it into a ViewModel that can in turn implement the notification mechanisms. This way the Model is pure data and doesn't need to worry about what is using it. Also, there are several frameworks you can use to simplify the mapping work (e.g. Automapper).

Upvotes: 0

Related Questions