Example #1
0
        public async Task <AppendResult> AppendEventAsync <TAggregateId>(IDomainEvent <TAggregateId> @event) where TAggregateId : IAggregateId
        {
            var paritionKey = @event.AggregateId.IdAsString();
            var partition   = new Partition(_table, paritionKey);

            var openResult = await Stream.TryOpenAsync(partition).ConfigureAwait(false);

            var stream = openResult.Found ? openResult.Stream : new Stream(partition);

            if (stream.Version != @event.AggregateVersion)
            {
                throw new EventStoreConcurrencyException();
            }

            try
            {
                var evtId         = EventId.From(@event.EventId.ToString());
                var evtProperties = new
                {
                    Id   = @event.EventId.ToString(),
                    Type = @event.GetType().AssemblyQualifiedName,
                    Data = JsonConvert.SerializeObject(@event)
                };

                var writeResult = await Stream.WriteAsync(stream, new EventData(EventId.From(evtId), EventProperties.From(evtProperties)))
                                  .ConfigureAwait(false);

                return(new AppendResult(writeResult.Events.ToList().Last().Version));
            }
            catch (ConcurrencyConflictException)
            {
                throw new EventStoreConcurrencyException();
            }
        }
 public FakeEvent()
 {
     Id        = EventId.From(Guid.Empty.ToString());
     Subject   = Guid.Empty.ToString();
     Timestamp = DateTimeOffset.MaxValue;
     Version   = 2;
 }
Example #3
0
        public void Supports_value_equality_via_Id(string id1, string id2, bool expectedToBeEqual)
        {
            var ev1 = Event <string> .New("123", EventId.From(id1));

            var ev2 = Event <string> .New("321", EventId.From(id2));

            (ev1 == ev2).Should().Be(expectedToBeEqual);
        }
Example #4
0
        static EventData CreateEvent(string id)
        {
            var properties = new Dictionary <string, EntityProperty>
            {
                { "Type", new EntityProperty("StreamChanged") },
                { "Data", new EntityProperty("{}") }
            };

            return(new EventData(EventId.From(id), EventProperties.From(properties)));
        }
        static EventData Event(int id)
        {
            var properties = new
            {
                Id   = id,
                Type = "<type>",
                Data = "{some}"
            };

            return(new EventData(EventId.From(id.ToString()), EventProperties.From(properties)));
        }
Example #6
0
        internal Event ToEvent(IEventContentTypeResolver eventContentTypeResolver, IEventContentSerializer contentSerializer)
        {
            var ct = new EventContentType(TypeAssemblyName, TypeNamespace, TypeName);
            var t  = eventContentTypeResolver.Resolve(ct);

            return(Event.From(
                       EventId.From(Id),
                       Rill.Sequence.From(Sequence),
                       Rill.Timestamp.From(Timestamp.UtcDateTime),
                       contentSerializer.Deserialize(Content, t)));
        }
        public void WhenDataIsNotNullThenShouldSerializeWithExpectedValueNonGeneric()
        {
            var serializer = new JsonEventDeserializer();

            var result = (FakeEvent)serializer.Deserialize(_json, typeof(FakeEvent));

            result.Should().NotBeNull();
            result.Id.Should().Be(EventId.From(Guid.Parse("5eb0ea02-7dde-4730-bd61-09fcf1065e11").ToString()));
            result.Subject.Should().Be(Guid.Parse("fdfbd5b3-cafb-4376-a3fa-7f74c20a0188").ToString());
            result.Timestamp.Should().Be(DateTimeOffset.MaxValue);
            result.Version.Should().Be(3);
        }
        public EventData BuildEventData <T>(IEvent eventData) where T : IAggregateRoot
        {
            var id = _guidProvider.GenerateGuid();

            var properties = new EventStorageData(
                typeof(T).Name,
                id,
                eventData.GetType().AssemblyQualifiedName,
                JsonConvert.SerializeObject(eventData));

            return(new EventData(EventId.From(id), EventProperties.From(properties)));
        }
Example #9
0
        public override EventId?ReadJson(JsonReader reader, Type objectType, EventId?existingValue, bool hasExistingValue,
                                         JsonSerializer serializer)
        {
            var value = serializer.Deserialize <string>(reader);

            if (value == null)
            {
                return(null);
            }

            return(EventId.From(value));
        }
Example #10
0
        static EventData ToEventData(Event e)
        {
            var id = Guid.NewGuid();

            var properties = new
            {
                Id   = id,
                Type = e.GetType().FullName,
                Data = JsonConvert.SerializeObject(e)
            };

            return(new EventData(EventId.From(id), EventProperties.From(properties)));
        }
Example #11
0
        static EventData ToEventData(object @event)
        {
            var id = Guid.NewGuid().ToString("D");

            var properties = new EventEntity
            {
                Id   = id,
                Type = @event.GetType().FullName,
                Data = JsonConvert.SerializeObject(@event, SerializerSettings)
            };

            return(new EventData(EventId.From(id), EventProperties.From(properties)));
        }
