Guest User

IEventAggregator.cs

a guest
Apr 16th, 2023
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 18.14 KB | None | 0 0
  1. namespace BionicCode.Utilities.Net
  2. {
  3.   using System;
  4.   using System.Collections.Generic;
  5.   using System.Threading;
  6.  
  7.   /// <summary>
  8.   /// Allows listening to events without introducing direct coupling between observer and observable. The observer can handle events withou introducing a dependency to the event source.
  9.   /// </summary>
  10.   public interface IEventAggregator
  11.   {
  12.     /// <summary>
  13.     /// Register a type as event source.
  14.     /// </summary>
  15.     /// <param name="eventSource">The publisher instance.</param>
  16.     /// <param name="eventNames">A collection of event names that define the observed events of the <paramref name="eventSource"/></param>
  17.     /// <returns><c>true</c> when registration was successful, otherwise <c>false</c>.</returns>
  18.     bool TryRegisterObservable(object eventSource, IEnumerable<string> eventNames);
  19.  
  20.     /// <summary>
  21.     /// Registers an event delegate to handle a specific event published by a specific observable type.
  22.     /// </summary>
  23.     /// <param name="eventName">The name of the observed event.</param>
  24.     /// <param name="eventSourceType">The type of the observable.</param>
  25.     /// <param name="eventHandler">A delegate that handles the specified event.</param>
  26.     /// <returns><c>true</c> when registration was successful, otherwise <c>false</c>.</returns>
  27.     bool TryRegisterObserver(string eventName, Type eventSourceType, Delegate eventHandler);
  28.  
  29.     /// <summary>
  30.     /// Registers an event delegate to handle a specific event published by a specific observable type.
  31.     /// </summary>
  32.     /// <param name="eventName">The name of the observed event.</param>
  33.     /// <param name="eventSourceType">The type of the observable.</param>
  34.     /// <param name="eventHandler">A delegate that handles the specified event.</param>
  35.     /// <param name="isMarshalEventToCurrentThreadEnabled"><c>true</c> if the current <see cref="SynchronizationContext"/> should be captured to execute the event handler.</param>
  36.     /// <returns><c>true</c> when registration was successful, otherwise <c>false</c>.</returns>
  37.     bool TryRegisterObserver(string eventName, Type eventSourceType, Delegate eventHandler, bool isMarshalEventToCurrentThreadEnabled);
  38.  
  39.     /// <summary>
  40.     /// Registers an event delegate to handle a specific event published by a specific observable type.
  41.     /// </summary>
  42.     /// <param name="eventName">The name of the observed event.</param>
  43.     /// <param name="eventSourceType">The type of the observable.</param>
  44.     /// <param name="eventHandler">A delegate that handles the specified event.</param>
  45.     /// <param name="synchronizationContext">The <see cref="SynchronizationContext"/> that the event delegate is to be executed on.</param>
  46.     /// <returns><c>true</c> when registration was successful, otherwise <c>false</c>.</returns>
  47.     bool TryRegisterObserver(string eventName, Type eventSourceType, Delegate eventHandler, SynchronizationContext synchronizationContext);
  48.  
  49.     /// <summary>
  50.     /// Registers an event delegate to handle a specific event published by a specific observable type.
  51.     /// </summary>
  52.     /// <typeparam name="TEventArgs">The type of the event args object.</typeparam>
  53.     /// <param name="eventName">The name of the observed event.</param>
  54.     /// <param name="eventSourceType">The type of the observable.</param>
  55.     /// <param name="eventHandler">A delegate that handles the specified event.</param>
  56.     /// <returns><c>true</c> when registration was successful, otherwise <c>false</c>.</returns>
  57.     bool TryRegisterObserver<TEventArgs>(string eventName, Type eventSourceType, EventHandler<TEventArgs> eventHandler);
  58.  
  59.     /// <summary>
  60.     /// Registers an event delegate to handle a specific event published by a specific observable type.
  61.     /// </summary>
  62.     /// <typeparam name="TEventArgs">The type of the event args object.</typeparam>
  63.     /// <param name="eventName">The name of the observed event.</param>
  64.     /// <param name="eventSourceType">The type of the observable.</param>
  65.     /// <param name="eventHandler">A delegate that handles the specified event.</param>
  66.     /// <param name="isMarshalEventToCurrentThreadEnabled"><c>true</c> if the current <see cref="SynchronizationContext"/> should be captured to execute the event handler.</param>
  67.     /// <returns><c>true</c> when registration was successful, otherwise <c>false</c>.</returns>
  68.     bool TryRegisterObserver<TEventArgs>(string eventName, Type eventSourceType, EventHandler<TEventArgs> eventHandler, bool isMarshalEventToCurrentThreadEnabled);
  69.  
  70.     /// <summary>
  71.     /// Registers an event delegate to handle a specific event published by a specific observable type.
  72.     /// </summary>
  73.     /// <typeparam name="TEventArgs">The type of the event args object.</typeparam>
  74.     /// <param name="eventName">The name of the observed event.</param>
  75.     /// <param name="eventSourceType">The type of the observable.</param>
  76.     /// <param name="eventHandler">A delegate that handles the specified event.</param>
  77.     /// <param name="synchronizationContext">The <see cref="SynchronizationContext"/> that the event delegate is to be executed on.</param>
  78.     /// <returns><c>true</c> when registration was successful, otherwise <c>false</c>.</returns>
  79.     bool TryRegisterObserver<TEventArgs>(string eventName, Type eventSourceType, EventHandler<TEventArgs> eventHandler, SynchronizationContext synchronizationContext);
  80.  
  81.     /// <summary>
  82.     /// Register an event delegate to handle a specific event which could be published by any type.
  83.     /// </summary>
  84.     /// <param name="eventName">The name of the observed event.</param>
  85.     /// <param name="eventHandler">A delegate that handles the specified event.</param>
  86.     /// <returns><c>true</c> when registration was successful, otherwise <c>false</c>.</returns>
  87.     bool TryRegisterGlobalObserver(string eventName, Delegate eventHandler);
  88.  
  89.     /// <summary>
  90.     /// Register an event delegate to handle a specific event which could be published by any type.
  91.     /// </summary>
  92.     /// <param name="eventName">The name of the observed event.</param>
  93.     /// <param name="eventHandler">A delegate that handles the specified event.</param>
  94.     /// <param name="isMarshalEventToCurrentThreadEnabled"><c>true</c> if the current <see cref="SynchronizationContext"/> should be captured to execute the event handler.</param>
  95.     /// <returns><c>true</c> when registration was successful, otherwise <c>false</c>.</returns>
  96.     bool TryRegisterGlobalObserver(string eventName, Delegate eventHandler, bool isMarshalEventToCurrentThreadEnabled);
  97.  
  98.     /// <summary>
  99.     /// Register an event delegate to handle a specific event which could be published by any type.
  100.     /// </summary>
  101.     /// <param name="eventName">The name of the observed event.</param>
  102.     /// <param name="eventHandler">A delegate that handles the specified event.</param>
  103.     /// <param name="synchronizationContext">The <see cref="SynchronizationContext"/> that the event delegate is to be executed on.</param>
  104.     /// <returns><c>true</c> when registration was successful, otherwise <c>false</c>.</returns>
  105.     bool TryRegisterGlobalObserver(string eventName, Delegate eventHandler, SynchronizationContext synchronizationContext);
  106.  
  107.     /// <summary>
  108.     /// Register an event delegate to handle a specific event which could be published by any type.
  109.     /// </summary>
  110.     /// <typeparam name="TEventArgs">The type of the event args object.</typeparam>
  111.     /// <param name="eventName">The name of the observed event.</param>
  112.     /// <param name="eventHandler">A delegate that handles the specified event.</param>
  113.     /// <returns><c>true</c> when registration was successful, otherwise <c>false</c>.</returns>
  114.     bool TryRegisterGlobalObserver<TEventArgs>(string eventName, EventHandler<TEventArgs> eventHandler);
  115.  
  116.     /// <summary>
  117.     /// Register an event delegate to handle a specific event which could be published by any type.
  118.     /// </summary>
  119.     /// <typeparam name="TEventArgs">The type of the event args object.</typeparam>
  120.     /// <param name="eventName">The name of the observed event.</param>
  121.     /// <param name="eventHandler">A delegate that handles the specified event.</param>
  122.     /// <param name="isMarshalEventToCurrentThreadEnabled"><c>true</c> if the current <see cref="SynchronizationContext"/> should be captured to execute the event handler.</param>
  123.     /// <returns><c>true</c> when registration was successful, otherwise <c>false</c>.</returns>
  124.     bool TryRegisterGlobalObserver<TEventArgs>(string eventName, EventHandler<TEventArgs> eventHandler, bool isMarshalEventToCurrentThreadEnabled);
  125.  
  126.     /// <summary>
  127.     /// Register an event delegate to handle a specific event which could be published by any type.
  128.     /// </summary>
  129.     /// <typeparam name="TEventArgs">The type of the event args object.</typeparam>
  130.     /// <param name="eventName">The name of the observed event.</param>
  131.     /// <param name="eventHandler">A delegate that handles the specified event.</param>
  132.     /// <param name="synchronizationContext">The <see cref="SynchronizationContext"/> that the event delegate is to be executed on.</param>
  133.     /// <returns><c>true</c> when registration was successful, otherwise <c>false</c>.</returns>
  134.     bool TryRegisterGlobalObserver<TEventArgs>(string eventName, EventHandler<TEventArgs> eventHandler, SynchronizationContext synchronizationContext);
  135.  
  136.     /// <summary>
  137.     /// Registers a handler for any registered event source with a compatible event delegate signature.
  138.     /// </summary>
  139.     /// <param name="eventHandler">A delegate that handles the specified event.</param>
  140.     /// <returns><c>true</c> when registration was successful, otherwise <c>false</c>.</returns>
  141.     bool TryRegisterGlobalObserver(Delegate eventHandler);
  142.  
  143.     /// <summary>
  144.     /// Registers a handler for any registered event source with a compatible event delegate signature.
  145.     /// </summary>
  146.     /// <param name="eventHandler">A delegate that handles the specified event.</param>
  147.     /// <param name="isMarshalEventToCurrentThreadEnabled"><c>true</c> if the current <see cref="SynchronizationContext"/> should be captured to execute the event handler.</param>
  148.     /// <returns><c>true</c> when registration was successful, otherwise <c>false</c>.</returns>
  149.     bool TryRegisterGlobalObserver(Delegate eventHandler, bool isMarshalEventToCurrentThreadEnabled);
  150.  
  151.     /// <summary>
  152.     /// Registers a handler for any registered event source with a compatible event delegate signature.
  153.     /// </summary>
  154.     /// <param name="eventHandler">A delegate that handles the specified event.</param>
  155.     /// <param name="synchronizationContext">The <see cref="SynchronizationContext"/> that the event delegate is to be executed on.</param>
  156.     /// <returns><c>true</c> when registration was successful, otherwise <c>false</c>.</returns>
  157.     bool TryRegisterGlobalObserver(Delegate eventHandler, SynchronizationContext synchronizationContext);
  158.  
  159.     /// <summary>
  160.     /// Registers a handler for any registered event source with a compatible event delegate signature.
  161.     /// </summary>
  162.     /// <typeparam name="TEventArgs">The type of the event args object.</typeparam>
  163.     /// <param name="eventHandler">A delegate that handles the specified event.</param>
  164.     /// <returns><c>true</c> when registration was successful, otherwise <c>false</c>.</returns>
  165.     bool TryRegisterGlobalObserver<TEventArgs>(EventHandler<TEventArgs> eventHandler);
  166.  
  167.     /// <summary>
  168.     /// Registers a handler for any registered event source with a compatible event delegate signature.
  169.     /// </summary>
  170.     /// <typeparam name="TEventArgs">The type of the event args object.</typeparam>
  171.     /// <param name="eventHandler">A delegate that handles the specified event.</param>
  172.     /// <param name="isMarshalEventToCurrentThreadEnabled"><c>true</c> if the current <see cref="SynchronizationContext"/> should be captured to execute the event handler.</param>
  173.     /// <returns><c>true</c> when registration was successful, otherwise <c>false</c>.</returns>
  174.     bool TryRegisterGlobalObserver<TEventArgs>(EventHandler<TEventArgs> eventHandler, bool isMarshalEventToCurrentThreadEnabled);
  175.  
  176.     /// <summary>
  177.     /// Registers a handler for any registered event source with a compatible event delegate signature.
  178.     /// </summary>
  179.     /// <typeparam name="TEventArgs">The type of the event args object.</typeparam>
  180.     /// <param name="eventHandler">A delegate that handles the specified event.</param>
  181.     /// <param name="synchronizationContext">The <see cref="SynchronizationContext"/> that the event delegate is to be executed on.</param>
  182.     /// <returns><c>true</c> when registration was successful, otherwise <c>false</c>.</returns>
  183.     bool TryRegisterGlobalObserver<TEventArgs>(EventHandler<TEventArgs> eventHandler, SynchronizationContext synchronizationContext);
  184.  
  185.     /// <summary>
  186.     /// Unregister the event publisher for a collection of specified events.
  187.     /// </summary>
  188.     /// <param name="eventSource">The event publisher instance.</param>
  189.     /// <param name="eventNames">The names of the events to unregister.</param>
  190.     /// <param name="removeEventObservers">If <c>true</c> removes all event listeners of the specified events. The value is <c>false</c> by default.</param>
  191.     /// <returns><c>true</c> when removal was successful, otherwise <c>false</c>.</returns>
  192.     bool TryRemoveObservable(
  193.       object eventSource,
  194.       IEnumerable<string> eventNames,
  195.       bool removeEventObservers = false);
  196.  
  197.     /// <summary>
  198.     /// Unregister the event publisher for all events.
  199.     /// </summary>
  200.     /// <param name="eventSource">The event publisher instance.</param>
  201.     /// <param name="removeEventObservers">If <c>true</c> removes all event listeners of the specified events. The value is <c>false</c> by default.</param>
  202.     /// <returns><c>true</c> when removal was successful, otherwise <c>false</c>.</returns>
  203.     bool TryRemoveObservable(object eventSource, bool removeEventObservers = false);
  204.  
  205.     /// <summary>
  206.     /// Removes the event handler for a specified event of a certain event publisher type.
  207.     /// </summary>
  208.     /// <param name="eventName">The event name of the event that the delegate is handling.</param>
  209.     /// <param name="eventSourceType">The type of the event publisher.</param>
  210.     /// <param name="eventHandler">The event handler to remove.</param>
  211.     /// <returns><c>true</c> when removal was successful, otherwise <c>false</c>.</returns>
  212.     bool TryRemoveObserver(string eventName, Type eventSourceType, Delegate eventHandler);
  213.  
  214.     /// <summary>
  215.     /// Removes the event handler for a specified event of a certain event publisher type.
  216.     /// </summary>
  217.     /// <typeparam name="TEventArgs">The type of the event args object.</typeparam>
  218.     /// <param name="eventName">The event name of the event that the delegate is handling.</param>
  219.     /// <param name="eventSourceType">The type of the event publisher.</param>
  220.     /// <param name="eventHandler">The event handler to remove.</param>
  221.     /// <returns><c>true</c> when removal was successful, otherwise <c>false</c>.</returns>
  222.     bool TryRemoveObserver<TEventArgs>(string eventName, Type eventSourceType, EventHandler<TEventArgs> eventHandler);
  223.  
  224.     /// <summary>
  225.     /// Removes all event handlers for a specified event no matter event publisher type.
  226.     /// </summary>
  227.     /// <param name="eventName">The event name of the event that the delegate is handling.</param>
  228.     /// <returns><c>true</c> when removal was successful, otherwise <c>false</c>.</returns>
  229.     bool TryRemoveAllObservers(string eventName);
  230.  
  231.     /// <summary>
  232.     /// Removes all event handlers for a specific event publisher type and specific event.
  233.     /// </summary>
  234.     /// <param name="eventName">The event name of the event that the delegate is handling.</param>
  235.     /// <param name="eventSourceType">The type of the event publisher.</param>
  236.     /// <returns><c>true</c> when removal was successful, otherwise <c>false</c>.</returns>
  237.     bool TryRemoveAllObservers(string eventName, Type eventSourceType);
  238.  
  239.     /// <summary>
  240.     /// Removes all event handlers for a specified event publisher type.
  241.     /// </summary>
  242.     /// <param name="eventSourceType">The type of the event publisher.</param>
  243.     /// <returns><c>true</c> when removal was successful, otherwise <c>false</c>.</returns>
  244.     bool TryRemoveAllObservers(Type eventSourceType);
  245.  
  246.     /// <summary>
  247.     /// Removes the event handler for a specified event no matter the event publisher type.
  248.     /// </summary>
  249.     /// <param name="eventName">The event name of the event that the delegate is handling.</param>
  250.     /// <param name="eventHandler">The event handler to remove</param>
  251.     /// <returns><c>true</c> when removal was successful, otherwise <c>false</c>.</returns>
  252.     bool TryRemoveGlobalObserver(string eventName, Delegate eventHandler);
  253.  
  254.     /// <summary>
  255.     /// Removes the event handler for a specified event no matter the event publisher type.
  256.     /// </summary>
  257.     /// <typeparam name="TEventArgs">The type of the event args object.</typeparam>
  258.     /// <param name="eventName">The event name of the event that the delegate is handling.</param>
  259.     /// <param name="eventHandler">The event handler to remove</param>
  260.     /// <returns><c>true</c> when removal was successful, otherwise <c>false</c>.</returns>
  261.     bool TryRemoveGlobalObserver<TEventArgs>(string eventName, EventHandler<TEventArgs> eventHandler);
  262.  
  263.     /// <summary>
  264.     /// Removes the event handler for all registered events with a compatible event delegate signature.
  265.     /// </summary>
  266.     /// <param name="eventHandler">The event handler to remove.</param>
  267.     /// <returns><c>true</c> when removal was successful, otherwise <c>false</c>.</returns>
  268.     bool TryRemoveGlobalObserver(Delegate eventHandler);
  269.  
  270.     /// <summary>
  271.     /// Removes the event handler for all registered events with a compatible event delegate signature.
  272.     /// </summary>
  273.     /// <typeparam name="TEventArgs">The type of the event args object.</typeparam>
  274.     /// <param name="eventHandler">The event handler to remove.</param>
  275.     /// <returns><c>true</c> when removal was successful, otherwise <c>false</c>.</returns>
  276.     bool TryRemoveGlobalObserver<TEventArgs>(EventHandler<TEventArgs> eventHandler);
  277.   }
  278. }
Advertisement
Add Comment
Please, Sign In to add comment