Example #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)));
            });
        }
Example #2
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);
        }
Example #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));
        }
Example #4
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"));
        }
        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))));
            }
        }
Example #6
0
 public void SaveEvent <TDomainEvent, TAggregateRoot>(TDomainEvent e)
     where TAggregateRoot : AggregateRoot, new()
     where TDomainEvent : DomainEvent <TAggregateRoot>
 {
     _eventStore.Save(Guid.NewGuid(), new[] { _domainEventSerializer.Serialize(e) });
 }