INotifyPropertyChanged usage outside data binding - c#

I've never used INotifyPropertyChanged, and I'm considering using it widely throughout a new application.
My question is, is it 'proper' to use INotifyPropertyChanged interface in order to provide event notifications for things other than databound controls?
It seems from a bunch of examples online that this interface is widely used for notifying grids and such for when data is changed. I have various scenario's where I need other classes to be notified of data changes in other classes, and I was wondering if you think it's cleaner to implement this interface, and perform the changed call on setters, or rather just create regular events.

When making a choice like this, I lean towards using language features over other constructs.
A severe downside to INotifyPropertyChanged is that it only provides the property name as a string on update, and your consuming classes have to parse that string and decide how to act on it.
With events you can provide any kind of delegate signature that the event requires and the consuming classes can act on that change directly.
If you peek under the hood, you'll find that INotifyPropertyChanged is an event anyway, so why not use events directly?

I think you can use this Interface for those scenarios but you have to really think hard about if you want this thight coupeling between your classes.
If this makes sense to you - sure, why reinvent the wheel?

I think you should go ahead an implement the INotifyPropertyChanged interface, just because it's there for that purpose and its what the UI uses to pickup changes, there's no need to create custom events yourself.

WPF and Silverlight both use INotifyPropertyChanged extensively in the data binding system. If your are going to databind to your objects, you should definetely use INotifyPropertyChanged. |
Otherwise, as far as I know, it does not influence .NET Framework technologies.
INotifyPropertyChanged is not the cleanest way of notifying when your property changes, depending on how you look at it, since there is one event and a string with the propertyname, you will have to make a switch statement. It sure is easier to implement comparing to events per property though

I would not use INotifyPropertyChanged for anything except databinding. This interface is probably the only option for databinding because controls which will act on PropertyChanged event do not know sender's type in advance. Because of that databinding has to use such a generic interface.
For my own types and scenarios I would use regular events (one event per each property which can change its value). INotifyPropertyChanged in such scenarios is kind of stringly typed code. You can see that even WPF itself is still full of oldscool events (FrameworkElement for example with a lot of ***Changed events).

Dont use is in other scenarios, if you need to notify other domain classes use domain events instead, which would be more business oriented and descriptive and strongly typed

I am not a fan of INPC outside of the UI realms as it hides the fact that significant events are being generated by instances of the object. Also, if I use your class implementing INPC, I will expect all properties to broadcast notifications, if only some of them do, that's a bug at runtime that can go unnoticed.

Related

C# MVVM Model Graph Message Mediator or INotifyPropertyChanged?

I am developing a C# MVVM WPF app and have problems to decide whether I should use the message mediator pattern or simple INotifyPropertyChanged for my UI 'live' Model change notifications. The problem in particular is that my model represents a graph with lots of 'live' objects that all have properties where different viewmodels will be interested in the changes at some point. I have about 3-5 viewmodels active that need notification of model changes. Some changes might be nested deep inside the models 'grandchildren'.
I tried to compare both messaging techniques, mediator pattern and INotifyPropertyChanged, and think that the mediator is better suited for change notifications between different modules/systems. My viewmodels definitely need the initial values of the model upon initialization and then change notifications afterwards. INotifyPropertyChanged seems to be the optimal choice in my case, but I am a bit skeptic, because I think lots of nameof(e.PropertyName) switch cases are not very elegant. Are there better alternatives for this problem?
As is typical for such cases, you could have both ways. You will obviously not be able to avoid using INotifyPropertyChanged because this is the WPF way of handling MVVM-based Data Binding, so all your view-models will be at least basic implementations of this interface.
I suspect that you might simply need both. I would not suggest using the INotifyPropertyChanged-based pattern inside your model. The reason this interface was created was to base the observer pattern on actual property names as strings. This was pretty much a "one-way street", as XAML, the typical "design" language supporting WPF, is parsed in a "stringly" manner, leaving few efficient alternatives. In my humble opinion, there is almost no reason that you would want to force notifications throughout your model to be as weakly-typed (stringly-typed, in fact) as this scenario, so I would suggest avoiding INotifyPropertyChanged if you have a relatively voluminous Domain Model.
Messaging would probably be the way to go, maintaining strong-typing with message type registrations and whatnot, unless you would like to consider other alternatives, such as simple C# events, or even combine both. While at it, try to stick and adhere to hierarchy and encapsulation as strongly as possible but also as weakly as practical*.
(*I mean, if some of your Domain objects reach multiple nesting levels, then, occasionally, it may be more practical to bypass hierarchy and let, for example, grand-grandchildren send messages of their own, instead of notifying their parents' parents through events, and letting these parents' parents then send the corresponding notification messages. In typical OOP programming, compromises are constantly weighted against practicality and time availability).

