What is the difference between using MessagingCenter and standard .NET event handlers to inform stakeholders about changes?
Two (unverified) implementations of the same thing are given below for demonstration:
public class FooClass {
public event EventHandler SomeEvent;
public void DoSomeWork() {
if(SomeEvent != null)
SomeEvent(this, EventArgs.Empty);
}
}
public class BarClass {
FooClass _foo;
public BarClass() {
_foo = new FooClass();
_foo.SomeEvent += delegate {
};
}
}
Poems:
public class FooClass {
public const string SomeEventName = "SomeEvent";
public void DoSomeWork() {
MessagingCenter.Send<FooClass>(this, SomeEventName);
}
}
public class BarClass : IDisposable {
public BarClass() {
MessagingCenter.Subscribe<FooClass>(this, FooClass.SomeEventName, delegate {
});
}
public void Dispose() {
MessagingCenter.Unsubscribe<FooClass>(this, FooClass.SomeEventName);
}
}
There is no difference from what I can say, but if anyone can suggest any pros or minus in order to help me understand, I am currently using event handlers.
Does it make sense to switch to using the MessagingCenter? Or any new best practice?
source
share