Event Bus subcription code example

Example: Event Bus subcription

public void Subscribe(IEventHandler eventHandler) where TEvent : IEvent
        {
            //Sync lock
            lock (_syncObject)
            {
                //Get the type of domain model
                var eventType = typeof(TEvent);
                //If this domain type has been registered in the event bus
                if (_dicEventHandler.ContainsKey(eventType))
                {
                    var handlers = _dicEventHandler[eventType];
                    if (handlers != null)
                    {
                        handlers.Add(eventHandler);
                    }
                    else
                    {
                        handlers = new List
                        {
                            eventHandler
                        };
                    }
                }
                else
                {
                    _dicEventHandler.Add(eventType, new List { eventHandler });
                }
            }
        }
                    
                
                

Tags:

Misc Example