Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

wpf

dto

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?

like image 414
JiBéDoublevé Avatar asked Aug 26 '11 13:08

JiBéDoublevé


People also ask

What is the use of INotifyPropertyChanged in WPF?

INotifyPropertyChanged interface is used to notify the view or ViewModel that it does not matter which property is binding; it is updated. Let's take an example for understanding this interface. Take one WPF Window in which there are a total of three fields: First Name, Last Name and Full Name.

Does ObservableCollection implement INotifyPropertyChanged?

The ObservableCollection provides us with the CollectionChanged Event and implements INotifyPropertyChanged itself.

How do you implement INotifyPropertyChanged?

To implement INotifyPropertyChanged you need to declare the PropertyChanged event and create the OnPropertyChanged method. Then for each property you want change notifications for, you call OnPropertyChanged whenever the property is updated.


1 Answers

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

like image 68
Rachel Avatar answered Nov 14 '22 23:11

Rachel