Should I use ObservableCollections in the Model in MVVM?

I'm using MVVM for a WPF client that represents a model and allows users to interact with it. I've always shied away from using the ObservableCollection class in the actual model (opting for generic collections like IList inside that model instead, and then converting that IList to the actual data-bound ObservableCollection on the ViewModel when the underlying collection changes). Reason being that MSDN presents the class as WPF and UI-centric:
You can enumerate over any collection that implements the IEnumerable
interface. However, to set up dynamic bindings so that insertions or
deletions in the collection update the UI automatically, the
collection must implement the INotifyCollectionChanged interface. This
interface exposes the CollectionChanged event, an event that should be
raised whenever the underlying collection changes. WPF provides the
ObservableCollection class, which is a built-in implementation of a
data collection that implements the INotifyCollectionChanged
interface.
Question: Is my distinction actually necessary? It's extra work and extra code. I understand that this topic might be too vague and subjective for SO, but maybe there are clear, universally agreed-upon conventions everybody follows.
Yes I think you are doing it right. Observable collections belong at the presentation layer. Add them to your view model, not your domain model.
This question may be of some help
but maybe there are clear, universally agreed-upon conventions everybody follows.
Please let me know when you find them, thx.
Is my distinction actually necessary?
It depends on what a model really is in this case, or rather how you define a model.
If it is some kind of domain business object that is defined in an assembly that is referenced by your business or service layer and may be used across your entire domain, it shouldn't implement any kind of client-specific interfaces such as INotifyCollectionChanged.
Then you should prefer to use a generic type such as IList and then create a wrapper class in the client application that you bind to the view element to.
But if the model is a class that is used only inside your client application and not in any other tiers of your application, then you might as well define the collection property as an ObservableCollection and bind to this one directly.
The point is that a domain business object should not have any knowledge of WPF or the fact that a client application may bind to it. That's why it seldom makes sense to bind directly to such objects in a WPF application without wrapping them in WPF-aware view model classes first.
This does add some extra work and an additional class but at the same time it helps maintain separation of concerns which is usually good for maintenance.
In an enterprise scenario it is also pretty common that the business object may contain business logic that you don't want to expose to the client and it also may contain additional properties that it makes no sense to expose to the view. So then you still need to wrap it in another class even if the collection property should actually return an ObservableCollection.
So if your question is "should I use ObservableCollections in the business objects" the answer is no.

How does a Binding actually work?

I've been learning WPF for a few months now and I'm curious about one thing. How does a Binding actually work? I mean, what happends, under the hood. I don't expect that anyone here would give a detailed explanation but maybe a good resource or link where to read up on something like this. I've been searching and googling for this but no good hits so far.
I realize that to fully understand this you would probably have to understand most parts of the framework but a little basic understanding would be great.
Thanks
There are two aspects to consider in binding, getting values in to the UI and having the UI be notified of changes in its DataContext.
Basically you can bind almost anything to any POCO object, the object does not need to implement anything special. The restriction with plain objects is the binding target will not be told when the underlying value changes.
Property changes are propogated through one of three mechanisims:
Dependancy Properties : will notify the binding system when its value changes, can also be used for animations.
INotifyPropertyChanged : If the binding is to a property on an object which implements INotifyPropertyChanged, the binding system will look for subscribe to the PropertyChanged event and update the binding target, when this event is raised, property names are sent as Strings.
*Property*Changed events : The last thing bindings will look for is an event with a name the same as the source property and Changed on the end, so a Name property would need to have a public event called NameChanged, this allows WPF to bind to older .net classes such as 1.1.
I don't know much about the specifics of binding in WPF, but if it's the same principle behind binding in System.ComponentModel and Windows Forms, then a blog article I wrote recently might help to shed some light on it for you:
Some Background on Windows Forms Data Binding
Basically, binding sources have to implement IList, IListSource, ITypedList or IBindingList. Through either reflection or explicit definition, data sources expose PropertyDescriptor objects which describe their bindable properties. The names of these properties (which may or may not be the names of actual properties on the objects contained in the data source - e.g. in DataTable, they are column names instead) are matched against the DisplayMember/ValueMember property or, in the case of WPF, the binding path.
This is quite a difficult question to answer. I believe there are roughly two aspects to an answer. The first is the documentation. If you go through all the documentation of binding expressions, including how paths are build and e.g. the helper classes like BindingOperations, you can find out a lot. If that's not enough, you can always dive into the code by downloading it from the Microsoft Source Initiative site.

Notify Gui that data class has changed

