예제 #1
0
            public When_an_event_is_mapped_as_a_delete()
            {
                Given(() =>
                {
                    var mapBuilder = new EventMapBuilder <ProductCatalogEntry, string, ProjectionContext>();
                    mapBuilder
                    .Map <ProductDiscontinuedEvent>()
                    .AsDeleteOf(e => e.ProductKey)
                    .ThrowingIfMissing();

                    map = mapBuilder.Build(new ProjectorMap <ProductCatalogEntry, string, ProjectionContext>
                    {
                        Delete = (key, context) =>
                        {
                            isDeleted = true;
                            return(Task.FromResult(true));
                        }
                    });
                });

                When(async() =>
                {
                    await map.Handle(
                        new ProductDiscontinuedEvent
                    {
                        ProductKey = "c350E"
                    },
                        new ProjectionContext());
                });
            }
예제 #2
0
            public When_deleting_a_non_existing_event_should_be_handled_manually_from_context()
            {
                Given(() =>
                {
                    var mapBuilder = new EventMapBuilder <ProductCatalogEntry, string, ProjectionContext>();
                    mapBuilder
                    .Map <ProductDiscontinuedEvent>()
                    .AsDeleteOf((e, context) => context.EventHeaders["ProductId"] as string)
                    .HandlingMissesUsing((key, context) =>
                    {
                        missedKey = key;
                    });

                    map = mapBuilder.Build(new ProjectorMap <ProductCatalogEntry, string, ProjectionContext>
                    {
                        Delete = (key, context) => Task.FromResult(false)
                    });
                });

                WhenLater(async() =>
                {
                    await map.Handle(
                        new ProductDiscontinuedEvent
                    {
                        ProductKey = "c350E"
                    },
                        new ProjectionContext()
                    {
                        EventHeaders = new Dictionary <string, object>(1)
                        {
                            { "ProductId", "1234" }
                        }
                    });
                });
            }
예제 #3
0
            public When_deleting_a_non_existing_event_should_be_handled_manually()
            {
                Given(() =>
                {
                    var mapBuilder = new EventMapBuilder <ProductCatalogEntry, string, ProjectionContext>();
                    mapBuilder
                    .Map <ProductDiscontinuedEvent>()
                    .AsDeleteOf(e => e.ProductKey)
                    .HandlingMissesUsing((key, context) =>
                    {
                        missedKey = key;
                    });

                    map = mapBuilder.Build(new ProjectorMap <ProductCatalogEntry, string, ProjectionContext>
                    {
                        Delete = (key, context) => Task.FromResult(false)
                    });
                });

                WhenLater(async() =>
                {
                    await map.Handle(
                        new ProductDiscontinuedEvent
                    {
                        ProductKey = "c350E"
                    },
                        new ProjectionContext());
                });
            }
예제 #4
0
            public When_an_event_is_mapped_as_a_custom_action()
            {
                Given(() =>
                {
                    var mapBuilder = new EventMapBuilder <object>();
                    mapBuilder.Map <ProductDiscontinuedEvent>().As((@event, context) =>
                    {
                        involvedKey = @event.ProductKey;

                        return(Task.FromResult(0));
                    });

                    map = mapBuilder.Build(new ProjectorMap <object>
                    {
                        Custom = (context, projector) => projector()
                    });
                });

                When(async() =>
                {
                    await map.Handle(
                        new ProductDiscontinuedEvent
                    {
                        ProductKey = "c350E"
                    },
                        new object());
                });
            }
            public When_a_condition_is_not_met()
            {
                Given(() =>
                {
                    var mapBuilder = new EventMapBuilder <object>();
                    mapBuilder.Map <ProductAddedToCatalogEvent>()
                    .When(e => e.Category == "Electric")
                    .As((e, ctx) =>
                    {
                        projection.Category = e.Category;

                        return(Task.FromResult(0));
                    });

                    mapBuilder.HandleCustomActionsAs((context, projector) =>
                    {
                        throw new InvalidOperationException("Custom action should not be called.");
                    });

                    map = mapBuilder.Build();
                });

                When(async() =>
                {
                    await map.Handle(
                        new ProductAddedToCatalogEvent
                    {
                        Category   = "Hybrids",
                        ProductKey = "c350E"
                    },
                        new object());
                });
            }
