Inheritance: Event, ISourcedEvent
Exemplo n.º 1
0
        protected override void OnEventApplied(SourcedEvent appliedEvent)
        {
            if (UnitOfWork.Current == null)
                throw new NoUnitOfWorkAvailableInThisContextException();

            UnitOfWork.Current.RegisterDirtyInstance(this);
        }
Exemplo n.º 2
0
 protected override void OnEventApplied(SourcedEvent appliedEvent)
 {
     if(EventApplied != null)
     {
         EventApplied(this, new EventAppliedArgs(appliedEvent));
     }
 }
Exemplo n.º 3
0
        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 SourcedEvent[]
                             {
                                 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.EventSourceId).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));
        }
Exemplo n.º 4
0
        public void Saving_event_source_should_succeed()
        {
            var targetStore = new RavenDBEventStore(_documentStore);
            var id = Guid.NewGuid();

            int sequenceCounter = 0;

            var events = new SourcedEvent[]
                             {
                                 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.EventSourceId).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);
        }
Exemplo n.º 5
0
        public void Rolling_back_transaction_should_remove_inserted_rows()
        {
            var id = Guid.NewGuid();
            var utcNow = DateTime.UtcNow.Date.AddHours(9).AddTicks(-1);

            var events = new SourcedEvent[]
            {
                new CustomerCreatedEvent(Guid.NewGuid(), id, 0, utcNow, "Foo", 35)
            };

            var eventSource = MockRepository.GenerateMock<IEventSource>();
            eventSource.Stub(e => e.EventSourceId).Return(id);
            eventSource.Stub(e => e.InitialVersion).Return(0);
            eventSource.Stub(e => e.Version).Return(events.Length);
            eventSource.Stub(e => e.GetUncommittedEvents()).Return(events);

            _store.Save(eventSource);
            _transaction.Rollback();

            using (var conn = new SQLiteConnection(_connString))
            {
                conn.Open();

                using (var cmd = new SQLiteCommand("SELECT [TimeStamp] FROM [Events]", conn))
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        Assert.Fail("No rows should exist");
                    }
                }
            }
        }
Exemplo n.º 6
0
        public void Retrieved_event_should_having_identical_timestamp_as_persisted()
        {
            var id = Guid.NewGuid();
            var utcNow = DateTime.UtcNow.Date.AddHours(9).AddTicks(-1);

            var events = new SourcedEvent[]
            {
                new CustomerCreatedEvent(Guid.NewGuid(), id, 0, utcNow, "Foo", 35)
            };

            var eventSource = MockRepository.GenerateMock<IEventSource>();
            eventSource.Stub(e => e.EventSourceId).Return(id);
            eventSource.Stub(e => e.InitialVersion).Return(0);
            eventSource.Stub(e => e.Version).Return(events.Length);
            eventSource.Stub(e => e.GetUncommittedEvents()).Return(events);
            _store.Save(eventSource);

            using (var conn = new SQLiteConnection(_connString))
            {
                conn.Open();

                using (var cmd = new SQLiteCommand("SELECT [TimeStamp] FROM [Events]", conn))
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var timestamp = (long)reader["Timestamp"];
                        timestamp.Should().Be(utcNow.Ticks);
                    }
                }
            }
        }
        public void Retrieving_all_events_should_return_the_same_as_added()
        {
            var targetStore = new MsSqlServerEventStore(connectionString);
            var aggregateId = Guid.NewGuid();
            var entityId = Guid.NewGuid();

            int sequenceCounter = 1;
            var events = new SourcedEvent[]
                             {
                                 new CustomerCreatedEvent(Guid.NewGuid(), aggregateId, sequenceCounter++, DateTime.UtcNow, "Foo",
                                                          35),
                                 new CustomerNameChanged(Guid.NewGuid(), aggregateId, sequenceCounter++, DateTime.UtcNow,
                                                         "Name" + sequenceCounter),
                                 new CustomerNameChanged(Guid.NewGuid(), aggregateId, sequenceCounter++, DateTime.UtcNow,
                                                         "Name" + sequenceCounter),
                                 new CustomerNameChanged(Guid.NewGuid(), aggregateId, sequenceCounter++, DateTime.UtcNow,
                                                         "Name" + sequenceCounter),
                                 new AccountNameChangedEvent(Guid.NewGuid(), aggregateId, entityId, sequenceCounter++, DateTime.UtcNow, "New Account Title" + sequenceCounter)
                             };

            var eventSource = MockRepository.GenerateMock<IEventSource>();
            eventSource.Stub(e => e.EventSourceId).Return(aggregateId);
            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(aggregateId);
            result.Count().Should().Be(events.Length);
            result.First().EventIdentifier.Should().Be(events.First().EventIdentifier);

            var foundEntityId = result.ElementAt(4).As<SourcedEntityEvent>().EntityId;
            foundEntityId.Should().Be(entityId);
        }
