Пример #1
0
        void SaveEvents(int numberOfCommandsToProcess, string aggregateRootId)
        {
            var eventStore     = new MongoDbEventStore(_database, "Events");
            var serializer     = new JsonDomainEventSerializer();
            var typeNameMapper = new DefaultDomainTypeNameMapper();

            Enumerable.Range(0, numberOfCommandsToProcess)
            .ToList()
            .ForEach(number =>
            {
                var domainEvents = new[]
                {
                    new RootGotNewNumber(number)
                    {
                        Meta =
                        {
                            { DomainEvent.MetadataKeys.AggregateRootId, aggregateRootId                                  },
                            { DomainEvent.MetadataKeys.SequenceNumber,  number.ToString()                                },
                            { DomainEvent.MetadataKeys.TimeUtc,         Time.UtcNow().ToString("u")                      },
                            { DomainEvent.MetadataKeys.Type,            typeNameMapper.GetName(typeof(RootGotNewNumber)) },
                            { DomainEvent.MetadataKeys.Owner,           typeNameMapper.GetName(typeof(Root))             },
                        }
                    }
                };

                eventStore.Save(Guid.NewGuid(), domainEvents.Select(e => serializer.Serialize(e)));
            });
        }
Пример #2
0
        CommandProcessor GetCommandProcessor(bool useCaching)
        {
            var eventStore = new MongoDbEventStore(_database, "events");

            _timeTaker = new TimeTaker
            {
                InnerEventStore = eventStore,
            };

            var serializer = new JsonDomainEventSerializer();

            IAggregateRootRepository aggregateRootRepository = new DefaultAggregateRootRepository(_timeTaker, serializer, _domainTypeNameMapper);

            if (useCaching)
            {
                aggregateRootRepository = new CachingAggregateRootRepositoryDecorator(aggregateRootRepository, new InMemorySnapshotCache { ApproximateMaxNumberOfCacheEntries = 100 }, eventStore, serializer);
            }

            _timeTaker.InnerAggregateRootRepository = aggregateRootRepository;

            var eventDispatcher = new ViewManagerEventDispatcher(_timeTaker, eventStore, serializer, _domainTypeNameMapper);

            var commandProcessor = new CommandProcessor(_timeTaker, _timeTaker, eventDispatcher, serializer, _commandMapper, _domainTypeNameMapper, new Options());

            RegisterForDisposal(commandProcessor);

            return commandProcessor;
        }
Пример #3
0
        public void RoundtripEventWithReadonlyFields()
        {
            var serializer = new JsonDomainEventSerializer("<events>")
                             .AddAliasesFor(typeof(ComplexDomainEvent), typeof(ComplexValue));

            var rootId = Guid.NewGuid();
            var utcNow = DateTime.UtcNow;

            var e = new ComplexDomainEvent("hello there", new ComplexValue(23))
            {
                Meta =
                {
                    { DomainEvent.MetadataKeys.AggregateRootId, rootId.ToString()    },
                    { DomainEvent.MetadataKeys.TimeUtc,         utcNow.ToString("u") }
                }
            };

            var text = serializer.Serialize(e);

            Console.WriteLine(text);

            var roundtrippedEvent = (ComplexDomainEvent)serializer.Deserialize(text);

            //Assert.That(roundtrippedEvent.Meta[DomainEvent.MetadataKeys.AggregateRootId], Is.EqualTo(rootId));
            //Assert.That(roundtrippedEvent.Meta[DomainEvent.MetadataKeys.TimeLocal], Is.EqualTo(utcNow.ToLocalTime()));
            //Assert.That(roundtrippedEvent.Meta[DomainEvent.MetadataKeys.TimeUtc], Is.EqualTo(utcNow));
            Assert.That(roundtrippedEvent.Text, Is.EqualTo("hello there"));
            Assert.That(roundtrippedEvent.Value.Value, Is.EqualTo(23));
        }
