internal void Receive( AggregateCommandHandler <Customer> handler, RepositoryFake <Customer> repo, IContext context, Customer customer, ID customerID ) { GIVEN["a handler instance and a service provider"] = () => { handler = new AggregateCommandHandler <Customer>(); IServiceProvider sp = new ServiceProviderFake().Register <IRepository <Customer> >( repo = Substitute.ForPartsOf <RepositoryFake <Customer> >()); context = new GenericContext(); context.Set(sp, false); }; When["sending a create message to the aggregate"] = () => Send(customerID = ID.NewID(), new Customer.Create()); Then["the aggregate gets created"] = async() => customer = await repo.Get(customerID); AND["it receives the command"] = () => customer.Commands.Should().BeEmpty(); When["sending a non-create message to the aggregate"] = () => { repo.ClearReceivedCalls(); customer.Commands.Clear(); return(Send(customerID, new Customer.Promote())); }; Then["the aggregate is loaded and saved"] = async() => { await repo.Received().Get(customerID); await repo.Received().Save(customer); }; AND["it receives the command"] = () => customer.Commands.Should().ContainSingle(m => m is Customer.Promote); When["sending a message that does not belong to the aggregate"] = () => { repo.ClearReceivedCalls(); return(Send(customerID, new Order.Ship())); }; THEN["no action is performed"] = () => repo.ReceivedCalls().Should().BeEmpty(); Task Send(ID target, ICommandMessage message) { Maybe <Task> result = handler.Receive(new HandleCommand( new Command(message, target), context)); return(result is Some <Task> t ? (Task)t : Task.CompletedTask); } }
private IReadOnlyDictionary <string, IMessageHandler <ICommandMessage> > InitializeHandlers(IAggregateModel aggregateModel) { var handlersFound = new Dictionary <string, IMessageHandler <ICommandMessage> >(); var aggregateCommandHandler = new AggregateCommandHandler(this); aggregateModel.CommandHandlers.ForEach(kv => { if (kv.Value.Unwrap <ICommandMessageHandlingMember>() .Map(x => x.IsFactoryHandler).OrElse(false)) { handlersFound[kv.Key] = new AggregateConstructorCommandHandler(kv.Value, this); } else { handlersFound[kv.Key] = aggregateCommandHandler; } }); return(handlersFound); }