protected override void OnEventApplied(ISourcedEvent appliedEvent) { if(EventApplied != null) { EventApplied(this, new EventAppliedArgs(appliedEvent)); } }
public byte[] SerializeEvent(ISourcedEvent e) { byte[] content; using (var ms = new MemoryStream()) { serializer.Serialize(e, e.GetType(), ms); content = ms.ToArray(); } byte[] messageContractBuffer; using (var ms = new MemoryStream()) { var name = e.GetType().GetContractName(); var messageContract = new MessageContract(name, content.Length, 0, e.EventIdentifier, e.EventSequence); serializer.Serialize(messageContract, typeof(MessageContract), ms); messageContractBuffer = ms.ToArray(); } using (var ms = new MemoryStream()) { var headerContract = new MessageHeaderContract(messageContractBuffer.Length); headerContract.WriteHeader(ms); ms.Write(messageContractBuffer, 0, messageContractBuffer.Length); ms.Write(content, 0, content.Length); return ms.ToArray(); } }
public void Retrieving_all_events_should_return_the_same_as_added() { var targetStore = new RavenDBEventStore(_documentStore); var id = Guid.NewGuid(); int sequenceCounter = 0; var events = new ISourcedEvent[] { new CustomerCreatedEvent(Guid.NewGuid(), id, sequenceCounter++, DateTime.UtcNow, "Foo", 35), new CustomerNameChanged(Guid.NewGuid(), id, sequenceCounter++, DateTime.UtcNow, "Name" + sequenceCounter), new CustomerNameChanged(Guid.NewGuid(), id, sequenceCounter++, DateTime.UtcNow, "Name" + sequenceCounter), new CustomerNameChanged(Guid.NewGuid(), id, sequenceCounter++, DateTime.UtcNow, "Name" + sequenceCounter) }; var eventSource = MockRepository.GenerateMock<IEventSource>(); eventSource.Stub(e => e.Id).Return(id); eventSource.Stub(e => e.InitialVersion).Return(0); eventSource.Stub(e => e.Version).Return(events.Length); eventSource.Stub(e => e.GetUncommittedEvents()).Return(events); targetStore.Save(eventSource); var result = targetStore.GetAllEvents(id); result.Count().Should().Be(events.Length); result.First().EventIdentifier.Should().Be(events.First().EventIdentifier); Assert.IsTrue(result.SequenceEqual(events)); }
public void Saving_event_source_should_succeed() { var targetStore = new RavenDBEventStore(_documentStore); var id = Guid.NewGuid(); int sequenceCounter = 0; var events = new ISourcedEvent[] { new CustomerCreatedEvent(Guid.NewGuid(), id, sequenceCounter++, DateTime.UtcNow, "Foo", 35), new CustomerNameChanged(Guid.NewGuid(), id, sequenceCounter++, DateTime.UtcNow, "Name" + sequenceCounter), new CustomerNameChanged(Guid.NewGuid(), id, sequenceCounter++, DateTime.UtcNow, "Name" + sequenceCounter), new CustomerNameChanged(Guid.NewGuid(), id, sequenceCounter++, DateTime.UtcNow, "Name" + sequenceCounter) }; var eventSource = MockRepository.GenerateMock<IEventSource>(); eventSource.Stub(e => e.Id).Return(id); eventSource.Stub(e => e.InitialVersion).Return(0); eventSource.Stub(e => e.Version).Return(events.Length); eventSource.Stub(e => e.GetUncommittedEvents()).Return(events); targetStore.Save(eventSource); }
public BusinessBalanceCreatedProjectedNotification(Guid balanceId, ISourcedEvent o) { BalanceId = balanceId; SourceId = o.SourceId; SagaId = o.SagaId; CreatedTime = o.CreatedTime; }
public byte[] SerializeEvent(ISourcedEvent e) { byte[] content; using (var ms = new MemoryStream()) { serializer.Serialize(e, e.GetType(), ms); content = ms.ToArray(); } byte[] messageContractBuffer; using (var ms = new MemoryStream()) { var name = e.GetType().GetContractName(); var messageContract = new MessageContract(name, content.Length, 0, e.EventIdentifier, e.EventSequence); serializer.Serialize(messageContract, typeof(MessageContract), ms); messageContractBuffer = ms.ToArray(); } using (var ms = new MemoryStream()) { var headerContract = new MessageHeaderContract(messageContractBuffer.Length); headerContract.WriteHeader(ms); ms.Write(messageContractBuffer, 0, messageContractBuffer.Length); ms.Write(content, 0, content.Length); return(ms.ToArray()); } }
private void SaveEvent(ISourcedEvent evnt, Guid eventSourceId, SQLiteTransaction transaction) { if (evnt == null || transaction == null) { throw new ArgumentNullException(); } using (var dataStream = new MemoryStream()) { var bag = _converter.Convert(evnt); var formatter = new BinaryFormatter(); formatter.Serialize(dataStream, bag); var data = dataStream.ToArray(); using (var cmd = new SQLiteCommand(Query.InsertNewEventQuery, transaction.Connection)) { cmd.SetTransaction(transaction) .AddParam("Id", eventSourceId) .AddParam("Name", evnt.GetType().FullName) .AddParam("Sequence", evnt.EventSequence) .AddParam("Timestamp", evnt.EventTimeStamp.Ticks) .AddParam("Data", data); cmd.ExecuteNonQuery(); } } }
protected override void OnEventApplied(ISourcedEvent appliedEvent) { if (EventApplied != null) { EventApplied(this, new EventAppliedArgs(appliedEvent)); } }
internal protected void ApplyEvent(ISourcedEvent evnt) { _uncommittedEvents.Append(evnt); HandleEvent(evnt); OnEventApplied(evnt); }
protected override void OnEventApplied(ISourcedEvent appliedEvent) { var callbacks = _eventAppliedCallbacks; foreach(var callback in callbacks) { callback(this, appliedEvent); } }
public static SourcedEventEntity FromEventSource(ISourcedEvent evnt) { Contract.Requires<ArgumentNullException>(evnt != null, "The evnt cannot be null."); return new SourcedEventEntity { RowKey = evnt.EventIdentifier.ToString(), PartitionKey = evnt.EventSourceId.ToString(), Sequence = evnt.EventSequence }; }
protected override void OnEventApplied(ISourcedEvent appliedEvent) { var callbacks = _eventAppliedCallbacks; foreach (var callback in callbacks) { callback(this, appliedEvent); } }
//TODO: add custom saga id mapping protected virtual Guid GetSagaId(TMessage msg) { ISourcedEvent e = msg as ISourcedEvent; if (e != null) { return(e.SagaId); } return(Guid.Empty); }
public void Invoke(object record, ISourcedEvent @event) { var projectionType = typeof(IProjection<,>).MakeGenericType(record.GetType(), @event.GetType()); var projection = _container.Resolve(projectionType); if (projection != null) { var method = projectionType.GetMethod("Project", new[] { record.GetType(), @event.GetType() }); method?.Invoke(projection, new [] { record, @event }); } }
Boolean ISourcedEventHandler.HandleEvent(ISourcedEvent evnt) { Boolean handled = false; if (evnt is TEvent) { handled |= HandleEvent((TEvent)evnt); } return(handled); }
public bool HandleEvent(ISourcedEvent sourcedEvent) { var sourcedEntityEvent = sourcedEvent as SourcedEntityEvent; if (sourcedEntityEvent == null) { return false; } if (sourcedEntityEvent.EntityId != _entityId) { return false; } return _wrappedHandler.HandleEvent(sourcedEvent); }
public bool HandleEvent(ISourcedEvent sourcedEvent) { var sourcedEntityEvent = sourcedEvent as SourcedEntityEvent; if (sourcedEntityEvent == null) { return(false); } if (sourcedEntityEvent.EntityId != _entityId) { return(false); } return(_wrappedHandler.HandleEvent(sourcedEvent)); }
/// <summary> /// Handles the event. /// </summary> /// <param name = "evnttData">The event data to handle. /// <remarks> /// This value should not be <c>null</c>. /// </remarks> /// </param> /// <returns> /// <c>true</c> when the event was handled; otherwise, <c>false</c>. /// <remarks> /// <c>false</c> does not mean that the handling failed, but that the /// handler was not interested in handling this event. /// </remarks> /// </returns> public bool HandleEvent(ISourcedEvent evnt) { Contract.Requires <ArgumentNullException>(evnt != null, "The Event cannot be null."); var handled = false; if (ShouldHandleThisEventData(evnt)) { _handler(evnt); handled = true; } return(handled); }
/// <summary> /// Handles the event. /// </summary> /// <param name = "evnttData">The event data to handle. /// <remarks> /// This value should not be <c>null</c>. /// </remarks> /// </param> /// <returns> /// <c>true</c> when the event was handled; otherwise, <c>false</c>. /// <remarks> /// <c>false</c> does not mean that the handling failed, but that the /// handler was not interested in handling this event. /// </remarks> /// </returns> public bool HandleEvent(ISourcedEvent evnt) { Contract.Requires<ArgumentNullException>(evnt != null, "The Event cannot be null."); var handled = false; if (ShouldHandleThisEventData(evnt)) { _handler(evnt); handled = true; } return handled; }
private static void SetEntityIdIfEntityEvent(StoredEvent <JObject> obj, ISourcedEvent theEvent) { var entityEvent = theEvent as ISourcedEntityEvent; if (entityEvent != null) { var eventAllowingSettingOfEntityId = entityEvent as IAllowSettingEntityId; if (eventAllowingSettingOfEntityId != null) { var entityId = new Guid((string)obj.Data["EntityId"]); eventAllowingSettingOfEntityId.SetEntityId(entityId); } } }
private void ValidateHistoricalEvent(ISourcedEvent evnt) { if (evnt.EventSourceId != EventSourceId) { var message = String.Format("Cannot apply historical event from other event source."); throw new InvalidOperationException(message); } if (evnt.EventSequence != InitialVersion + 1) { var message = String.Format("Cannot apply event with sequence {0}. Since the initial version of the " + "aggregate root is {1}. Only an event with sequence number {2} can be applied.", evnt.EventSequence, InitialVersion, InitialVersion + 1); throw new InvalidOperationException(message); } }
protected virtual void HandleEvent(ISourcedEvent evnt) { Contract.Requires <ArgumentNullException>(evnt != null, "The Event cannot be null."); Boolean handled = false; // Get a copy of the handlers because an event // handler can register a new handler. This will // cause the _eventHandlers list to be modified. // And modification while iterating it not allowed. var handlers = new List <ISourcedEventHandler>(_eventHandlers); foreach (var handler in handlers) { handled |= handler.HandleEvent(evnt); } if (!handled) { throw new EventNotHandledException(evnt); } }
/// <summary> /// Saves the event to the data store. /// </summary> /// <param name="evnt">The event to save.</param> /// <param name="eventSourceId">The id of the event source that owns the event.</param> /// <param name="transaction">The transaction.</param> private void SaveEvent(ISourcedEvent evnt, SqlTransaction transaction) { Contract.Requires <ArgumentNullException>(evnt != null, "The argument evnt could not be null."); Contract.Requires <ArgumentNullException>(transaction != null, "The argument transaction could not be null."); var document = _formatter.Serialize(evnt); var raw = _translator.TranslateToRaw(document); using (var command = new SqlCommand(Queries.InsertNewEventQuery, transaction.Connection)) { command.Transaction = transaction; command.Parameters.AddWithValue("EventId", raw.EventIdentifier); command.Parameters.AddWithValue("TimeStamp", raw.EventTimeStamp); command.Parameters.AddWithValue("EventSourceId", raw.EventSourceId); command.Parameters.AddWithValue("Name", raw.EventName); command.Parameters.AddWithValue("Version", raw.EventVersion.ToString()); command.Parameters.AddWithValue("Sequence", raw.EventSequence); command.Parameters.AddWithValue("Data", raw.Data); command.ExecuteNonQuery(); } }
public StoredEvent <JObject> Serialize(ISourcedEvent theEvent) { var eventName = _typeResolver.EventNameFor(theEvent.GetType()); var data = JObject.FromObject(theEvent, _serializer); StoredEvent <JObject> obj = new StoredEvent <JObject>( theEvent.EventIdentifier, theEvent.EventTimeStamp, eventName, theEvent.EventVersion, theEvent.EventSourceId, theEvent.EventSequence, data); data.Remove("EventIdentifier"); data.Remove("EventTimeStamp"); data.Remove("EventName"); data.Remove("EventVersion"); data.Remove("EventSourceId"); data.Remove("EventSequence"); return(obj); }
private void AggregateRootEventAppliedHandler(AggregateRoot aggregateRoot, ISourcedEvent evnt) { RegisterDirtyInstance(aggregateRoot); }
/// <summary> /// Saves the event to the data store. /// </summary> /// <param name="evnt">The event to save.</param> /// <param name="eventSourceId">The id of the event source that owns the event.</param> /// <param name="transaction">The transaction.</param> private void SaveEvent(ISourcedEvent evnt, SqlTransaction transaction) { Contract.Requires<ArgumentNullException>(evnt != null, "The argument evnt could not be null."); Contract.Requires<ArgumentNullException>(transaction != null, "The argument transaction could not be null."); var document = _formatter.Serialize(evnt); var raw = _translator.TranslateToRaw(document); using (var command = new SqlCommand(Queries.InsertNewEventQuery, transaction.Connection)) { command.Transaction = transaction; command.Parameters.AddWithValue("EventId", raw.EventIdentifier); command.Parameters.AddWithValue("TimeStamp", raw.EventTimeStamp); command.Parameters.AddWithValue("EventSourceId", raw.EventSourceId); command.Parameters.AddWithValue("Name", raw.EventName); command.Parameters.AddWithValue("Version", raw.EventVersion.ToString()); command.Parameters.AddWithValue("Sequence", raw.EventSequence); command.Parameters.AddWithValue("Data", raw.Data); command.ExecuteNonQuery(); } }
public new void ApplyEvent(ISourcedEvent e) { base.ApplyEvent(e); }
private void OnFoo(ISourcedEvent e) { FooEventHandlerInvokeCount++; }
public void Saving_event_source_while_there_is_a_newer_event_source_should_throw_concurency_exception() { var targetStore = new RavenDBEventStore(_documentStore); var id = Guid.NewGuid(); int sequenceCounter = 0; var events = new ISourcedEvent[] { new CustomerCreatedEvent(Guid.NewGuid(), id, sequenceCounter++, DateTime.UtcNow, "Foo", 35), new CustomerNameChanged(Guid.NewGuid(), id, sequenceCounter++, DateTime.UtcNow, "Name" + sequenceCounter) }; var eventSource = MockRepository.GenerateMock<IEventSource>(); eventSource.Stub(e => e.Id).Return(id).Repeat.Any(); eventSource.Stub(e => e.InitialVersion).Return(0).Repeat.Any(); eventSource.Stub(e => e.Version).Return(events.Length).Repeat.Any(); eventSource.Stub(e => e.GetUncommittedEvents()).Return(events).Repeat.Any(); targetStore.Save(eventSource); Action act = () => targetStore.Save(eventSource); act.ShouldThrow<ConcurrencyException>(); }
public bool HandleEvent(ISourcedEvent sourcedEvent) { Contract.Requires <ArgumentNullException>(sourcedEvent != null, "The sourcedEvent cannot be null."); return(default(bool)); }
public EventAppliedArgs(ISourcedEvent appliedEvent) { AppliedEvent = appliedEvent; }
protected internal void ApplyEvent(ISourcedEvent evnt) { _uncommittedEvents.Append(evnt); HandleEvent(evnt); OnEventApplied(evnt); }
public StoredEvent <T> Serialize(ISourcedEvent theEvent) { Contract.Requires <ArgumentNullException>(theEvent != null, "theEvent"); Contract.Ensures(Contract.Result <StoredEvent <T> >() != null); return(default(StoredEvent <T>)); }
private void AggregateRootEventAppliedHandler(AggregateRoot aggregateRoot, ISourcedEvent evnt) { _events.Add(evnt); }
/// <summary> /// Saves the event to the data store. /// </summary> /// <param name="evnt">The event to save.</param> /// <param name="eventSourceId">The id of the event source that owns the event.</param> /// <param name="transaction">The transaction.</param> private void SaveEvent(ISourcedEvent evnt, OleDbTransaction transaction) { Contract.Requires<ArgumentNullException>(evnt != null, "The argument evnt could not be null."); Contract.Requires<ArgumentNullException>(transaction != null, "The argument transaction could not be null."); var document = _formatter.Serialize(evnt); var raw = _translator.TranslateToRaw(document); var sql = String.Format(AccessQueries.InsertNewEventQuery, raw.EventIdentifier, raw.EventSourceId, raw.EventName, raw.EventVersion, raw.Data, raw.EventSequence, raw.EventTimeStamp); var command = new OleDbCommand(sql, transaction.Connection); command.Transaction = transaction; command.ExecuteNonQuery(); }
protected virtual void HandleEvent(ISourcedEvent evnt) { Contract.Requires<ArgumentNullException>(evnt != null, "The Event cannot be null."); Boolean handled = false; // Get a copy of the handlers because an event // handler can register a new handler. This will // cause the _eventHandlers list to be modified. // And modification while iterating it not allowed. var handlers = new List<ISourcedEventHandler>(_eventHandlers); foreach (var handler in handlers) { handled |= handler.HandleEvent(evnt); } if (!handled) throw new EventNotHandledException(evnt); }
public bool HandleEvent(ISourcedEvent sourcedEvent) { Contract.Requires<ArgumentNullException>(sourcedEvent != null, "The sourcedEvent cannot be null."); return default(bool); }
protected virtual void OnEventApplied(ISourcedEvent appliedEvent) { // Nothing to do. Allow override from subclassers. }
private void ApplyEventFromHistory(ISourcedEvent evnt) { ValidateHistoricalEvent(evnt); HandleEvent(evnt); }
/// <summary> /// Saves the event to the data store. /// </summary> /// <param name="evnt">The event to save.</param> /// <param name="eventSourceId">The id of the event source that owns the event.</param> /// <param name="transaction">The transaction.</param> private static void SaveEvent(ISourcedEvent evnt, Guid eventSourceId, SqlTransaction transaction) { Contract.Requires<ArgumentNullException>(evnt != null, "The argument evnt could not be null."); Contract.Requires<ArgumentNullException>(transaction != null, "The argument transaction could not be null."); using (var dataStream = new MemoryStream()) { var formatter = new BinaryFormatter(); formatter.Serialize(dataStream, evnt); byte[] data = dataStream.ToArray(); using (var command = new SqlCommand(InsertNewEventQuery, transaction.Connection)) { command.Transaction = transaction; command.Parameters.AddWithValue("Id", eventSourceId); command.Parameters.AddWithValue("Name", evnt.GetType().FullName); command.Parameters.AddWithValue("Sequence", evnt.EventSequence); command.Parameters.AddWithValue("Data", data); command.ExecuteNonQuery(); } } }
/// <summary> /// Saves the event to the data store. /// </summary> /// <param name="evnt">The event to save.</param> /// <param name="eventSourceId">The id of the event source that owns the event.</param> /// <param name="transaction">The transaction.</param> private void SaveEvent(ISourcedEvent evnt, SqlTransaction transaction) { Contract.Requires<ArgumentNullException>(evnt != null, "The argument evnt could not be null."); Contract.Requires<ArgumentNullException>(transaction != null, "The argument transaction could not be null."); var bag = _converter.Convert(evnt); byte[] data = ToBinary(bag); using (var command = new SqlCommand(InsertNewEventQuery, transaction.Connection)) { command.Transaction = transaction; command.Parameters.AddWithValue("EventId", evnt.EventIdentifier); command.Parameters.AddWithValue("EventSourceId", evnt.EventSourceId); command.Parameters.AddWithValue("Name", evnt.GetType().FullName); command.Parameters.AddWithValue("Sequence", evnt.EventSequence); command.Parameters.AddWithValue("Data", data); command.ExecuteNonQuery(); } }
private void SaveEvent(ISourcedEvent evnt, Guid eventSourceId, SQLiteTransaction transaction) { if (evnt == null || transaction == null) throw new ArgumentNullException(); using (var dataStream = new MemoryStream()) { var bag = _converter.Convert(evnt); var formatter = new BinaryFormatter(); formatter.Serialize(dataStream, bag); var data = dataStream.ToArray(); using (var cmd = new SQLiteCommand(Query.InsertNewEventQuery, transaction.Connection)) { cmd.SetTransaction(transaction) .AddParam("Id", eventSourceId) .AddParam("Name", evnt.GetType().FullName) .AddParam("Sequence", evnt.EventSequence) .AddParam("Timestamp", evnt.EventTimeStamp.Ticks) .AddParam("Data", data); cmd.ExecuteNonQuery(); } } }