Exemplo n.º 1
0
        public virtual T GetByID(Guid id)
        {
            T aggregate = aggregate_factory_.CreateAggregate(id);
            ICollection <Event> events = storage_.GetEventsForAggregate(id, serialzer_);

            aggregate.LoadFromHistory(events);
            return(aggregate);
        }
Exemplo n.º 2
0
        public async Task SaveEvents(Guid aggregateId, IEnumerable <Event> events, int expectedVersion)
        {
            List <Event> Events = await _eventStorage.GetEventsForAggregate(aggregateId);

            // try to get event list for given aggregate id
            // otherwise -> create empty list
            if (Events == null || !Events.Any())
            {
                Events = new List <Event>();
            }
            // check whether latest event version matches current aggregate version
            // otherwise -> throw exception
            else if (Events[Events.Count - 1].Version != expectedVersion && expectedVersion != -1)
            {
                throw new ConcurrencyException();
            }
            var i = expectedVersion;

            // iterate through current aggregate events increasing version with each processed event
            List <Event> newEvents = new List <Event>();

            foreach (var @event in events)
            {
                i++;
                @event.Version = i;

                // push event to the event descriptors list for current aggregate
                @event.AggregateId = aggregateId;
                newEvents.Add(@event);
            }

            //Save events to storage
            await _eventStorage.SaveNewEventsForAggregate(aggregateId, newEvents);

            foreach (var @event in events)
            {
                // publish current event to the bus for further processing by subscribers
                await _publisher.Publish(@event);
            }
        }
Exemplo n.º 3
0
 public async Task <List <Event> > GetEventsForAggregate(Guid aggregateId)
 {
     return(await _eventStorage.GetEventsForAggregate(aggregateId));
 }
Exemplo n.º 4
0
 public async Task <Aggregate <TData> > GetAggregateAtLatestVersion(Guid datasetId, Guid aggregateId)
 => (await _eventStorage.GetEventsForAggregate(datasetId, aggregateId))
 .OrderBy(e => e.Version)
 .Aggregate(default(Aggregate <TData>), ApplyEvent);