Пример #4
0
        public void RoundtripEventWithFloatFields(float value)
        {
            var serializer = new JsonDomainEventSerializer("<events>");
            //  .AddAliasesFor(typeof(NumberDomainEvent), typeof(ComplexValue));

            var rootId = Guid.NewGuid();
            var utcNow = DateTime.UtcNow;

            var e = new DomainEvent <float>()
            {
                Value = value,
                Meta  =
                {
                    { DomainEvent.MetadataKeys.AggregateRootId, rootId.ToString()    },
                    { DomainEvent.MetadataKeys.TimeUtc,         utcNow.ToString("u") }
                }
            };

            var text = serializer.Serialize(e);

            Console.WriteLine(text);

            var roundtrippedEvent = (DomainEvent <float>)serializer.Deserialize(text);

            //Assert.That(roundtrippedEvent.Meta[DomainEvent.MetadataKeys.AggregateRootId], Is.EqualTo(rootId));
            //Assert.That(roundtrippedEvent.Meta[DomainEvent.MetadataKeys.TimeLocal], Is.EqualTo(utcNow.ToLocalTime()));
            //Assert.That(roundtrippedEvent.Meta[DomainEvent.MetadataKeys.TimeUtc], Is.EqualTo(utcNow));
            Assert.AreEqual(value, roundtrippedEvent.Value);
        }
        protected override void DoSetUp()
        {
            var servicePath = TestAzureHelper.GetPath("test");

            _commandProcessor = CommandProcessor.With()
                                .Logging(l => l.UseConsole(minLevel: Logger.Level.Debug))
                                .EventStore(e => e.Register <IEventStore>(c => new InMemoryEventStore()))
                                .EventDispatcher(e => e.UseAzureServiceBusRelayEventDispatcher("cirqus", servicePath, TestAzureHelper.KeyName, TestAzureHelper.SharedAccessKey))
                                .Create();

            RegisterForDisposal(_commandProcessor);

            _eventStoreProxy = new AzureServiceBusRelayEventStoreProxy("cirqus", servicePath, TestAzureHelper.KeyName, TestAzureHelper.SharedAccessKey);

            RegisterForDisposal(_eventStoreProxy);

            _viewManager = new InMemoryViewManager <View>();

            var serializer = new JsonDomainEventSerializer();
            var typeMapper = new DefaultDomainTypeNameMapper();

            var eventDispatcher = new ViewManagerEventDispatcher(new DefaultAggregateRootRepository(_eventStoreProxy, serializer, typeMapper), _eventStoreProxy, serializer, typeMapper);

            RegisterForDisposal(eventDispatcher);

            eventDispatcher.AddViewManager(_viewManager);
            eventDispatcher.Initialize();
        }
Пример #6
0
        static InMemoryUnitOfWork GetUnitOfWork()
        {
            var serializer = new JsonDomainEventSerializer();
            var mapper     = new DefaultDomainTypeNameMapper();
            var eventStore = new InMemoryEventStore(serializer);
            var repository = new DefaultAggregateRootRepository(eventStore, serializer, mapper);

            return(new InMemoryUnitOfWork(repository, mapper));
        }
Пример #7
0
        public void InvokesCreatedHookWhenAggregateRootIsFirstCreated()
        {
            var domainEventSerializer = new JsonDomainEventSerializer();

            _commandProcessor.ProcessCommand(new MakeRootDoSomething("id1"));

            var expectedSequenceOfEvents = new[] { typeof(RootCreated), typeof(RootDidSomething) };
            var actualSequenceOfEvents   = _eventStoreTask.Result.Select(e => domainEventSerializer.Deserialize(e).GetType()).ToArray();

            Assert.That(actualSequenceOfEvents, Is.EqualTo(expectedSequenceOfEvents));
        }
Пример #8
0
        public void DoesNotThrowWhenDisposingUnstartedReplicator()
        {
            // arrange
            var serializer      = new JsonDomainEventSerializer();
            var eventReplicator = new EventReplicator(new InMemoryEventStore(serializer), new InMemoryEventStore(serializer));

            // act
            eventReplicator.Dispose();

            // assert
        }
Пример #9
0
        protected override void DoSetUp()
        {
            _eventStore            = new MongoDbEventStore(MongoHelper.InitializeTestDatabase(), "Events");
            _domainEventSerializer = new JsonDomainEventSerializer();

            _domainTypeNameMapper = new DefaultDomainTypeNameMapper();

            var snapshotCache = new InMemorySnapshotCache();

            _realAggregateRootRepository    = new DefaultAggregateRootRepository(_eventStore, _domainEventSerializer, _domainTypeNameMapper);
            _cachingAggregateRootRepository = new CachingAggregateRootRepositoryDecorator(_realAggregateRootRepository, snapshotCache, _eventStore, _domainEventSerializer);
        }
