public IEventContext <IEvent> CreateContext(Entities.Event dbEvent)
        {
            if (dbEvent == null)
            {
                throw new ArgumentNullException(nameof(dbEvent));
            }

            if (!_eventTypeCache.TryGet(dbEvent.Type, out var type))
            {
                throw new ArgumentException($"Could not find event type for '{dbEvent.Type}'");
            }

            var @event = (IEvent)_eventDeserializer.Deserialize(dbEvent.Data, type);

            if (_cache.TryGetValue(type, out var activator))
            {
                return(activator(dbEvent.StreamId, @event, dbEvent.CorrelationId, dbEvent.CausationId, @event.Timestamp, Actor.From(dbEvent.Actor)));
            }

            activator = BuildActivator(typeof(EventContext <>).MakeGenericType(type));

            _cache.TryAdd(type, activator);

            var correlationId = dbEvent.CorrelationId != null?CorrelationId.From(dbEvent.CorrelationId) : (CorrelationId?)null;

            var causationId = dbEvent.CausationId != null?CausationId.From(dbEvent.CorrelationId) : (CausationId?)null;

            return(activator(dbEvent.StreamId, @event, correlationId, causationId, @event.Timestamp, Actor.From(dbEvent.Actor)));
        }
예제 #2
0
        public static void LambdaAccessor_throws_null_lambda()
        {
            // Act
            void Act() => CorrelationId.From((Func <string>)null);

            // Assert
            Assert.Throws <ArgumentNullException>(Act);
        }
예제 #3
0
        public static void CorrelationId_From_lambda_returns_value()
        {
            // Act
            ICorrelationIdAccessor accessor = CorrelationId.From(() => Guid.NewGuid().ToString("D", CultureInfo.InvariantCulture));

            // Assert
            Assert.True(Guid.TryParse(accessor.CorrelationId, out _));
        }
        public IEventContext <IEvent> CreateContext(ReceivedMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            if (!_eventTypeCache.TryGet(message.RoutingKey, out var type))
            {
                throw new ArgumentException($"Could not find event type for '{message.RoutingKey}'");
            }

            var eventData = Encoding.UTF8.GetString(message.Body);
            var @event    = (IEvent)_eventDeserializer.Deserialize(eventData, type);

            string        streamId      = null;
            CorrelationId?correlationId = null;
            CausationId?  causationId   = null;
            string        actor         = null;

            if (message.BasicProperties.Headers != null)
            {
                if (message.BasicProperties.Headers.ContainsKey(nameof(IEventContext <IEvent> .StreamId)))
                {
                    streamId = message.BasicProperties.Headers[nameof(IEventContext <IEvent> .StreamId)]?.ToString();
                }

                if (message.BasicProperties.Headers.ContainsKey(nameof(IEventContext <IEvent> .CorrelationId)))
                {
                    var value = message.BasicProperties.Headers[nameof(IEventContext <IEvent> .CorrelationId)]?.ToString();
                    correlationId = value != null?CorrelationId.From(value) : (CorrelationId?)null;
                }

                if (message.BasicProperties.Headers.ContainsKey(nameof(IEventContext <IEvent> .CausationId)))
                {
                    var value = message.BasicProperties.Headers[nameof(IEventContext <IEvent> .CausationId)]?.ToString();
                    causationId = value != null?CausationId.From(value) : (CausationId?)null;
                }

                if (message.BasicProperties.Headers.ContainsKey(nameof(IEventContext <IEvent> .Actor)))
                {
                    actor = message.BasicProperties.Headers[nameof(IEventContext <IEvent> .Actor)]?.ToString();
                }
            }

            if (_cache.TryGetValue(type, out var activator))
            {
                return(activator(streamId, @event, correlationId, causationId, @event.Timestamp, Actor.From(actor ?? "<Unknown>")));
            }

            activator = BuildActivator(typeof(EventContext <>).MakeGenericType(type));

            _cache.TryAdd(type, activator);

            return(activator(streamId, @event, correlationId, causationId, @event.Timestamp, Actor.From(actor ?? "<Unknown>")));
        }
예제 #5
0
        public static void CorrelationId_From_string_returns_value(string expected)
        {
            // Arrange
            ICorrelationIdAccessor accessor = CorrelationId.From(expected);

            // Act
            var actual = accessor.CorrelationId;

            // Assert
            Assert.Equal(expected, actual);
        }
예제 #6
0
        public override CorrelationId?ReadJson(JsonReader reader, Type objectType, CorrelationId?existingValue, bool hasExistingValue,
                                               JsonSerializer serializer)
        {
            var value = serializer.Deserialize <string>(reader);

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

            return(CorrelationId.From(value));
        }
예제 #7
0
        public static void CorrelationId_From_guid_returns_value()
        {
            // Arrange
            var expected = Guid.NewGuid();
            ICorrelationIdAccessor accessor = CorrelationId.From(expected);

            // Act
            var actual = Guid.Parse(accessor.CorrelationId);

            // Assert
            Assert.Equal(expected, actual);
        }
        public void WhenDataIsNotNullThenShouldSerializeWithExpectedValueNonGeneric()
        {
            var serializer = new JsonQueryDeserializer();

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

            result.Should().NotBeNull();
            result.Id.Should().Be(QueryId.From(Guid.Empty.ToString()));
            result.CorrelationId.Should().Be(CorrelationId.From(Guid.Empty.ToString()));
            result.Timestamp.Should().Be(DateTimeOffset.MaxValue);
            result.Actor.Should().Be(Actor.From("User"));
        }
        public void WhenDataIsNotNullThenShouldSerializeWithExpectedValueNonGeneric()
        {
            var serializer = new JsonCommandDeserializer();

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

            result.Should().NotBeNull();
            result.Id.Should().Be(CommandId.From(Guid.Empty.ToString()));
            result.Subject.Should().Be(Guid.Empty.ToString());
            result.CorrelationId.Should().Be(CorrelationId.From(Guid.Empty.ToString()));
            result.Timestamp.Should().Be(DateTimeOffset.MaxValue);
            result.Version.Should().Be(3);
            result.Actor.Should().BeEquivalentTo(Actor.From("User"));
        }