private TAggregateRoot GetById <TAggregateRoot>(Guid Id) where TAggregateRoot : AggregateRoot, new() { //获取该记录的所有缓存事件 IEnumerable <Event> events; Memento memento = null; var obj = new TAggregateRoot(); if (eventStorage is IEventOriginator) { //获取该记录的更改快照 memento = ((IEventOriginator)eventStorage).GetMemento <Memento>(Id); } if (memento != null) { //获取该记录最后一次快照之后的更改,避免加载过多历史更改 events = eventStorage.GetEvents(Id).Where(x => x.Version >= memento.Version); //从快照恢复 ((IOriginator)obj).SetMemento(memento); } else { //获取所有历史更改记录 events = eventStorage.GetEvents(Id); } if (memento == null && !events.Any()) { return(null); } //重现历史更改 obj.LoadsFromHistory(events); return(obj); }
public T GetById(Guid id) { IEnumerable <DomainEvent> events; // 从快照中查询最近发生一次领域事件 var snapshot = _storage.GetSnapshot <BaseSnapshot>(id); if (snapshot != null) { // 如果快照存在,重建过程则从快照事件之后的事件开始重建,而不需要每次从最开始的事件进行重建 events = _storage.GetEvents(id).Where(e => e.Version >= snapshot.Version); } else { // 从不存在则还是需要从最开始的对象开始重建 events = _storage.GetEvents(id); } var obj = new T(); if (snapshot != null) { ((ISnapshotOrignator)obj).BuildFromSnapshot(snapshot); // 先应用快照中领域事件 } obj.LoadsFromHistory(events); // 逐个应用领域事件来重建对象 return(obj); }
public T GetById(Guid id) { //获取该记录的所有缓存事件 IEnumerable <Event> events; Memento memento = null; var obj = new T(); if (_eventStorage is IEventOriginator) { //获取该记录的更改快照 memento = ((IEventOriginator)_eventStorage).GetMemento <Memento>(id); } if (memento != null) { //获取该记录最后一次快照之后的更改,避免加载过多历史更改 events = _eventStorage.GetEvents(id).Where(x => x.Version >= memento.Version); //从快照恢复 ((IOriginator)obj).SetMemento(memento); } else { //获取所有历史更改记录 events = _eventStorage.GetEvents(id); } //重现历史更改 obj.LoadsFromHistory(events); return(obj); }
public async Task <T> GetById(int id) { IEnumerable <Event> events; var memento = await _storage.GetMemento <BaseMemento>(id).ConfigureAwait(false); if (memento != null) { var list = await _storage.GetEvents(id).ConfigureAwait(false); events = list.Where(e => e.Version >= memento.Version); } else { events = await _storage.GetEvents(id).ConfigureAwait(false); } var obj = new T(); if (memento != null) { ((IOriginator)obj).SetMemento(memento); } await Task.Run(() => obj.LoadsFromHistory(events)).ConfigureAwait(false); return(obj); }
public T GetById(Guid id) { IEnumerable <Event> events; var memento = _eventStorage.GetMemento <BaseMemento>(id); if (memento != null) { events = _eventStorage.GetEvents(id).Where(e => e.Version >= memento.Version); } else { events = _eventStorage.GetEvents(id); } var obj = new T(); if (memento != null) { ((IOriginator)obj).SetMemento(memento); } obj.LoadsFromHistory(events); return(obj); }
public T GetById <T>(Guid id) where T : AggregateRoot, new() { IEnumerable <DomainEvent> events; events = _eventStorage.GetEvents(id); var obj = new T(); if (events.Count() > 0) { obj.LoadsFromHistory(events); } return(obj); }
public TAggregateRoot GetById(Guid id) { IEnumerable <IEvent> events; var memento = _eventStorage.GetMemento <Memento>(id); if (memento != null) { events = _eventStorage.GetEvents(id).Where(e => e.Version >= memento.Version); } else { events = _eventStorage.GetEvents(id); } var instance = new TAggregateRoot(); if (memento != null) { ((IOriginator)instance).SetMemento(memento); } instance.LoadDataFromHistories(events); return(instance); }
public T GetById(Guid id) { IEnumerable <Event> events; if (_storage == null) { return(new T()); } var memento = _storage.GetMemento <BaseMemento>(id); events = memento != null ? _storage.GetEvents(id).Where(p => p.Version >= memento.Version) : _storage.GetEvents(id); var obj = new T(); if (memento != null) { ((IOriginator)obj).SetMemento(memento); } obj.LoadsFromHistory(events); return(obj); }