Inheritance: Event, ISourcedEvent
コード例 #1
0
ファイル: AggregateRoot.cs プロジェクト: abombss/ncqrs
        protected override void OnEventApplied(SourcedEvent appliedEvent)
        {
            if (UnitOfWork.Current == null)
                throw new NoUnitOfWorkAvailableInThisContextException();

            UnitOfWork.Current.RegisterDirtyInstance(this);
        }
コード例 #2
0
ファイル: AggregateRoot.cs プロジェクト: hoghweed/ncqrs
 protected override void OnEventApplied(SourcedEvent appliedEvent)
 {
     if(EventApplied != null)
     {
         EventApplied(this, new EventAppliedArgs(appliedEvent));
     }
 }
コード例 #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));
        }
コード例 #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);
        }
コード例 #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");
                    }
                }
            }
        }
コード例 #6
0
ファイル: SQLiteEventStoreTests.cs プロジェクト: HAXEN/ncqrs
        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);
                    }
                }
            }
        }
コード例 #7
0
        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);
        }
コード例 #8
0
ファイル: Program.cs プロジェクト: VincentSchippefilt/ncqrs
        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);
        }
コード例 #9
0
        /// <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;
        }
コード例 #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);
 }
コード例 #11
0
ファイル: SourcedEventArgs.cs プロジェクト: hoghweed/ncqrs
 public EventAppliedArgs(SourcedEvent appliedEvent)
 {
     AppliedEvent = appliedEvent;
 }
コード例 #12
0
 private void OnFoo(SourcedEvent e)
 {
     FooEventHandlerInvokeCount++;
 }
 public void OnDomainEvent(SourcedEvent e1, SourcedEvent e2)
 {
 }
コード例 #14
0
ファイル: EventSource.cs プロジェクト: graydo64/ncqrs
        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);
            }
        }
コード例 #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);
        }
コード例 #16
0
 public new void ApplyEvent(SourcedEvent e)
 {
     base.ApplyEvent(e);
 }
コード例 #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();
                }
            }
        }
コード例 #18
0
ファイル: EventSource.cs プロジェクト: graydo64/ncqrs
        protected internal void ApplyEvent(SourcedEvent evnt)
        {
            _uncommittedEvents.Append(evnt);

            HandleEvent(evnt);

            OnEventApplied(evnt);
        }
コード例 #19
0
ファイル: EventSource.cs プロジェクト: graydo64/ncqrs
        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)
 {
 }
コード例 #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)
 {
 }
コード例 #25
0
ファイル: EventSource.cs プロジェクト: abombss/ncqrs
        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);
        }
コード例 #26
0
ファイル: EventSource.cs プロジェクト: graydo64/ncqrs
 protected virtual void OnEventApplied(SourcedEvent appliedEvent)
 {
     // Nothing to do. Allow override from subclassers.
 }
コード例 #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>();
        }
コード例 #28
0
ファイル: EventSource.cs プロジェクト: graydo64/ncqrs
 private void ApplyEventFromHistory(SourcedEvent evnt)
 {
     ValidateHistoricalEvent(evnt);
     HandleEvent(evnt);
 }
コード例 #29
0
ファイル: EntityTests.cs プロジェクト: rodolfograve/ncqrs
 public void OnOrderLineCreated(SourcedEvent evnt)
 {
     _lines.Add(new OrderLine(((OrderLineCreatedEvent)evnt).Value));
 }
コード例 #30
0
 private void CatchAllHandler(SourcedEvent e)
 {
 }