예제 #6
0
            public When_a_condition_is_met()
            {
                Given(() =>
                {
                    var mapBuilder = new EventMapBuilder <object>();
                    mapBuilder
                    .Map <ProductAddedToCatalogEvent>()
                    .When(e => e.Category == "Hybrids")
                    .As((e, ctx) =>
                    {
                        involvedKey = e.ProductKey;
                        return(Task.FromResult(0));
                    });

                    map = mapBuilder.Build(new ProjectorMap <object>
                    {
                        Custom = (context, projector) => projector()
                    });
                });

                When(async() =>
                {
                    await map.Handle(
                        new ProductAddedToCatalogEvent
                    {
                        Category   = "Hybrids",
                        ProductKey = "c350E"
                    },
                        new object());
                });
            }
예제 #7
0
            public When_an_updating_event_should_ignore_missing_projections()
            {
                Given(() =>
                {
                    var mapBuilder = new EventMapBuilder <ProductCatalogEntry, string, ProjectionContext>();

                    mapBuilder
                    .Map <ProductAddedToCatalogEvent>()
                    .AsUpdateOf(e => e.ProductKey)
                    .IgnoringMisses()
                    .Using((p, e, ctx) =>
                    {
                        p.Category = e.Category;
                        return(Task.FromResult(0));
                    });

                    map = mapBuilder.Build(new ProjectorMap <ProductCatalogEntry, string, ProjectionContext>
                    {
                        Update = (key, context, projector, createIfMissing) => Task.FromResult(false)
                    });
                });

                WhenLater(async() =>
                {
                    await map.Handle(
                        new ProductAddedToCatalogEvent
                    {
                        Category   = "Hybrids",
                        ProductKey = "c350E"
                    },
                        new ProjectionContext());
                });
            }
예제 #8
0
        private Task StreamMessageReceived(EventResponse streamMessage)
        {
            var @event = DeserializeJsonEvent(streamMessage);

            _map.Handle(@event, _events);
            return(Task.CompletedTask);
        }
예제 #9
0
        public async Task ProjectEvent(object anEvent, NHibernateProjectionContext context)
        {
            foreach (INHibernateChildProjector child in children)
            {
                await child.ProjectEvent(anEvent, context).ConfigureAwait(false);
            }

            await map.Handle(anEvent, context).ConfigureAwait(false);
        }
예제 #10
0
        private async Task ProjectEvent(object anEvent, ProjectionContext context)
        {
            foreach (var child in _children)
            {
                await child.ProjectEvent(anEvent, context);
            }

            // There is no way to identify the child projector when an exception happens so we don't handle exceptions here.
            await _map.Handle(anEvent, context);
        }
            public When_an_event_is_mapped_as_a_create_if_does_not_exist()
            {
                Given(() =>
                {
                    var mapBuilder = new EventMapBuilder <ProductCatalogEntry, string, ProjectionContext>();

                    mapBuilder
                    .Map <ProductAddedToCatalogEvent>()
                    .AsCreateIfDoesNotExistOf(e => e.ProductKey)
                    .Using((p, e, ctx) =>
                    {
                        p.Category = e.Category;
                        return(Task.FromResult(0));
                    });

                    mapBuilder.HandleProjectionModificationsAs(async(key, context, projector, options) =>
                    {
                        projection = new ProductCatalogEntry
                        {
                            Id = key,
                        };

                        this.options = options;
                        await projector(projection);
                    });

                    mapBuilder.HandleProjectionDeletionsAs((key, context, options) =>
                    {
                        throw new InvalidOperationException("Deletion should not be called.");
                    });

                    mapBuilder.HandleCustomActionsAs((context, projector) =>
                    {
                        throw new InvalidOperationException("Custom action should not be called.");
                    });

                    map = mapBuilder.Build();
                });

                When(async() =>
                {
                    await map.Handle(
                        new ProductAddedToCatalogEvent
                    {
                        Category   = "Hybrids",
                        ProductKey = "c350E"
                    },
                        new ProjectionContext());
                });
            }
            public When_a_condition_is_not_met_on_a_projection()
            {
                Given(() =>
                {
                    var mapBuilder = new EventMapBuilder <ProductCatalogEntry, string, ProjectionContext>();
                    mapBuilder.HandleProjectionModificationsAs(async(key, context, projector, options) =>
                    {
                        projection = new ProductCatalogEntry
                        {
                            Id = key,
                        };

                        await projector(projection);
                    });

                    mapBuilder
                    .Map <ProductAddedToCatalogEvent>()
                    .When(e => e.Category == "Electric")
                    .AsUpdateOf(e => e.ProductKey)
                    .Using((p, e, ctx) =>
                    {
                        p.Category = e.Category;

                        return(Task.FromResult(0));
                    });

                    mapBuilder.HandleProjectionDeletionsAs((key, context, options) =>
                    {
                        throw new InvalidOperationException("Deletion should not be called.");
                    });

                    mapBuilder.HandleCustomActionsAs((context, projector) =>
                    {
                        throw new InvalidOperationException("Custom action should not be called.");
                    });

                    map = mapBuilder.Build();
                });

                When(async() =>
                {
                    await map.Handle(
                        new ProductAddedToCatalogEvent
                    {
                        Category   = "Hybrids",
                        ProductKey = "c350E"
                    },
                        new ProjectionContext());
                });
            }
