Example #1
0
        public TAggregateRoot Source <TAggregateRoot, TKey>(TKey aggregateId)
            where TAggregateRoot : IAggregateRoot <TKey>
            where TKey : IEquatable <TKey>
        {
            IEnumerable <Event> events  = Enumerable.Empty <Event>();
            Memento             memento = null;

            var instance = (TAggregateRoot)Activator.CreateInstance(typeof(TAggregateRoot), true);

            //获取该记录的更改快照
            memento = MementoRepository.GetMemento(aggregateId);

            if (memento != null)
            {
                //获取该记录最后一次快照之后的更改,避免加载过多历史更改
                events = GetEvents(aggregateId, memento.Version);
                //从快照恢复
                ((IOriginator)instance).SetMemento(memento);
            }
            else
            {
                //获取所有历史更改记录
                events = GetEvents(aggregateId);
            }

            if (memento == null && !events.Any())
            {
                return(default(TAggregateRoot));
            }

            //重现历史更改
            instance.LoadsFromHistory(events);
            return(instance);
        }
        public TAggregateRoot Source <TAggregateRoot, TKey>(TKey aggregateId)
            where TAggregateRoot : IAggregateRoot <TKey>, new()
            where TKey : IEquatable <TKey>
        {
            IEnumerable <Event> events  = Enumerable.Empty <Event>();
            Memento             memento = null;
            var obj = new TAggregateRoot();

            //获取该记录的更改快照
            memento = MementoRepository.GetMemento(aggregateId);

            if (memento != null)
            {
                //获取该记录最后一次快照之后的更改,避免加载过多历史更改
                events = GetEvents(aggregateId, memento.Version);
                //从快照恢复
                ((IOriginator)obj).SetMemento(memento);
            }
            else
            {
                //获取所有历史更改记录
                events = GetEvents(aggregateId);
            }

            if (memento == null && !events.Any())
            {
                return(default(TAggregateRoot));
            }

            //重现历史更改
            obj.LoadsFromHistory(events);
            return(obj);
        }