Exemple #1
0
        /// <summary>
        /// <see cref="ModuleHost"/> implements <see cref="IEventHandler"/> and exposes its methods so that a <see cref="IModule"/>
        /// instance can check to see if an <see cref="IEvent"/> instance can be handled and pass it to <see cref="ModuleHost.Handle(IEvent)"/>
        /// for handling. It is recommended to always use <see cref="IModuleHost"/> to handle events rather than invoking
        /// <see cref="Module.Handle(IEvent)"/> directly. Invoking <see cref="Module.Handle(IEvent)"/> will create a ghost event
        /// which will bee invisible to the <see cref="IModuleHost"/> and all other modules.
        /// </summary>
        public virtual void Handle(IEvent e)
        {
            // We generate a unique ID for the event and add it to the IEvent.Meta dictionary. This unique ID can be used to
            // Track and monitor the event during the handling process through the exposed EventsInProgress property.
            var id = GenerateEventId(e);

            e.SetMetaValue("id", id);

            lock (_EventsInProgress)
            {
                _EventsInProgress.Add(id, e);
            }

            // Added try/finally block to ensure that events that throw an exception are still removed from the EventsInProgress
            // list. A try block without the catch will bubble any exceptions thrown.
            try
            {
                // Pass the event over to the ModuleCollection for handling. This keeps things more readable in this class.
                if (_ModuleCollection != null)
                {
                    _ModuleCollection.Handle(e);
                }
            }
            finally
            {
                // Once the event is completed we need to remove it from the EventsInProgress list.
                lock (_EventsInProgress)
                {
                    _EventsInProgress.Remove(id);
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// <see cref="ModuleHost"/> implements <see cref="IEventHandler"/> and exposes its methods so that a <see cref="IModule"/>
        /// instance can check to see if an <see cref="IEvent"/> instance can be handled and pass it to <see cref="ModuleHost.Handle(IEvent)"/>
        /// for handling. It is recommended to always use <see cref="IModuleHost"/> to handle events rather than invoking
        /// <see cref="Module.Handle(IEvent)"/> directly. Invoking <see cref="Module.Handle(IEvent)"/> will create a ghost event
        /// which will bee invisible to the <see cref="IModuleHost"/> and all other modules.
        /// </summary>
        public virtual void Handle(IEvent e)
        {
            // We generate a unique ID for the event and add it to the IEvent.Meta dictionary. This unique ID can be used to
            // Track and monitor the event during the handling process through the exposed EventsInProgress property.
            var id = GenerateEventId(e);

            e.SetMetaValue("id", id);

            lock (_EventsInProgress)
            {
                _EventsInProgress.Add(id, e);
            }

            // Pass the event over to the ModuleCollection for handling. This keeps things more readable in this class.
            _ModuleCollection.Handle(e);

            // Once the event is completed we need to remove it from the EventsInProgress list.
            lock (_EventsInProgress)
            {
                _EventsInProgress.Remove(id);
            }
        }