예제 #13
0
            public When_a_creating_event_should_allow_manual_handling_of_duplicates()
            {
                Given(() =>
                {
                    var mapBuilder = new EventMapBuilder <ProductCatalogEntry, string, ProjectionContext>();

                    mapBuilder
                    .Map <ProductAddedToCatalogEvent>()
                    .AsCreateOf(e => e.ProductKey)
                    .HandlingDuplicatesUsing((duplicate, @event, context) =>
                    {
                        duplicateProjection = existingProjection;
                        return(true);
                    })
                    .Using((p, e, ctx) =>
                    {
                        p.Category = e.Category;
                        return(Task.FromResult(0));
                    });

                    existingProjection = new ProductCatalogEntry
                    {
                        Id       = "c350E",
                        Category = "OldCategory",
                    };

                    map = mapBuilder.Build(new ProjectorMap <ProductCatalogEntry, string, ProjectionContext>
                    {
                        Create = async(key, context, projector, shouldOverwrite) =>
                        {
                            if (shouldOverwrite(existingProjection))
                            {
                                await projector(existingProjection);
                            }
                        }
                    });
                });

                When(async() =>
                {
                    await map.Handle(new ProductAddedToCatalogEvent
                    {
                        Category   = "NewCategory",
                        ProductKey = "c350E"
                    },
                                     new ProjectionContext());
                });
            }
예제 #14
0
            public When_an_updating_event_should_create_a_missing_projection_from_context()
            {
                Given(() =>
                {
                    var mapBuilder = new EventMapBuilder <ProductCatalogEntry, string, ProjectionContext>();

                    mapBuilder
                    .Map <ProductAddedToCatalogEvent>()
                    .AsUpdateOf((e, context) => context.EventHeaders["ProductId"] as string)
                    .CreatingIfMissing()
                    .Using((p, e, ctx) =>
                    {
                        p.Category = e.Category;
                        return(Task.FromResult(0));
                    });

                    map = mapBuilder.Build(new ProjectorMap <ProductCatalogEntry, string, ProjectionContext>
                    {
                        Update = (key, context, projector, createIfMissing) =>
                        {
                            shouldCreate = true;

                            return(Task.FromResult(0));
                        }
                    });
                });

                When(async() =>
                {
                    await map.Handle(
                        new ProductAddedToCatalogEvent
                    {
                        Category   = "Hybrids",
                        ProductKey = "c350E"
                    },
                        new ProjectionContext()
                    {
                        EventHeaders = new Dictionary <string, object>(1)
                        {
                            { "ProductId", "1234" }
                        }
                    });
                });
            }
