Ejemplo n.º 1
0
        public void Save <T>(T aggregate, SessionInfo sessionInfo, int?expectedVersion = null) where T : AggregateRoot
        {
            if (expectedVersion != null && _domainEventStore.Get(aggregate.Id, expectedVersion.Value).Any())
            {
                throw new ConcurrencyException(aggregate.Id);
            }

            var changes = aggregate.FlushUncommitedChanges().ToArray();

            if (changes.Any())
            {
                _domainEventStore.Save(changes, sessionInfo);
                _domainEventPublisher.Publish(changes, sessionInfo.TrackingId);
            }
        }
Ejemplo n.º 2
0
        public T Get <T>(Guid aggregateId) where T : AggregateRoot
        {
            var aggregate       = AggregateFactory.CreateAggregate <T>();
            var snapshotVersion = TryRestoreAggregateFromSnapshot(aggregateId, aggregate);

            if (snapshotVersion == -1)
            {
                return(_domainRepository.Get <T>(aggregateId));
            }
            var events = _domainEventStore.Get(aggregateId, snapshotVersion).Where(desc => desc.Version > snapshotVersion);

            aggregate.LoadFromHistory(events);

            return(aggregate);
        }