What is the difference between using MessagingCenter and standard .NET event handlers to inform stakeholders about changes?

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() {
     // ... stuff
    if(SomeEvent != null)
      SomeEvent(this, EventArgs.Empty);
  }
}

public class BarClass {
  FooClass _foo;

  public BarClass() {
    _foo = new FooClass();
    _foo.SomeEvent += delegate {
      // ... did something
   };
  }
}

Poems:

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);
  }
}

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?

+4
source share
3
  • MessagingCenter Model-View-ViewModel .
  • ViewModels, , - .
  • Ex. , - , MessagingCenter .
  • , ViewModels, EventHandlers - . , .
  • MessagingCenter , , , . , , , , .
  • MessagingCenter Events, MessagingCenter, , , , , .
+1

MessagingCenter Xamarin ViewModels, .

, "EventHub" / "EventAggregator", .NET.

MessagingCenter - EventAggregator

EventAggregator Image

ImageSource: https://msdn.microsoft.com/en-us/library/ff921122.aspx

EventAggregators.

- . , , , - . .

:

MessagingCenter? ?

- EventAggregator, , MessagingCenter, EventAggregator. Saruman , . .

+3

(, , ), .

, (, ),

, .

In software development, communication is the degree of interdependence between software modules; A measure of how closely two subprograms or modules are related is the strength of the relationship between the modules.

+2
source

Source: https://habr.com/ru/post/1691345/


All Articles