In C#:
I have a data class that is shared amongst several gui classes. I would like all of the gui classes that use it to be notified when some of the properties change, so they can keep the GUI up to date.
In a couple of the properties I have added delegates that the GUI classes can listen to for updates. This seems to work ok.
The problem I have is that more and more of the properties will require GUI notification. When that happens I will have to add more delegates. It also seems that this is adding an extra responsibility to the data class that it has to manage.
Is there some common pattern that I can use to monitor this class to extract this notification responsibility from the data class?
The common way of doing this is for the data class to implement INotifyPropertyChanged.
EDIT: If you have a lot of properties, this can lead to very repetitive code in the data class, and if you are binding to a UI, it might be best to use an AOP approach and intercept calls to the properties that you want to notify on. Most IoC containers have support for this sort of thing.
The pattern is called Observer. In .Net, events are one implementation of that pattern.
For the specific case of observing individual properties, the INotifyPropertyChanged interface should be used (as #Lee describes).
You don't say what GUI framework (WinForms, WPF) you are using, but there is the INotifyPropertyChanged interface. There is an How to guide on MSDN too.
You can just use the built-in System.EventHandler. That is a pretty standard pattern. You still need to define an event for each property that you want to monitor, but you don't need a separate delegate.
In Windows Forms there are several ways to notify GUI when some properties are chaged using Data Binding.
There are several types of Data binding in Windows Forms.
For simple data binding (data source with one object boud to one control) you can use INotifyPropertyChanged, or you can add events in this format: PropertyNameChanged for every property that you want update in GUI. And you should set Binding.ControlUpdateMode property to OnPropertyChanged.
For complex data binding (data source with many objects bound to control that can display many objects) you should use all from (1) and you should use BindingSource or BindingList or manually implement IBindingList;
For more information you should see this great books:
Windows Forms 2.0 Programming by Chris Sells
Data Binding with Windows Forms 2.0: Programming Smart Client Data Applications with .NET by Brian Noyes

Java "events" - nothing more than Interfaces. Why pretend it is Events when it is just a normal Interface and classes that implements that interface?

I am mainly developing in .NET C# and I love the Events in C#.
Im now doing som Android stuff and thus must deal with Java. When porting some code from C# to Java I ran into the problem of Events; Java does not have anything that corresponds to C# Events.
So, when reading up on how Java handles "events", the only thing I can conclude is that it doesnt. There is no such thing as "events" in Java. Instead they use normal Interfaces and classes that implement those interfaces.
In Java:
First, you have to first create the Interface
Then, all classes that want to listen to the "event" has to implement that interface.
Then, the class that fires the "event" has to keep a list of all listeners (Array of some sort)
Then, the class that fires the "event" has to have a method so that listeners can add themselves to the Array
And when the firing class decides to "fire the event", it has to iterate through the Array of the listeners, calling the methods.
That is just plain Interface-usage, not Events in my world.
Am I wrong?
No one is "pretending". The Java concepts were implemented long before C# was designed. The events in C# are really the same thing internally; though they are easier for the programmer to use. That is, the C# event variable maintains a list of event subscribing methods and the subscribers can add and remove method references from that list. The event provides a way for the owning class to trigger the event.
What is your definition of "event" anyway? This is a design pattern intended to decouple one system of classes from another. The other classes can subscribe to receive notification of an event in either case (C# or Java), it's just the implementation of that subscription and message trigger that differ.
In C# you must define the event method signature (a delegate). This is what interfaces do.
What C# does add that Java doesn't have is that you can pass references to methods (delegate, though I now understand this may become available in Java 7, at least at the JVM level). Since this does not exist in Java, there must be another means to provide event (an observer design pattern) receiver which must be an object and the best means to define this is through an interface -- yes you could use an abstract class, but that would severely limit the recipient types.
It is indeed just normal interfaces albeit with a naming and implementation "convention" - XXXListener and anonymous inner classes. I think this is fine and works well enough - the core language does not have to be bloated (maybe a little too harsh word for this - I don't want to start a flame war) by the notion of "events".
AFAIK Android draws inspiration for this pattern from Swing.
Note how most languages don't have a language construct for patterns like Singleton, Observer, ... it is not necessary if the patterns can be implemented easily - if it is a little more complicated it can be implemented by libraries, toolkits or frameworks.
JavaBeans is all about "convention over configuration". :-P Events are made by simply having methods that are named addXXXListener, removeXXXListener, and getXXXListeners, where the types involved derive from a listener interface.
In the same way, a property is made by simply having methods named getXXX and setXXX (either can be omitted for read-only or write-only properties).
How does a program locate events and properties? By using java.beans.Introspector, which puts those naming conventions into good use.
Sometimes, simplest is best. :-D
For clarification, the way the Android SDK implements and exposes the concept of an Event is distinct from the Java language. Different toolkits (i.e Swing, SWT) have their own way of doing things.
In Java, events represent all activity that goes on between the user and the application. Java's Abstract Windowing Toolkit (AWT) communicates these actions to the programs using events.

Categories

Resources