public IEnumerable<AggregateRootEvent> Mutate(AggregateRootEvent @event) { Contract.Assert(_aggregateId == @event.AggregateRootId); if (_eventMigrators.Length == 0) { return Seq.Create(@event); } @event.AggregateRootVersion = _aggregateVersion; _eventModifier.Reset(@event); for(var index = 0; index < _eventMigrators.Length; index++) { if (_eventModifier.Events == null) { _eventMigrators[index].MigrateEvent(@event, _eventModifier); } else { var node = _eventModifier.Events.First; while (node != null) { _eventModifier.MoveTo(node); _eventMigrators[index].MigrateEvent(_eventModifier.Event, _eventModifier); node = node.Next; } } } var newHistory = _eventModifier.MutatedHistory; _aggregateVersion += newHistory.Length; return newHistory; }
public Event(AggregateRootEvent aggregateRootEvent) { Id = Guid.NewGuid().ToString(); EventType = aggregateRootEvent.GetType().ToString(); EventData = aggregateRootEvent.ToJson(); CreateTime = DateTime.Now; }
public IEnumerable <AggregateRootEvent> Mutate(AggregateRootEvent @event) { Contract.Assert(_aggregateId == @event.AggregateRootId); if (_eventMigrators.Length == 0) { return(Seq.Create(@event)); } @event.AggregateRootVersion = _aggregateVersion; _eventModifier.Reset(@event); for (var index = 0; index < _eventMigrators.Length; index++) { if (_eventModifier.Events == null) { _eventMigrators[index].MigrateEvent(@event, _eventModifier); } else { var node = _eventModifier.Events.First; while (node != null) { _eventModifier.MoveTo(node); _eventMigrators[index].MigrateEvent(_eventModifier.Event, _eventModifier); node = node.Next; } } } var newHistory = _eventModifier.MutatedHistory; _aggregateVersion += newHistory.Length; return(newHistory); }
public void MoveTo(LinkedListNode <AggregateRootEvent> current) { if (current.Value is EndOfAggregateHistoryEventPlaceHolder && !(Event is EndOfAggregateHistoryEventPlaceHolder)) { _lastEventInActualStream = Event; } CurrentNode = current; _insertedEvents = null; _replacementEvents = null; }
public void Reset(AggregateRootEvent @event) { if (@event is EndOfAggregateHistoryEventPlaceHolder && !(Event is EndOfAggregateHistoryEventPlaceHolder)) { _lastEventInActualStream = Event; } Event = @event; Events = null; _insertedEvents = null; _replacementEvents = null; }
public void Reset(AggregateRootEvent @event) { if(@event is EndOfAggregateHistoryEventPlaceHolder && !(Event is EndOfAggregateHistoryEventPlaceHolder)) { _lastEventInActualStream = Event; } Event = @event; Events = null; _insertedEvents = null; _replacementEvents = null; }
/// <summary> /// 向数据库中插入一个新的事件 /// </summary> protected virtual void PersistNewEvent(AggregateRootEvent evnt, long version) { evnt.Version = version; evnt.AggregateRootName = GetAggregateRootName(); evnt.Name = GetEventName(evnt.Event.GetType()); evnt.Data = SerializeEvent(evnt.Event); OpenSession().Save(evnt); if (_logger.IsDebugEnabled) { string eventDetail = string.Format( "AggregateRootName: {0}, aggregateRootId: {1}, Version: {2}, OccurredTime: {3}, EventName: {4}, EventData: {5}", evnt.AggregateRootName, evnt.AggregateRootId, evnt.Version, evnt.OccurredTime, evnt.Name, evnt.Data); _logger.DebugFormat("Inserted AggregateRootEvent. event detail:{0}", eventDetail); } }
private void SaveEventsWithinReadOrderRange(AggregateRootEvent[] newEvents, SqlDecimal rangeStart, SqlDecimal rangeEnd) { var increment = (rangeEnd - rangeStart)/(newEvents.Length + 1); IReadOnlyList<EventWithManualReadorder> eventsToPersist = newEvents.Select( (@event, index) => new EventWithManualReadorder { Event = @event, ManualReadOrder = rangeStart + (index + 1)*increment }) .ToList(); SaveEventsInternal(eventsToPersist); }
private static bool IsRefactoringEvent(AggregateRootEvent @event) { return @event.InsertBefore.HasValue || @event.InsertAfter.HasValue || @event.Replaces.HasValue; }
public static void AssertMigrationsAreIdempotent(IReadOnlyList<IEventMigration> eventMigrations, AggregateRootEvent[] @events) { var creationEvent = events.First(); var migrators = eventMigrations .Where(migration => migration.MigratedAggregateEventHierarchyRootInterface.IsInstanceOfType(creationEvent)) .Select(migration => migration.CreateSingleAggregateInstanceHandlingMigrator()) .ToArray(); for(var eventIndex = 0; eventIndex < events.Length; eventIndex++) { var @event = events[eventIndex]; for(var migratorIndex = 0; migratorIndex < migrators.Length; migratorIndex++) { migrators[migratorIndex].MigrateEvent(@event, AssertMigrationsAreIdempotentEventModifier.Instance); } } }
public EventModifier(AggregateRootEvent @event, Action <IReadOnlyList <AggregateRootEvent> > eventsAddedCallback) { Event = @event; _eventsAddedCallback = eventsAddedCallback; }
public void AddEvent(AggregateRootEvent @event) { _events.Add(@event); }
public void MoveTo(LinkedListNode<AggregateRootEvent> current) { if (current.Value is EndOfAggregateHistoryEventPlaceHolder && !(Event is EndOfAggregateHistoryEventPlaceHolder)) { _lastEventInActualStream = Event; } CurrentNode = current; _insertedEvents = null; _replacementEvents = null; }
public EventModifier(AggregateRootEvent @event, Action<IReadOnlyList<AggregateRootEvent>> eventsAddedCallback) { Event = @event; _eventsAddedCallback = eventsAddedCallback; }
private static bool IsRefactoringEvent(AggregateRootEvent @event) { return(@event.InsertBefore.HasValue || @event.InsertAfter.HasValue || @event.Replaces.HasValue); }