示例#1
0
        public void ExecuteEvent(EventsRepository repository, string eventName)
        {
            var requiredEvent = repository.Events.First(e => e.Name == eventName);

            ShowEventExecutingMessage(requiredEvent);
            foreach (var action in requiredEvent.Actions)
            {
                action.Action();
            }
        }
示例#2
0
        public void Unsubscribe(EventsRepository repository, string eventName, Guid actionId)
        {
            var requiredEvent = repository.Events.First(e => e.Name == eventName);

            requiredEvent.Actions.Remove(requiredEvent.Actions.First(e => e.Id == actionId));
            if (!requiredEvent.Actions.Any())
            {
                repository.Events.Remove(requiredEvent);
            }
        }
示例#3
0
 public void ExecuteAll(EventsRepository repository)
 {
     foreach (var @event in repository.Events)
     {
         ShowEventExecutingMessage(@event);
         foreach (var action in @event.Actions)
         {
             action.Action();
         }
     }
 }
示例#4
0
        public Guid Subscribe(EventsRepository repository, string eventName, Action action)
        {
            var newActionId = Guid.NewGuid();

            if (!repository.Events.Exists(e => e.Name == eventName))
            {
                repository.Events.Add(new Event(eventName));
            }

            repository
            .Events
            .First(e => e.Name == eventName)
            .Actions
            .Add(new EventAction(newActionId, action));

            return(newActionId);
        }
示例#5
0
        static void ExecuteLab3()
        {
            var eventsRepository = new EventsRepository();
            var eventBus         = new EventBus();

            eventBus.Subscribe(eventsRepository, "First event",
                               () => { Console.WriteLine("First event is executing."); });
            var eventActionId = eventBus.Subscribe(eventsRepository, "First event",
                                                   () => { Console.WriteLine("Still running..."); });

            eventBus.Subscribe(eventsRepository, "Second event",
                               () => { Console.WriteLine("Second event is executing."); });

            eventBus.ExecuteEvent(eventsRepository, "First event");

            eventBus.Unsubscribe(eventsRepository, "First event", eventActionId);

            eventBus.ExecuteAll(eventsRepository);
        }