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()); }); }
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()); }); }
public static IEventMap <Dictionary <Guid, PaymentModel> > BuildBalanceMap(this EventMapBuilder <Dictionary <Guid, PaymentModel> > eventMapBuilder) { return(eventMapBuilder.Build(new ProjectorMap <Dictionary <Guid, PaymentModel> >() { Custom = (context, projector) => projector() })); }
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()); }); }
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()); }); }
public When_multiple_conditions_are_registered_on_a_projection() { When(() => { action = () => { var mapBuilder = new EventMapBuilder <ProductCatalogEntry, string, ProjectionContext>(); 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.HandleCustomActionsAs((context, projector) => { throw new InvalidOperationException("Custom action should not be called."); }); mapBuilder.Map <ProductAddedToCatalogEvent>() .When(e => e.Category == "Hybrids") .AsUpdateOf(e => e.ProductKey).Using((p, e, ctx) => p.Category = e.Category); mapBuilder.Map <ProductAddedToCatalogEvent>() .When(e => e.Category == "Electrics") .AsDeleteOf(e => e.ProductKey); var map = mapBuilder.Build(); }; }); }
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" } } }); }); }
public static async Task PlayThing() { var mapBuilder = new EventMapBuilder <GiftCardProjectionContext>(); mapBuilder .Map <RedeemedEvent>() .When((evt, context) => { Console.WriteLine("in conditional"); return(Task.FromResult(evt.Credits > 0)); }) .As((evt, context) => { Console.WriteLine("in as"); return(Task.CompletedTask); }); var projectorMap = new ProjectorMap <GiftCardProjectionContext> { Custom = (context, projector) => { Console.WriteLine("in projector"); return(projector()); } }; var map = mapBuilder.Build(projectorMap); var a = await map.Handle(new RedeemedEvent(5), new GiftCardProjectionContext()); Console.ReadLine(); }
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()); }); }
public static IEventMap <Balance> BuildBalanceMap(this EventMapBuilder <Balance> eventMapBuilder) { return(eventMapBuilder.Build(new ProjectorMap <Balance>() { Custom = (context, projector) => projector() })); }
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()); }); }
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()); }); }
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_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" } } }); }); }
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()); }); }
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" } } }); }); }
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()); }); }
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()); }); }
public BalanceProjection(IStreamStore streamStore, StreamId streamId) { var mapBuilder = new EventMapBuilder <Balance>(); mapBuilder.Map <Deposited>().As((deposited, balance) => { balance.Add(deposited.Amount); }); mapBuilder.Map <Withdrawn>().As((withdrawn, balance) => { balance.Subtract(withdrawn.Amount); }); _map = mapBuilder.Build(new ProjectorMap <Balance>() { Custom = (context, projector) => projector() }); streamStore.SubscribeToStream(streamId, null, StreamMessageReceived); }
public When_multiple_conditions_are_registered() { When(() => { action = () => { var mapBuilder = new EventMapBuilder <object>(); mapBuilder.Map <ProductAddedToCatalogEvent>() .When(e => e.Category != "Hybrids") .When(e => e.Category != "Electrics") .As((e, ctx) => {}); mapBuilder.HandleCustomActionsAs((context, projector) => { throw new InvalidOperationException("Custom action should not be called."); }); var map = mapBuilder.Build(); }; }); }
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 static async Task Rubbish(Akka.Configuration.Config config) { var actorSystem = ActorSystem.Create("tessttt", config); var repositoryActor = actorSystem.ActorOf(Props.Create(() => new RepositoryActor <GiftCardProjection, GiftCardProjectionId>()), "repository-actor"); var projectorMap = new ProjectorMap <GiftCardProjection, GiftCardProjectionId, GiftCardProjectionContext> { Create = async(key, context, projector, shouldOverride) => { var projection = new GiftCardProjection() { Id = key }; await projector(projection); repositoryActor.Tell(new Create <GiftCardProjection, GiftCardProjectionId> { Projection = projection }); }, Update = async(key, context, projector, createIfMissing) => { var query = new Read <GiftCardProjectionId> { Key = key }; var projection = await repositoryActor.Ask <GiftCardProjection>(query); await projector(projection); repositoryActor.Tell(new Update <GiftCardProjection, GiftCardProjectionId> { Projection = projection }); }, Delete = (key, context) => { var command = new Delete <GiftCardProjectionId> { Key = key }; repositoryActor.Tell(command); return(Task.FromResult(true)); }, Custom = (context, projector) => projector() }; var eventMap = new EventMapBuilder <GiftCardProjection, GiftCardProjectionId, GiftCardProjectionContext>(); eventMap.Map <DomainEvent <GiftCard, GiftCardId, IssuedEvent> >() .AsCreateOf(x => IProjectionIdExtensions.From(x.AggregateIdentity)) .Using((projection, evt) => { projection.Credits = evt.AggregateEvent.Credits; projection.IsCancelled = false; projection.Issued = evt.Timestamp.UtcDateTime; }); eventMap.Map <DomainEvent <GiftCard, GiftCardId, RedeemedEvent> >() .AsUpdateOf(x => IProjectionIdExtensions.From(x.AggregateIdentity)) .Using((projection, evt) => { projection.Credits -= evt.AggregateEvent.Credits; }); eventMap.Map <DomainEvent <GiftCard, GiftCardId, CancelledEvent> >() .AsUpdateOf(x => IProjectionIdExtensions.From(x.AggregateIdentity)) .Using((projection, evt) => { projection.IsCancelled = false; }); var handler = eventMap.Build(projectorMap); var context2 = new GiftCardProjectionContext(); var mat = ActorMaterializer.Create(actorSystem); await PersistenceQuery .Get(actorSystem) .ReadJournalFor <SqlReadJournal>(SqlReadJournal.Identifier) .CurrentEventsByPersistenceId("giftcard-a8fd515e-d4fb-4bf6-9d7f-67abdd0fdeef", 0, 10) .RunForeach(async x => await handler.Handle(x.Event, context2), mat); }