Example #12
0
        static EventData CreateEvent(string id = null)
        {
            var properties = new Dictionary <string, EntityProperty>
            {
                { "Type", new EntityProperty("StreamChanged") },
                { "Data", new EntityProperty("{}") }
            };

            var eventId = id != null
                ? EventId.From(id)
                : EventId.None;

            return(new EventData(eventId, EventProperties.From(properties)));
        }
        static EventData Event(object e)
        {
            var id = Guid.NewGuid();

            var properties = new
            {
                Id   = id,               // id that you specify for Event ctor is used only for duplicate event detection
                Type = e.GetType().Name, // you can include any number of custom properties along with event
                Data = JSON(e),          // you're free to choose any name you like for data property
                Bin  = BSON(e)           // and any storage format: binary, string, whatever (any EdmType)
            };

            return(new EventData(EventId.From(id), EventProperties.From(properties)));
        }
        static EventData Event(object @event, params Include[] includes)
        {
            var id = Guid.NewGuid();

            var properties = new
            {
                Type = @event.GetType().Name,
                Data = JsonConvert.SerializeObject(@event)
            };

            return(new EventData(
                       EventId.From(id),
                       EventProperties.From(properties),
                       EventIncludes.From(includes)));
        }
Example #15
0
        public void When_writing_duplicate_event()
        {
            var partition = new Partition(table, "test-cosmos-dupes");
            var stream    = new Stream(partition);

            partition.InsertEventIdEntities("e1", "e2");
            partition.CaptureContents(contents =>
            {
                var duplicate = new EventData(EventId.From("e2"));

                Assert.ThrowsAsync <DuplicateEventException>(
                    async() => await Stream.WriteAsync(stream, new EventData(EventId.From("e3")), duplicate));

                contents.AssertNothingChanged();
            });
        }
        void SimultaneousWriting()
        {
            var a = Stream.Open(Partition);
            var b = Stream.Open(Partition);

            Stream.Write(a, new EventData(EventId.From("123")));

            try
            {
                Stream.Write(b, new EventData(EventId.From("456")));
            }
            catch (ConcurrencyConflictException)
            {
                Console.WriteLine("Simultaneously writing to the same version of stream will lead to ConcurrencyConflictException");
            }
        }
Example #17
0
        public void When_single_event_along_with_includes_is_over_WATS_max_batch_size_limit()
        {
            var stream = new Stream(partition);

            var includes = Enumerable
                           .Range(1, Api.AzureMaxBatchSize)
                           .Select(i => Include.Insert(new TestEntity(i.ToString())))
                           .ToArray();

            var @event = new EventData(
                EventId.From("offsize"),
                EventIncludes.From(includes)
                );

            Assert.ThrowsAsync <InvalidOperationException>(
                async() => await Stream.WriteAsync(stream, @event));
        }
Example #18
0
        async Task SequentiallyWritingToStreamIgnoringReturnedStreamHeader()
        {
            var stream = await Stream.OpenAsync(Partition);

            var result = await Stream.WriteAsync(stream, new EventData(EventId.From("AAA")));

            // a new stream header is returned after each write, it contains new Etag
            // and it should be used for subsequent operations
            // stream = result.Stream;

            try
            {
                await Stream.WriteAsync(stream, new EventData(EventId.From("BBB")));
            }
            catch (ConcurrencyConflictException)
            {
                Console.WriteLine("Ignoring new stream (header) returned after each Write() operation will lead to ConcurrencyConflictException on subsequent write operation");
            }
        }
        public override void Run()
        {
            var result = Stream.Write(new Stream(Partition), new EventData(EventId.From("42")));

            try
            {
                var events = new[]
                {
                    new EventData(EventId.From("56")),
                    new EventData(EventId.From("42"))  // conflicting (duplicate) event
                };

                Stream.Write(result.Stream, events);
            }
            catch (DuplicateEventException e)
            {
                Console.WriteLine("Duplicate event detection is based on ID of the event.");
                Console.WriteLine("An ID of conflicting event will be reported back as a property of DuplicateEventException.");
                Console.WriteLine("Here the conflicting event is: {0}", e.Id);
                Console.WriteLine("The caller can use this information to remove conflicting event from the batch and retry (or cancel)");
            }
        }
Example #20
0
        public void Can_be_reconstructed()
        {
            var org = Guid.NewGuid();

            (EventId.From(org) == org).Should().BeTrue();
        }
Example #21
0
        public void Can_be_reconstructed_from_guid_string()
        {
            var org = Guid.NewGuid();

            (EventId.From(org.ToString()) == org).Should().BeTrue();
        }
Example #22
0
        public void Can_not_be_created_from_an_empty_guid()
        {
            Action a = () => EventId.From(Guid.Empty);

            a.Should().Throw <ArgumentException>();
        }