What is the difference between using MessagingCenter and standard .NET event handlers for informing interested parties of changes?
Two (untested) implementations of the same thing are below to demonstrate:
public class FooClass {
public event EventHandler SomeEvent;
public void DoSomeWork() {
// ... stuff
if(SomeEvent != null)
SomeEvent(this, EventArgs.Empty);
}
}
public class BarClass {
FooClass _foo;
public BarClass() {
_foo = new FooClass();
_foo.SomeEvent += delegate {
// ... did something
};
}
}
Verses:
public class FooClass {
public const string SomeEventName = "SomeEvent";
public void DoSomeWork() {
// ... stuff
MessagingCenter.Send<FooClass>(this, SomeEventName);
}
}
public class BarClass : IDisposable {
public BarClass() {
MessagingCenter.Subscribe<FooClass>(this, FooClass.SomeEventName, delegate {
// .. did something
});
}
public void Dispose() {
MessagingCenter.Unsubscribe<FooClass>(this, FooClass.SomeEventName);
}
}
From what I can tell there doesn't seem to be any difference, but if anyone can suggest any pros or cons for either, that'd help me understand. Currently, I've been using event handlers.
Is there any point in switching to using MessagingCenter? Or any new best practice?
MessagingCenter messages are strings. Publishers notify subscribers of a message with one of the MessagingCenter.Send overloads. The following code example publishes a Hi message: C# Copy.
An event handler, in C#, is a method that contains the code that gets executed in response to a specific event that occurs in an application. Event handlers are used in graphical user interface (GUI) applications to handle events such as button clicks and menu selections, raised by controls in the user interface.
Events are typically used to signal user actions such as button clicks or menu selections in graphical user interfaces. When an event has multiple subscribers, the event handlers are invoked synchronously when an event is raised. To invoke events asynchronously, see Calling Synchronous Methods Asynchronously.
To define an event, you use the C# event or the Visual Basic Event keyword in the signature of your event class, and specify the type of delegate for the event. Delegates are described in the next section.
The MessagingCenter from Xamarin is used to reduce coupling between ViewModels, as the sender and receiver do not need to know each other.
You can still build a similar structure by creating something like an "EventHub"/"EventAggregator" which knows sender and receiver and uses .NET events.
The MessagingCenter itself is kind of an EventAggregator
ImageSource : https://msdn.microsoft.com/en-us/library/ff921122.aspx
Here is a nice explanation of EventAggregators.
An Event Aggregator is a simple element of indirection. In its simplest form you have it register with all the source objects you are interested in, and have all target objects register with the Event Aggregator. The Event Aggregator responds to any event from a source object by propagating that event to the target objects.
To Answer the question:
Is there any point in switching to using MessagingCenter? Or any new best practice?
If you are not using something like an EventAggregator it is a good choice to switch to the MessagingCenter, or build an EventAggregator on your own. As Saruman made a good hint on explaining what coupling is. You allways want to reduce coupling for a clean code.
ViewModels
without knowing who sent it to whom via a simple Message Contract.MessagingCenter
is the best approach.ViewModels
,
while EventHandlers
makes dependency and may prohibit something
from being released. You explicitly have to decouple event handlers
from the events to better release the resources.MessagingCenter
should be applied when the receiver doesn't care
who sent the message and the sender doesn't care who will receive it.
Events should be used when the receiver needs to know who sent the
message, but the sender still doesn't care who handles it.MessagingCenter
over Events
but, if you make too much use of
too many Messages using MessagingCenter
, it would be hard to
identify who sent it and when sent it, the relation between messages
would be hard to guess, thus making it hard time while debugging the
app.If you have access to those classes (i.e from where you want to call your methods) then there really is not a lot of difference.
However if you don't have access to those classes(i.e inside a view model or decoupled class) then message subscription event aggregation is a useful tool
Message center reduces coupling and enables view models and other components to communicate with without having to know anything about each other besides a simple Message contract.
Coupling
In software engineering, coupling is the degree of interdependence between software modules; a measure of how closely connected two routines or modules are the strength of the relationships between modules.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With