public void HaveConsequences(CustomerAccount.RequestedNoSpam @event) { var customer = customerRepository.GetLatest(@event.AggregateId).Result; customer.Apply(new ChangeEmailAddress { NewEmailAddress = "*****@*****.**" }); customerRepository.Save(customer).Wait(); }
public async Task When_a_scheduled_command_fails_validation_then_a_failure_event_can_be_recorded_in_HandleScheduledCommandException_method() { // arrange var order = CreateOrder(customerAccountId: (await customerRepository.GetLatest(customerAccountId)).Id); // by the time Ship is applied, it will fail because of the cancellation order.Apply(new ShipOn(shipDate: Clock.Now().AddMonths(1).Date)); order.Apply(new Cancel()); await orderRepository.Save(order); // act VirtualClock.Current.AdvanceBy(TimeSpan.FromDays(32)); //assert order = await orderRepository.GetLatest(order.Id); var lastEvent = order.Events().Last(); lastEvent.Should().BeOfType <Order.ShipmentCancelled>(); }
public TAggregate GetAggregate(Guid id) { var aggregate = repository.GetLatest(id); if (aggregate == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } return(aggregate); }
public async Task If_renamed_and_saved_When_the_aggregate_is_sourced_then_the_event_is_the_new_name() { var order = await repository.GetLatest(aggregateId); var rename = new EventMigrator.Rename(order.EventHistory.Last().SequenceNumber, "ItemAdded2"); await EventMigrator.SaveWithRenames(MigratableRepository, order, new[] { rename }); (await repository.GetLatest(aggregateId)).EventHistory.Last().Should().BeOfType <Order.ItemAdded2>(); }
public static async Task <ScheduledCommandResult> ApplyScheduledCommand <TAggregate>( this IEventSourcedRepository <TAggregate> repository, IScheduledCommand <TAggregate> scheduled, Func <Task <bool> > verifyPrecondition = null) where TAggregate : class, IEventSourced { TAggregate aggregate = null; Exception exception = null; try { if (verifyPrecondition != null && !await verifyPrecondition()) { return(await FailScheduledCommand(repository, scheduled, new PreconditionNotMetException())); } aggregate = await repository.GetLatest(scheduled.AggregateId); if (aggregate == null) { if (scheduled.Command is ConstructorCommand <TAggregate> ) { var ctor = typeof(TAggregate).GetConstructor(new[] { scheduled.Command.GetType() }); aggregate = (TAggregate)ctor.Invoke(new[] { scheduled.Command }); } else { // TODO: (ApplyScheduledCommand) this should probably be a different exception type. throw new ConcurrencyException( string.Format("No {0} was found with id {1} so the command could not be applied.", typeof(TAggregate).Name, scheduled.AggregateId), new IEvent[] { scheduled }); } } else { await aggregate.ApplyAsync(scheduled.Command); } await repository.Save(aggregate); return(new CommandSucceeded(scheduled)); } catch (Exception ex) { exception = ex; } return(await FailScheduledCommand(repository, scheduled, exception, aggregate)); }
public void SetUp() { Command<Order>.AuthorizeDefault = delegate { return true; }; repository = CreateRepository(); var order = new Order().Apply(new AddItem { ProductName = "Widget", Price = 10m, Quantity = 2 }); repository.Save(order).Wait(); aggregateId = order.Id; repository.GetLatest(aggregateId).Result.EventHistory.Last().Should().BeOfType<Order.ItemAdded>(); }
public async Task The_aggregate_can_be_sourced() { var order = await repository.GetLatest(aggregateId); order.Apply(new Annotate <Order>(Any.String())); repository.Save(order).Wait(); order = await repository.GetLatest(aggregateId); order.CustomerName.Should().Be(customerName); }
public void SetUp() { Command <Order> .AuthorizeDefault = delegate { return(true); }; repository = CreateRepository(); var order = new Order().Apply(new AddItem { ProductName = "Widget", Price = 10m, Quantity = 2 }); repository.Save(order).Wait(); aggregateId = order.Id; repository.GetLatest(aggregateId).Result.EventHistory.Last().Should().BeOfType <Order.ItemAdded>(); }
public BankAccountPersistenceUnitTests() { Command <BankAccount> .AuthorizeDefault = (target, command) => true; var connectionString = @"Data Source=(localdb)\MSSQLLocalDB; Integrated Security=True; Initial Catalog=BankEventStore"; var configuration = new Configuration(); configuration.UseSqlEventStore(c => c.UseConnectionString(connectionString)); _repository = configuration.Repository <BankAccount>(); Guid bankAccountAggregateId = new Guid("0a59517e-f69d-46b4-aa24-45aea90f0012"); _bankAccount = _repository.GetLatest(bankAccountAggregateId).Result; if (_bankAccount == null) { _bankAccount = new BankAccount(new BankAccount.OpenAccount(bankAccountAggregateId) { CustomerId = new CustomerId("Xavier") }); } }
public Task <TAggregate> GetLatest(Guid aggregateId) { return(innerRepository.GetLatest(aggregateId)); }