public void Process <TAggregateRoot>(Command <TAggregateRoot> command) where TAggregateRoot : AggregateRoot, new() { var aggregateId = command.AggregateId; var aggregate = _aggregateFactory.Get <TAggregateRoot>(aggregateId); if (aggregate == null) { aggregate = _aggregateFactory.Create <TAggregateRoot>(aggregateId); var eventHistory = new List <DomainEvent>(); if (_eventStore.Exists(aggregateId)) { // Get all events for aggregate here from eventstore eventHistory = _eventStore.Load(aggregateId); } // Load all events aggregate.LoadsFromHistory(eventHistory); } // Execute command command.Execute(aggregate); // Get uncommitted events var uncommittedEvents = aggregate.GetUncommittedChanges(); // Save uncommitted events to eventstore _eventStore.Save(aggregateId, uncommittedEvents); // Clear uncommitted changes aggregate.MarkChangesAsCommitted(); }
public async Task <object[]> Handle(CancelReservation command, CancellationToken cancellationToken) { var reservationId = command.Id; //use factory to get entity info var reservation = await _reservationFactory.Get(reservationId, cancellationToken); //persist and publish await _eventStoreClient.Save(reservation, reservationId); //return events return(new object[] { }); }
public async Task <object[]> Handle(CancelReservation command, CancellationToken cancellationToken) { var reservationId = command.Id; //use factory to get entity info var reservation = await _reservationFactory.Get(reservationId, cancellationToken); //do something on reservation to raise events var events = reservation.Cancel(command); //persist and publish await _eventStoreClient.Save(reservation, reservationId); //return events return(events.ToArray()); }
public async Task <object[]> Handle(AddRoom command, CancellationToken cancellationToken) { var roomId = command.RoomId; //use factory to get entity info var room = await _roomFactory.Get(roomId, cancellationToken); //do something on housekeeping room to raise events var events = room.Add(command); //persist and publish await _eventStoreClient.Save(room, roomId); //return events return(events.ToArray()); }