Exemplo n.º 8
0
        static void Process(SourcedEvent evnt)
        {
            Thread.Sleep(200);

            Interlocked.Increment(ref _processedEvents);

            Console.WriteLine("Processing event {0} (id {1})", evnt.EventSequence, evnt.EventIdentifier);
            _bus.Publish(evnt);
        }
        /// <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(SourcedEvent evnt)
        {
            Contract.Requires<ArgumentNullException>(evnt != null, "The Event cannot be null.");

            var handled = false;

            if (ShouldHandleThisEventData(evnt))
            {
                _handler(evnt);
                handled = true;
            }

            return handled;
        }
Exemplo n.º 10
0
 public void Save_SmokeTest()
 {
     var sequenceCounter = 0;
     var id=Guid.NewGuid();
     var events = new SourcedEvent[]{
         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)
     };
     var source = MockRepository.GenerateMock<IEventSource>();
     source.Stub(e => e.EventSourceId).Return(id);
     source.Stub(e => e.InitialVersion).Return(0);
     source.Stub(e => e.Version).Return(events.Length);
     source.Stub(e => e.GetUncommittedEvents()).Return(events);
     _store.Save(source);
 }
Exemplo n.º 11
0
 public EventAppliedArgs(SourcedEvent appliedEvent)
 {
     AppliedEvent = appliedEvent;
 }
Exemplo n.º 12
0
 private void OnFoo(SourcedEvent e)
 {
     FooEventHandlerInvokeCount++;
 }
 public void OnDomainEvent(SourcedEvent e1, SourcedEvent e2)
 {
 }
Exemplo n.º 14
0
        private void ValidateHistoricalEvent(SourcedEvent 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);
            }
        }
Exemplo n.º 15
0
        public void Initializing_from_history_should_not_throw_an_exception_when_the_history_was_empty()
        {
            var theAggregate = new MyAggregateRoot();

            IEnumerable<SourcedEvent> history = new SourcedEvent[0];

            theAggregate.InitializeFromHistory(history);
        }
 public new void ApplyEvent(SourcedEvent e)
 {
     base.ApplyEvent(e);
 }
Exemplo n.º 17
0
        private void SaveEvent(SourcedEvent 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("Data", data);
                    cmd.ExecuteNonQuery();
                }
            }
        }
Exemplo n.º 18
0
        protected internal void ApplyEvent(SourcedEvent evnt)
        {
            _uncommittedEvents.Append(evnt);

            HandleEvent(evnt);

            OnEventApplied(evnt);
        }
Exemplo n.º 19
0
        protected virtual void HandleEvent(SourcedEvent 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 static void MyEventHandlerMethod(SourcedEvent e)
 {
 }
 private void CatchAllEventHandler(SourcedEvent e)
 {
     CatchAllEventHandlerInvokeCount++;
 }
 public void MyEventHandlerMethod(SourcedEvent e1, SourcedEvent e2)
 {
 }
Exemplo n.º 23
0
        public bool HandleEvent(SourcedEvent sourcedEvent)
        {
            Contract.Requires<ArgumentNullException>(sourcedEvent != null, "The sourcedEvent cannot be null.");

            return default(bool);
        }
 public static void OnDomainEvent(SourcedEvent e)
 {
 }
Exemplo n.º 25
0
        protected virtual void HandleEvent(SourcedEvent evnt)
        {
            Contract.Requires<ArgumentNullException>(evnt != null, "The Event cannot be null.");
            Boolean handled = false;

            foreach (var handler in _eventHandlers)
            {
                handled |= handler.HandleEvent(evnt);
            }

            if (!handled)
                throw new EventNotHandledException(evnt);
        }
Exemplo n.º 26
0
 protected virtual void OnEventApplied(SourcedEvent appliedEvent)
 {
     // Nothing to do. Allow override from subclassers.
 }
Exemplo n.º 27
0
        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 SourcedEvent[]
                             {
                                 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.EventSourceId).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>();
        }
Exemplo n.º 28
0
 private void ApplyEventFromHistory(SourcedEvent evnt)
 {
     ValidateHistoricalEvent(evnt);
     HandleEvent(evnt);
 }
Exemplo n.º 29
0
 public void OnOrderLineCreated(SourcedEvent evnt)
 {
     _lines.Add(new OrderLine(((OrderLineCreatedEvent)evnt).Value));
 }
Exemplo n.º 30
0
 private void CatchAllHandler(SourcedEvent e)
 {
 }