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(); } }
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); } }
public void ExecuteAll(EventsRepository repository) { foreach (var @event in repository.Events) { ShowEventExecutingMessage(@event); foreach (var action in @event.Actions) { action.Action(); } } }
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); }
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); }