public static ICommandHandlerBuilder <TCommand> UseValidator <TCommand>( this ICommandHandlerBuilder <TCommand> builder, IValidator <TCommand> validator) { return(builder.Pipe(next => async(message, ct) => { await validator.ValidateAndThrowAsync(message.Body, cancellationToken: ct); await next(message, ct); })); }
internal static ICommandHandlerBuilder <CommandMessage <TMessage> > Validate <TMessage>( this ICommandHandlerBuilder <CommandMessage <TMessage> > commandHandlerBuilder, Func <TMessage, ValidationResult> validator) { return(commandHandlerBuilder.Pipe(next => (commandMessage, ct) => { validator(commandMessage.Command); return next(commandMessage, ct); })); }
// 3. Example pipeline that ensures the user is authorized by // checking their role. internal static ICommandHandlerBuilder <CommandMessage <TMessage> > RequireRole <TMessage>( this ICommandHandlerBuilder <CommandMessage <TMessage> > commandHandlerBuilder, string role) { return(commandHandlerBuilder.Pipe(next => (commandMessage, ct) => { if (!commandMessage.GetUser().IsInRole(role)) { throw new InvalidOperationException("Not Authorized"); } return next(commandMessage, ct); })); }
public static ICommandHandlerBuilder <CommandMessage <TMessage> > RequiresRole <TMessage>(this ICommandHandlerBuilder <CommandMessage <TMessage> > source, string roleName) where TMessage : class { return(source.Pipe(next => async(message, ct) => { var metadata = CommandMetaData.FromDictionary(message.Metadata); if (metadata.HasRole(roleName)) { return await next(message, ct); } throw new InsufficientRights(); })); }
public static ICommandHandlerBuilder <IRoadRegistryContext, TCommand> UseRoadRegistryContext <TCommand>( this ICommandHandlerBuilder <TCommand> builder, IStreamStore store, IRoadNetworkSnapshotReader snapshotReader, EventEnricher enricher) { if (store == null) { throw new ArgumentNullException(nameof(store)); } if (snapshotReader == null) { throw new ArgumentNullException(nameof(snapshotReader)); } if (enricher == null) { throw new ArgumentNullException(nameof(enricher)); } return(builder.Pipe <IRoadRegistryContext>(next => async(message, ct) => { var map = new EventSourcedEntityMap(); var context = new RoadRegistryContext(map, store, snapshotReader, SerializerSettings, EventMapping); await next(context, message, ct); foreach (var entry in map.Entries) { var events = entry.Entity.TakeEvents(); if (events.Length != 0) { var messageId = message.MessageId.ToString("N"); var version = entry.ExpectedVersion; Array.ForEach(events, @event => enricher(@event)); var messages = Array.ConvertAll( events, @event => new NewStreamMessage( Deterministic.Create(Deterministic.Namespaces.Events, $"{messageId}-{version++}"), EventMapping.GetEventName(@event.GetType()), JsonConvert.SerializeObject(@event, SerializerSettings) )); await store.AppendToStream(entry.Stream, entry.ExpectedVersion, messages, ct); } } } )); }
public static ICommandHandlerBuilder <CommandMessage <TCommand> > AddSqlStreamStore <TCommand>( this ICommandHandlerBuilder <CommandMessage <TCommand> > commandHandlerBuilder, Func <IStreamStore> getStreamStore, Func <ConcurrentUnitOfWork> getUnitOfWork, EventMapping eventMapping, EventSerializer eventSerializer) { return(commandHandlerBuilder.Pipe(next => async(commandMessage, ct) => { await next(commandMessage, ct); return await AddSqlStreamStore( getStreamStore, getUnitOfWork, eventMapping, eventSerializer, commandMessage, ct); })); }
public static ICommandHandlerBuilder <T> Log <T>(this ICommandHandlerBuilder <T> builder, ILoggerFactory?loggerFactory = null) where T : class { var log = loggerFactory?.CreateLogger <T>() ?? new NullLogger <T>(); return(builder.Pipe(next => async(m, ct) => { if (log.IsEnabled(LogLevel.Information)) { log.LogInformation(m.ToString()); } try { return await next(m, ct); } catch (Exception ex) { if (log.IsEnabled(LogLevel.Error)) { log.LogError(ex.ToString()); } throw; } })); }