Esempio n. 1
0
        public bool ShouldTakeMemento(AggregateRoot aggregate)
        {
            if (!IsMementable(aggregate.GetType()))
            {
                return(false);
            }

            var aggregateVersion = aggregate.Version;

            if (aggregateVersion < 2)
            {
                return(false);
            }
            IDomainEventProvider domainEventProvider = (IDomainEventProvider)aggregate;
            var unCommitedChangesCount = domainEventProvider.GetUncommittedChanges().ToList().Count;

            for (var j = 0; j < unCommitedChangesCount; j++)
            {
                if (++aggregateVersion % 3 == 0 && aggregateVersion != 0)
                {
                    return(true);
                }
            }
            return(false);
        }
Esempio n. 2
0
        private Task <T> LoadAggregate(Guid id)
        {
            return(Task.Run(() => {
                var aggregate = new T();
                IDomainEventProvider domainEventProvider = (IDomainEventProvider)aggregate;
                var events = _eventStore.LoadEventsFor(id, -1);

                domainEventProvider.LoadFromHistory(events);
                return aggregate;
            }));
        }
Esempio n. 3
0
        public void Save(T aggregate, int?expectedVersion = null)
        {
            lock (_locker.GetLock(aggregate.Id.ToString()))
            {
                IDomainEventProvider domainEventProvider = (IDomainEventProvider)aggregate;
                var eventList = domainEventProvider.GetUncommittedChanges().ToList();
                eventList.ForEach(e =>
                {
                    if (e.Id == Guid.Empty)
                    {
                        e.Id = aggregate.Id;
                    }
                    if (e.Id == Guid.Empty)
                    {
                        throw new AggregateOrEventMissingIdException(
                            aggregate.GetType(), e.GetType());
                    }
                });

                _eventStore.Save(aggregate.Id, eventList);
                domainEventProvider.MarkChangesAsCommitted();
            }
        }