Пример #10
0
        public void TryReplicating()
        {
            var serializer  = new JsonDomainEventSerializer();
            var source      = new InMemoryEventStore();
            var destination = new InMemoryEventStore();
            var seqNo       = 0;

            Func <string, EventData> getRecognizableEvent = text => serializer.Serialize(new RecognizableEvent(text)
            {
                Meta =
                {
                    { DomainEvent.MetadataKeys.AggregateRootId,      "268DD0C0-529F-4242-9D53-601A88BB1813"     },
                    { DomainEvent.MetadataKeys.SequenceNumber,       (seqNo).ToString(Metadata.NumberCulture)   },
                    { DomainEvent.MetadataKeys.GlobalSequenceNumber, (seqNo++).ToString(Metadata.NumberCulture) },
                }
            });

            // arrange
            using (var eventReplicator = new EventReplicator(source, destination))
            {
                eventReplicator.Start();
                Thread.Sleep(TimeSpan.FromSeconds(2));

                // act
                source.Save(Guid.NewGuid(), new[] { getRecognizableEvent("hello") });
                source.Save(Guid.NewGuid(), new[] { getRecognizableEvent("there") });
                source.Save(Guid.NewGuid(), new[] { getRecognizableEvent("my") });
                source.Save(Guid.NewGuid(), new[] { getRecognizableEvent("friend") });

                Thread.Sleep(TimeSpan.FromSeconds(1));
            }

            // assert
            var greeting = string.Join(" ", destination
                                       .Select(x => serializer.Deserialize(x))
                                       .OfType <RecognizableEvent>()
                                       .Select(e => e.Id));

            Assert.That(greeting, Is.EqualTo("hello there my friend"));
        }
Пример #11
0
        public void TimeStampsCanRoundtripAsTheyShould()
        {
            var someLocalTime = new DateTime(2015, 10, 31, 12, 10, 15, DateTimeKind.Local);
            var someUtcTime   = someLocalTime.ToUniversalTime();

            TimeMachine.FixCurrentTimeTo(someUtcTime);

            var serializer = new JsonDomainEventSerializer();

            var processor = CreateCommandProcessor(config => config
                                                   .EventStore(e => e.RegisterInstance(_eventStore))
                                                   .EventDispatcher(e => e.UseConsoleOutEventDispatcher())
                                                   .Options(o => o.RegisterInstance <IDomainEventSerializer>(serializer)));;

            RegisterForDisposal(processor);

            processor.ProcessCommand(new MakeSomeRootEmitTheEvent("rootid"));

            var domainEvents = _eventStore.Stream().Select(serializer.Deserialize).Single();

            Assert.That(domainEvents.GetUtcTime(), Is.EqualTo(someUtcTime));
        }
        public void IndexSpeedTest(bool useIndexes, int numberOfQueries, int numberOfEvents)
        {
            var sequenceNumbers = new Dictionary <string, long>();
            var serializer      = new JsonDomainEventSerializer();

            try
            {
                var database = MongoHelper.InitializeTestDatabase();
                database.Drop();
                var eventStore = new MongoDbEventStore(database, "events", automaticallyCreateIndexes: useIndexes);

                var random           = new Random(DateTime.Now.GetHashCode());
                var aggregateRootIds = Enumerable.Range(0, 1000).Select(i => i.ToString()).ToArray();

                Func <string, long> getNextSequenceNumber = id => !sequenceNumbers.ContainsKey(id) ? (sequenceNumbers[id] = 0) : ++sequenceNumbers[id];
                Func <string>       randomAggregateRootId = () => aggregateRootIds[random.Next(aggregateRootIds.Length)];

                var events = Enumerable.Range(1, numberOfEvents)
                             .Select(i => Event(getNextSequenceNumber, randomAggregateRootId()))
                             .ToList();

                TakeTime("Insert " + events.Count + " events", () =>
                {
                    foreach (var e in events)
                    {
                        eventStore.Save(Guid.NewGuid(), new[] { serializer.Serialize(e) });
                    }
                });

                TakeTime("Execute " + numberOfQueries + " queries", () => numberOfQueries.Times(() => eventStore.Load(randomAggregateRootId()).ToList()));
            }
            finally
            {
                Console.WriteLine(@"This is how far we got:
{0}", string.Join(Environment.NewLine, sequenceNumbers.Select(kvp => string.Format("    {0}: {1}", kvp.Key, kvp.Value))));
            }
        }
 protected override void DoSetUp()
 {
     _serializer = new JsonDomainEventSerializer();
 }