예제 #15
0
            public When_a_creating_event_must_ignore_an_existing_projection()
            {
                Given(() =>
                {
                    var mapBuilder = new EventMapBuilder <ProductCatalogEntry, string, ProjectionContext>();

                    mapBuilder
                    .Map <ProductAddedToCatalogEvent>()
                    .AsCreateOf(e => e.ProductKey).IgnoringDuplicates()
                    .Using((p, e, ctx) =>
                    {
                        p.Category = e.Category;
                        return(Task.FromResult(0));
                    });

                    existingProjection = new ProductCatalogEntry
                    {
                        Id       = "c350E",
                        Category = "Fosile",
                    };

                    map = mapBuilder.Build(new ProjectorMap <ProductCatalogEntry, string, ProjectionContext>
                    {
                        Create = async(key, context, projector, shouldOverwrite) =>
                        {
                            if (shouldOverwrite(existingProjection))
                            {
                                await projector(existingProjection);
                            }
                        }
                    });
                });

                When(async() =>
                {
                    await map.Handle(
                        new ProductAddedToCatalogEvent
                    {
                        Category   = "Hybrids",
                        ProductKey = "c350E"
                    },
                        new ProjectionContext());
                });
            }
            public When_an_event_is_mapped_as_a_delete_if_exists()
            {
                Given(() =>
                {
                    var mapBuilder = new EventMapBuilder <ProductCatalogEntry, string, ProjectionContext>();
                    mapBuilder.Map <ProductDiscontinuedEvent>().AsDeleteIfExistsOf(e => e.ProductKey);

                    mapBuilder.HandleProjectionDeletionsAs((key, context, options) =>
                    {
                        projection = new ProductCatalogEntry
                        {
                            Id      = key,
                            Deleted = true
                        };

                        this.options = options;
                        return(Task.FromResult(0));
                    });

                    mapBuilder.HandleProjectionModificationsAs((key, context, projector, options) =>
                    {
                        throw new InvalidOperationException("Modification should not be called.");
                    });

                    mapBuilder.HandleCustomActionsAs((context, projector) =>
                    {
                        throw new InvalidOperationException("Custom action should not be called.");
                    });

                    map = mapBuilder.Build();
                });

                When(async() =>
                {
                    await map.Handle(
                        new ProductDiscontinuedEvent
                    {
                        ProductKey = "c350E"
                    },
                        new ProjectionContext());
                });
            }
            public When_an_event_is_mapped_as_a_custom_action_on_a_projection()
            {
                Given(() =>
                {
                    var mapBuilder = new EventMapBuilder <ProductCatalogEntry, string, ProjectionContext>();

                    mapBuilder.HandleCustomActionsAs((context, projector) =>
                    {
                        customActionDecoratorExecuted = true;
                        return(projector());
                    });

                    mapBuilder.HandleProjectionModificationsAs((key, context, projector, options) =>
                    {
                        throw new InvalidOperationException("Modification should not be called.");
                    });

                    mapBuilder.HandleProjectionDeletionsAs((key, context, options) =>
                    {
                        throw new InvalidOperationException("Deletion should not be called.");
                    });

                    mapBuilder.Map <ProductDiscontinuedEvent>().As((@event, context) =>
                    {
                        involvedKey = @event.ProductKey;

                        return(Task.FromResult(0));
                    });

                    map = mapBuilder.Build();
                });

                When(async() =>
                {
                    await map.Handle(
                        new ProductDiscontinuedEvent
                    {
                        ProductKey = "c350E"
                    },
                        new ProjectionContext());
                });
            }
예제 #18
0
            public When_an_updating_event_should_throw_on_misses()
            {
                Given(() =>
                {
                    var mapBuilder = new EventMapBuilder <ProductCatalogEntry, string, ProjectionContext>();
                    mapBuilder.Map <ProductAddedToCatalogEvent>().AsUpdateOf(e => e.ProductKey).Using((p, e, ctx) =>
                    {
                        p.Category = e.Category;

                        return(Task.FromResult(0));
                    });

                    existingProjection = new ProductCatalogEntry
                    {
                        Id       = "c350E",
                        Category = "OldCategory",
                    };

                    map = mapBuilder.Build(new ProjectorMap <ProductCatalogEntry, string, ProjectionContext>
                    {
                        Update = async(key, context, projector, createIfMissing) =>
                        {
                            if (createIfMissing())
                            {
                                await projector(existingProjection);
                            }
                        }
                    });
                });

                WhenLater(async() =>
                {
                    await map.Handle(
                        new ProductAddedToCatalogEvent
                    {
                        ProductKey = "c350E",
                        Category   = "NewCategory"
                    },
                        new ProjectionContext());
                });
            }
