public async Task <IActionResult> CreateDomain( [FromServices] ICommandHandlerResolver bus, [FromCommandId] Guid commandId, [FromBody] CreateDomainRequest request, CancellationToken cancellationToken = default) { // TODO: Get this validator from DI await new CreateDomainRequestValidator() .ValidateAndThrowAsync(request, cancellationToken: cancellationToken); var command = CreateDomainRequestMapping.Map(request); // TODO: Sending null for top level domain should give a decent error, not 500 // TODO: Apikey description in documentation should be translatable // TODO: Add bad format response code if it is not json return(Accepted( $"/v1/domains/{command.DomainName}", new LastObservedPositionResponse( await bus.Dispatch( commandId, command, GetMetadata(), cancellationToken)))); }
public async Task <IActionResult> UpdateExampleAggregate( [FromServices] ICommandHandlerResolver bus, [FromCommandId] Guid commandId, [FromRoute] Guid exampleAggregateId, [FromBody] UpdateExampleAggregateRequest request, CancellationToken cancellationToken = default) { if (request != null) { request.Id = exampleAggregateId; } await new UpdateExampleAggregateRequestValidator() .ValidateAndThrowAsync(request, cancellationToken: cancellationToken); var command = UpdateExampleAggregateRequestMapping.Map(request); return(Accepted( $"/v1/example-aggregates/{command.ExampleAggregateId}", await bus.Dispatch( commandId, command, GetMetadata(), cancellationToken))); }
public async Task <IActionResult> AddGoogleSuiteService( [FromServices] ICommandHandlerResolver bus, [FromServices] ApiProjectionsContext context, [FromCommandId] Guid commandId, [FromRoute] string secondLevelDomain, [FromRoute] string topLevelDomain, [FromBody] AddGoogleSuiteServiceRequest request, CancellationToken cancellationToken = default) { if (request != null) { request.SecondLevelDomain = secondLevelDomain; request.TopLevelDomain = topLevelDomain; } // TODO: We can check in the eventstore if those aggregates even exist await new AddGoogleSuiteServiceRequestValidator() .ValidateAndThrowAsync(request, cancellationToken: cancellationToken); var command = AddGoogleSuiteServiceRequestMapping.Map( new DomainName( new SecondLevelDomain(secondLevelDomain), TopLevelDomain.FromValue(topLevelDomain)), request); return(Accepted( $"/v1/domains/{command.DomainName}/services/{command.ServiceId}", new LastObservedPositionResponse( await bus.Dispatch( commandId, command, GetMetadata(), cancellationToken)))); }
public async Task <IActionResult> RemoveService( [FromServices] ICommandHandlerResolver bus, [FromServices] ApiProjectionsContext context, [FromCommandId] Guid commandId, [FromRoute] string secondLevelDomain, [FromRoute] string topLevelDomain, [FromRoute] Guid?serviceId, CancellationToken cancellationToken = default) { var request = new RemoveServiceRequest { SecondLevelDomain = secondLevelDomain, TopLevelDomain = topLevelDomain, ServiceId = serviceId }; // TODO: We can check in the eventstore if those aggregates even exist await new RemoveServiceRequestValidator() .ValidateAndThrowAsync(request, cancellationToken: cancellationToken); var command = RemoveServiceRequestMapping.Map(request); return(Accepted( $"/v1/domains/{command.DomainName}/services", new LastObservedPositionResponse( await bus.Dispatch( commandId, command, GetMetadata(), cancellationToken)))); }
public static async Task <long> Dispatch( this ICommandHandlerResolver handlerResolver, Guid commandId, object command, IDictionary <string, object> metadata = null, CancellationToken cancellationToken = default(CancellationToken)) { if (command == null) { throw new ArgumentNullException(nameof(command)); } metadata = metadata ?? new Dictionary <string, object>(); var eventType = command.GetType(); var dispatchMethod = DispatchInternalMethod.MakeGenericMethod(eventType); // Make sure they align with the method signature below var parameters = new[] { handlerResolver, commandId, command, metadata, cancellationToken }; return(await(Task <long>) dispatchMethod.Invoke(handlerResolver, parameters)); }
public CommandsGateway( ICommandHandlerResolver commandHandlerResolver, Func <IDisposable> scopeFactory, IValidatorResolver validatorResolver) { _commandHandlerResolver = commandHandlerResolver; _scopeFactory = scopeFactory; _validationFacade = new ValidationFacade(validatorResolver); }
public CommandHandlingSettings( [NotNull] ICommandHandlerResolver handlerResolver, [NotNull] ResolveCommandType resolveCommandType) { Condition.Requires(handlerResolver, "handlerResolver").IsNotNull(); Condition.Requires(resolveCommandType, "ResolveCommandType").IsNotNull(); _handlerResolver = handlerResolver; _resolveCommandType = resolveCommandType; }
public void Setup() { command = new TestCommand(); commandHandler = Substitute.For<IAsyncCommandHandler<TestCommand>>(); commandHandlerResolver = Substitute.For<ICommandHandlerResolver>(); commandHandlerResolver.ResolveCommandHandler<IAsyncCommandHandler<TestCommand>>().Returns(commandHandler); sut = new AsyncCommandBus(commandHandlerResolver); }
private static async Task DispatchInternal <TCommand>( ICommandHandlerResolver handlerResolver, Guid commandId, TCommand command, IDictionary <string, object> metadata, CancellationToken cancellationToken) where TCommand : class { var commandMessage = new CommandMessage <TCommand>(commandId, command, metadata); await handlerResolver.Resolve <TCommand>()(commandMessage, cancellationToken); }
private static async Task DispatchCommand <TCommand>( ICommandHandlerResolver handlerResolver, Guid commandId, TCommand command, Predispatch predispatch, IEnumerable <KeyValuePair <string, IEnumerable <string> > > requestHeaders, CancellationToken cancellationToken) where TCommand : class { var commandMessage = new CommandMessage <TCommand>(commandId, command); predispatch(commandMessage.Metadata, requestHeaders); await handlerResolver.Resolve <TCommand>()(commandMessage, cancellationToken); }
public EntityFrameworkDataSource( DbContext context, IConnectionFactory connectionFactory, IQueryResolver queryResolver, ICommandHandlerResolver commandHandlerResolver, IValidatorResolver valdiatorResolver ) { Context = context; ConnectionFactory = connectionFactory; QueryResolver = queryResolver; CommandHandlerResolver = commandHandlerResolver; ValidatorResolver = valdiatorResolver; entitiesDataSource = new EntityFrameworkEntitiesDataSource(Context); readonlyEntitiesDataSource = new EntityFrameworkReadOnlyEntitiesDataSource(Context); }
public CommandHandlingSettings( [NotNull] ICommandHandlerResolver handlerResolver, [NotNull] ResolveCommandType resolveCommandType) { Ensure.That(handlerResolver, "handlerResolver").IsNotNull(); Ensure.That(resolveCommandType, "ResolveCommandType").IsNotNull(); _handlerResolver = handlerResolver; _resolveCommandType = resolveCommandType; _deserializeCommand = CatchDeserializationExceptions( (commandReader, type) => { var body = commandReader.ReadToEnd(); // Will cause LOH problems if command json > 85KB return(SimpleJson.DeserializeObject(body, type, CommandClient.JsonSerializerStrategy)); }); }
public CommandHandlingSettings( [NotNull] ICommandHandlerResolver handlerResolver, [NotNull] ResolveCommandType resolveCommandType) { Ensure.That(handlerResolver, "handlerResolver").IsNotNull(); Ensure.That(resolveCommandType, "ResolveCommandType").IsNotNull(); _handlerResolver = handlerResolver; _resolveCommandType = resolveCommandType; _deserializeCommand = CatchDeserializationExceptions( (commandReader, type) => { var body = commandReader.ReadToEnd(); // Will cause LOH problems if command json > 85KB return SimpleJson.DeserializeObject(body, type, CommandClient.JsonSerializerStrategy); }); }
public async Task <IActionResult> AddAddress( [FromServices] ICommandHandlerResolver bus, [FromServices] AddressCrabEditClient editClient, [FromServices] Func <IAddresses> getAddresses, [FromBody] AddAddressRequest request, CancellationToken cancellationToken) { // TODO: Turn this into proper VBR API Validation if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var crabAddAddress = await AddToCrab(editClient, request, cancellationToken); var addressId = crabAddAddress.Result; // todo: add command implementation for BoxNumber var command = new RegisterAddress( addressId, StreetNameId.CreateForPersistentId(request.StreetNameId.AsIdentifier().Map(IdentifierMappings.StreetNameId)), PostalCode.CreateForPersistentId(request.PostalCode.AsIdentifier().Map(IdentifierMappings.PostalCode)), new HouseNumber(request.HouseNumber), new BoxNumber(request.BoxNumber)); var position = await bus.Dispatch( Guid.NewGuid(), command, GetMetadata(), cancellationToken); // Because we don't use the addressId as an identifier, we are stuck with the mess of retrieving our aggregate // and getting the surrogate identifier from it.... PersistentLocalIdentifier var addresses = getAddresses(); var address = await addresses.GetOptionalAsync(addressId, cancellationToken); if (!address.HasValue) { throw new ApiException("Er is een fout opgetreden.", StatusCodes.Status500InternalServerError); } return(CreatedWithPosition( $"/v1/adressen/{address.Value.PersistentLocalId}", position, crabAddAddress.ExecutionTime)); }
private static async Task <long> DispatchInternal <TCommand>( ICommandHandlerResolver handlerResolver, Guid commandId, TCommand command, IDictionary <string, object> metadata, CancellationToken cancellationToken) where TCommand : class { var commandMessage = new CommandMessage <TCommand>(commandId, command, metadata); var handler = handlerResolver.Resolve <TCommand>(); if (handler == null) { throw new ApplicationException($"No handler was found for command {typeof(TCommand).FullName}"); } return(await handler(commandMessage, cancellationToken)); }
public static async Task Dispatch( this ICommandHandlerResolver handlerResolver, Guid commandId, object command, IDictionary <string, object> metadata = null, CancellationToken cancellationToken = default(CancellationToken)) { metadata = metadata ?? new Dictionary <string, object>(); var eventType = command.GetType(); var dispatchMethod = s_dispatchInternalMethod.MakeGenericMethod(eventType); var paramaters = new[] { handlerResolver, commandId, command, metadata, cancellationToken }; await(Task) dispatchMethod.Invoke(handlerResolver, paramaters); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { // Swagger. services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "AspNetCore Sample", Version = "v1" }); }); // Repository. services.AddSingleton <IProductRepository, InMemoryProductRepository>(); // Register command handlers. SetupContainerRegistration(services); SetupAttributeRegistration(services); SetupBasicRegistration(services); // Command dispatcher. services.AddSingleton <ICommandAsyncDispatcher>(serviceProvider => { // Wrap ASP NET Core service provider in a resolver. ICommandHandlerResolver containerResolver = new ContainerCommandAsyncHandlerResolver(new AspNetCoreServiceProviderAdapter(serviceProvider)); // CommandHandlerAttributeRegistration implements ICommandHandlerResolver. ICommandHandlerResolver attributeResolver = serviceProvider.GetRequiredService <CommandHandlerAttributeRegistration>(); // CommandHandlerRegistration implements ICommandHandlerResolver. ICommandHandlerResolver basicResolver = serviceProvider.GetRequiredService <CommandHandlerRegistration>(); // Merge all resolvers. var compositeResolver = new CompositeCommandHandlerResolver(new ICommandHandlerResolver[] { // Order is followed when resolving handlers. containerResolver, attributeResolver, basicResolver }, (ex) => true); // Handle any exception if exception is thrown from a resolver, we can return true to allow the dispatcher to proceed to next resolver. return(new CommandDispatcher(compositeResolver)); }); services.AddMvc(); }
public async Task <IActionResult> CreateDomain( [FromServices] ICommandHandlerResolver bus, [FromCommandId] Guid commandId, [FromBody] RegisterOrganisationRequest request, CancellationToken cancellationToken = default) { await new RegisterOrganisationRequestValidator() .ValidateAndThrowAsync(request, cancellationToken: cancellationToken); var command = RegisterOrganisationRequestMapping.Map(request); return(Accepted( $"/v1/organisations/{command.OvoNumber}", await bus.Dispatch( commandId, command, GetMetadata(), cancellationToken))); }
public async Task <IActionResult> CreateAccount( [FromServices] ICommandHandlerResolver bus, [FromCommandId] Guid commandId, [FromBody] CreateAccountRequest request, CancellationToken cancellationToken = default) { await new CreateAccountRequestValidator() .ValidateAndThrowAsync(request, cancellationToken: cancellationToken); var command = CreateAccountRequestMapping.Map(request); return(Accepted( $"/v1/accounts/{command.AccountId}", await bus.Dispatch( commandId, command, GetMetadata(), cancellationToken))); }
protected ApiBusController(ICommandHandlerResolver bus) => Bus = bus;
public CommandHandlingSettings( [NotNull] ICommandHandlerResolver handlerResolver, [NotNull] CommandMediaTypeMap commandMediaTypeMap) : this(handlerResolver, commandMediaTypeMap.GetCommandType) { }
public BPostImportController(ICommandHandlerResolver bus) : base(bus) { }
public LegaslitiveDocumentIdController(ICommandHandlerResolver bus) : base(bus) { }
public LifeCycleController(ICommandHandlerResolver bus) : base(bus) { }
/// <summary> /// Initializes a new instance of the <see cref="CommandHandlingSettings"/> class using /// <see cref="CommandTypeResolvers.FullNameWithUnderscoreVersionSuffix"/> as the command type resolver. /// </summary> /// <param name="handlerResolver">The handler resolver.</param> public CommandHandlingSettings([NotNull] ICommandHandlerResolver handlerResolver) : this( handlerResolver, CommandTypeResolvers.FullNameWithUnderscoreVersionSuffix(handlerResolver.KnownCommandTypes)) { }
public CustomResolver(ICommandHandlerResolver commandHandlerResolver) => _commandHandlerResolver = commandHandlerResolver ?? throw new ArgumentNullException(nameof(commandHandlerResolver));
public LabelsController(ICommandHandlerResolver bus) : base(bus) { }
public IpdcCodeController(ICommandHandlerResolver bus) : base(bus) { }
public CommandDispatcher(ICommandHandlerResolver provider) { _provider = provider; }
public ReflectionBasedHandlerResolver(ICommandHandlerResolver commandHandlerResolver) => _commandHandlerResolver = commandHandlerResolver ?? throw new ArgumentNullException(nameof(commandHandlerResolver));
public InMemoryCommandBus(ICommandHandlerResolver commandHandlerResolver) { _commandHandlerResolver = commandHandlerResolver; }
public CommandDispatcher(ICommandHandlerResolver handlerResolver) { _handlerResolver = handlerResolver; _dispatchMethodInfo = GetType() .GetMethod("DispatchGeneric", BindingFlags.Instance | BindingFlags.NonPublic); }