WPF MVVM: INPC and mediation between model and view model

I have read various approaches for transferring changes in model data to a view model. Some speculate that the model should implement INotifyPropertyChanged where possible so that it can notify the representation model of the changed properties. Some offer a service level between the model and the presentation model, with the service level implementing INPC method calls routed through this service level to the model, so the service level notifies the presentation model.

I believe that the latter is a more detailed revision of the former and began to introduce INPC in my model classes. This is wrong because

a) Now I need to write an event handler in my view model for notifications from the model. This takes the form of a long switch (propertyName), which sets the corresponding property (s) in the view model, causing the NPCs to be sent up again. I feel like I write a lot of code tags here.

b) The view model is now tied to my model through a bunch of lines, which are regulated solely by agreement, if the "interface" is not defined. Not to mention the difficulties that this IDE has.

c) My model must be modified to accommodate this context! What if it somehow closed? I thought similar models were designed to increase code reuse and separate problems. Not only that, but the code needed to trigger INPC events is tedious and repetitive and not very abstract.

I really want to know how WPF professionals approach this issue regardless of dependency properties, etc. I feel like I'm missing something. I'm not interested in using a framework that I would like to learn from scratch. I have been away from WPF for a year or two, and recently working with AngularJS, I asked me a question about my methods.

Thanks!

+7
c # wpf mvvm inotifypropertychanged dependency-properties
source share
1 answer

For the purposes of this answer, I will name the data types of business object classes.

In my personal experience, view models are always tied to data types. You must have the properties of the types you want to map, so there will always be a link from the namespace of the view models to the namespace of the data types.

What you call a model (from your description in your comment) is similar to my view models. My view models have properties, mostly data type types, and methods, while my data type classes just contain properties for the most part ... they just own data and change reporters.

You seem to have the impression that the INotifyPropertyChanged interface fulfills some responsibilities between the "model", as you call it, and the classes of the view model ... in my opinion, this is not necessary at best ... from the INotifyPropertyChanged Interface on MSDN:

The INotifyPropertyChanged interface is used to notify clients, usually binding clients, that the value of a property has changed.

Therefore, I see the INotifyPropertyChanged interface as a "life blood" that goes between views and view models and data objects. Some developers prefer to โ€œwrapโ€ each data type in their own view model, but I prefer to implement the INotifyPropertyChanged interface INotifyPropertyChanged in my data types.

The main reason I do this is because I can connect to this structure in the custom sets of classes that I have. This allows me to have collections that are aware of any changes made to a property in any element in the collection, among other things. It also allows me to sync data to my base classes so that objects know when they have any changes.

It also saves time when creating matching class classes for each data type class. Why do two classes do what can be done? I never need this level of separation. If I understand you correctly, the implementation of this interface in your data type types will lead to a denial of the need to fulfill your point a).

Some of your b) and c) points can also be invalidated if you can use .NET 4.5, because there is a new CallerMemberNameAttribute attribute that you can use to automatically provide the name of each property to the PropertyChanged handler. I found a good article called C # 5 - creating INotifyPropertyChanged Easy with a good description about this.

I have written several large-scale WPF applications and several frameworks, and I have never had a problem implementing the INotifyPropertyChanged interface in my data type classes. Actually, I donโ€™t think I could write them at the same time if I had to implement shell model classes for each data type class. This method has served me well so far, and I plan to stick with it until I find a better way, at least. However, this is just one developerโ€™s opinion, and you need to go with what suits you.

+5
source share

All Articles