/// <summary>
        /// Gets the object where the subscription is registered for
        /// </summary>
        /// <returns>The <see cref="object"/></returns>
        protected override object GetSubscriptionObject(IMessageBusEventHandlerSubscription messageBusEventHandlerSubscription)
        {
            if (messageBusEventHandlerSubscription is ObjectChangedMessageBusEventHandlerSubscription objectChangedMessageBusEventHandlerSubscription)
            {
                return(objectChangedMessageBusEventHandlerSubscription.Thing as object ?? objectChangedMessageBusEventHandlerSubscription.Type);
            }

            return(null);
        }
        /// <summary>
        /// Register a message bus event handler
        /// </summary>
        /// <param name="listener">The <see cref="IObservable{T}"/> to listen to for message bus messages</param>
        /// <param name="messageBusEventHandlerSubscription">The <see cref="IMessageBusEventHandlerSubscription"/> instance that holds information about the event handler</param>
        /// <returns>A <see cref="MessageBusEventHandlerDisposer"/> as an <see cref="IDisposable"/></returns>
        public IDisposable RegisterEventHandler(IObservable <T> listener, IMessageBusEventHandlerSubscription messageBusEventHandlerSubscription)
        {
            return(Task.Run
                       (() =>
            {
                if (!this.MessageBusHandlerDataList.TryGetValue(listener, out var messageBusEventHandlerSubscriptionDictionary))
                {
                    messageBusEventHandlerSubscriptionDictionary = new Dictionary <object, HashSet <IMessageBusEventHandlerSubscription> >();
                    this.MessageBusHandlerDataList.Add(listener, messageBusEventHandlerSubscriptionDictionary);

                    // At least one subscription, otherwise the CDPMessageBus could remove the listener unexpectedly
                    this.disposables.Add(
                        listener
                        .ObserveOn(RxApp.MainThreadScheduler)
                        .Subscribe(
                            x =>
                            this.HandleEvents(listener, x)));
                }

                var subscriptionObject = this.GetSubscriptionObject(messageBusEventHandlerSubscription);
                if (subscriptionObject == null)
                {
                    return new MessageBusEventHandlerDisposer(new Action(() => { }));
                }

                if (!messageBusEventHandlerSubscriptionDictionary.TryGetValue(subscriptionObject, out var messageBusEventHandlerSubscriptionHashSet))
                {
                    messageBusEventHandlerSubscriptionHashSet = new HashSet <IMessageBusEventHandlerSubscription>();
                    messageBusEventHandlerSubscriptionDictionary.Add(subscriptionObject, messageBusEventHandlerSubscriptionHashSet);
                }

                messageBusEventHandlerSubscriptionHashSet.Add(messageBusEventHandlerSubscription);

                Action cleanUpAction = () =>
                {
                    if (messageBusEventHandlerSubscriptionHashSet.Contains(messageBusEventHandlerSubscription))
                    {
                        messageBusEventHandlerSubscriptionHashSet.Remove(messageBusEventHandlerSubscription);
                    }

                    if (!messageBusEventHandlerSubscriptionHashSet.Any())
                    {
                        messageBusEventHandlerSubscriptionDictionary.Remove(subscriptionObject);
                    }

                    messageBusEventHandlerSubscription.Dispose();
                };

                return new MessageBusEventHandlerDisposer(cleanUpAction);
            }
                       ).Result);
        }
 /// <summary>
 /// Gets the object where the subscription is registered for
 /// </summary>
 /// <returns>The <see cref="object"/></returns>
 protected virtual object GetSubscriptionObject(IMessageBusEventHandlerSubscription messageBusEventHandlerSubscription)
 {
     return(this);
 }