예제 #19
0
            public When_a_global_filter_is_not_met()
            {
                Given(() =>
                {
                    var mapBuilder = new EventMapBuilder <object>()
                                     .Where((@event, context) =>
                    {
                        if (@event is ProductAddedToCatalogEvent addedEvent)
                        {
                            return(Task.FromResult(addedEvent.Category == "Electric"));
                        }

                        return(Task.FromResult(true));
                    });

                    mapBuilder
                    .Map <ProductAddedToCatalogEvent>()
                    .As((e, ctx) =>
                    {
                        involvedKey = e.ProductKey;

                        return(Task.FromResult(0));
                    });

                    map = mapBuilder.Build(new ProjectorMap <object>
                    {
                        Custom = (context, projector) => projector()
                    });
                });

                When(async() =>
                {
                    await map.Handle(
                        new ProductAddedToCatalogEvent
                    {
                        Category   = "Hybrids",
                        ProductKey = "c350E"
                    },
                        new object());
                });
            }
예제 #20
0
            public When_event_should_create_a_new_projection_from_context()
            {
                Given(() =>
                {
                    var mapBuilder = new EventMapBuilder <ProductCatalogEntry, string, ProjectionContext>();
                    mapBuilder.Map <ProductAddedToCatalogEvent>().AsCreateOf((e, context) => context.EventHeaders["ProductId"] as string).Using((p, e, ctx) =>
                    {
                        p.Category = e.Category;

                        return(Task.FromResult(0));
                    });

                    map = mapBuilder.Build(new ProjectorMap <ProductCatalogEntry, string, ProjectionContext>
                    {
                        Create = async(key, context, projector, shouldOverwrite) =>
                        {
                            projection = new ProductCatalogEntry
                            {
                                Id = key,
                            };

                            await projector(projection);
                        }
                    });
                });

                When(async() =>
                {
                    await map.Handle(new ProductAddedToCatalogEvent
                    {
                        Category = "Hybrids"
                    },
                                     new ProjectionContext()
                    {
                        EventHeaders = new Dictionary <string, object>(1)
                        {
                            { "ProductId", "1234" }
                        }
                    });
                });
            }
예제 #21
0
            public When_event_should_create_a_new_projection()
            {
                Given(() =>
                {
                    var mapBuilder = new EventMapBuilder <ProductCatalogEntry, string, ProjectionContext>();
                    mapBuilder.Map <ProductAddedToCatalogEvent>().AsCreateOf(e => e.ProductKey).Using((p, e, ctx) =>
                    {
                        p.Category = e.Category;

                        return(Task.FromResult(0));
                    });

                    map = mapBuilder.Build(new ProjectorMap <ProductCatalogEntry, string, ProjectionContext>
                    {
                        Create = async(key, context, projector, shouldOverwrite) =>
                        {
                            projection = new ProductCatalogEntry
                            {
                                Id = key,
                            };

                            await projector(projection);
                        }
                    });
                });

                When(async() =>
                {
                    await map.Handle(new ProductAddedToCatalogEvent
                    {
                        Category   = "Hybrids",
                        ProductKey = "c350E"
                    },
                                     new ProjectionContext());
                });
            }
 public async Task ProjectEvent(object anEvent, EntityFrameworkProjectionContext context)
 {
     context.WasHandled = await _eventMap.Handle(anEvent, context).ConfigureAwait(false);
 }
        private async Task StreamMessageReceived(IStreamSubscription subscription, StreamMessage streamMessage, CancellationToken cancellationToken)
        {
            var @event = await DeserializeJsonEvent(streamMessage, cancellationToken);

            await _map.Handle(@event, Balance);
        }