public Application(IMediatorContext mediator) { _mediator = mediator; _parameters = new List <string> { "1", "2", "3", "4", "5" }; }
public SendEmailHandler(IMediatorContext mediator, IEmailService emailService, ILogger logger, IEmailRetryPolicy emailPolicy) { _mediator = mediator; _emailService = emailService; _logger = logger; _emailPolicy = emailPolicy; }
public MaintainTimer(IMediatorContext mediatorContext, IClientContext clientContext) { this.mediatorContext = mediatorContext; this.clientContext = clientContext; mediatorContext.Completed += MediatorContext_Completed; timer = new Timer(Tick); }
private static Task <TResponse> GetPipeline(TRequest request, IMediatorContext context, RequestHandlerDelegate <TResponse> invokeHandler, MultiInstanceFactory factory) { var behaviors = factory(typeof(IPipelineBehavior <TRequest, TResponse>)) .Cast <IPipelineBehavior <TRequest, TResponse> >() .Reverse(); var aggregate = behaviors.Aggregate(invokeHandler, (next, pipeline) => () => pipeline.Handle(request, next, context)); return(aggregate()); }
private static async Task GetPipeline(TNotification request, IMediatorContext context, IEnumerable <RequestHandlerDelegate <Unit> > handlers, MultiInstanceFactory factory, Func <IEnumerable <RequestHandlerDelegate <Unit> >, Task> publish) { var behaviors = factory(typeof(IPipelineBehavior <TNotification, Unit>)) .Cast <IPipelineBehavior <TNotification, Unit> >() .Reverse() .ToArray(); var tasks = new List <RequestHandlerDelegate <Unit> >(); foreach (var handle in handlers) { RequestHandlerDelegate <Unit> handleCoreDelegate = async() => { await handle(); return(Unit.Value); }; var aggregate = behaviors.Aggregate(handleCoreDelegate, (next, pipeline) => () => pipeline.Handle(request, next, context)); tasks.Add(aggregate); } await publish(tasks); }
public async Task <TResponse> Handle(TRequest request, RequestHandlerDelegate <TResponse> next, IMediatorContext context) { await Task.WhenAll(_preProcessors.Select(p => p.Process(request, context))).ConfigureAwait(false); return(await next().ConfigureAwait(false)); }
public EmailSentHandler(IMediatorContext mediator, ILogger logger) { _mediator = mediator; _logger = logger; }
public Task Process(TRequest request, IMediatorContext context) { _writer.WriteLine("- Starting Up"); return(Task.FromResult(0)); }
public Task Process(TRequest request, TResponse response, IMediatorContext context) { _writer.WriteLine("- All Done"); return(Task.FromResult(0)); }
private static IMediatorContext SetDefaultContext(CancellationToken cancellationToken, IMediatorContext context) { context = context ?? new DefaultMediatorContext(); if (context.CancellationToken == default(CancellationToken)) { context.CancellationToken = cancellationToken; } return(context); }
public abstract Task Handle(IRequest request, CancellationToken cancellationToken, IMediatorContext context, SingleInstanceFactory singleFactory, MultiInstanceFactory multiFactory);
public Task Process(Ping request, Pong response, IMediatorContext context) { response.Message = response.Message + " " + request.Message; return(Task.FromResult(0)); }
public async Task <Pong> Handle(Ping request, RequestHandlerDelegate <Pong> next, IMediatorContext context) { _output.Messages.Add("Outer before"); var response = await next(); _output.Messages.Add("Outer after"); return(response); }
public async Task <TResponse> Handle(TRequest request, RequestHandlerDelegate <TResponse> next, IMediatorContext context) { _output.Messages.Add("Constrained before"); var response = await next(); _output.Messages.Add("Constrained after"); return(response); }
public ReportGeneratedHandler(IMediatorContext mediator, ILogger logger) { _mediator = mediator; _logger = logger; }
public Task Process(Ping request, IMediatorContext context) { request.Message = request.Message + " Ping"; return(Task.FromResult(0)); }
private RequestHandlerDelegate <TResponse> GetHandler(TRequest request, CancellationToken cancellationToken, IMediatorContext context, SingleInstanceFactory factory) { var resolveExceptions = new Collection <Exception>(); LazyInitializer.EnsureInitialized(ref _handlerFactory, ref _initialized, ref _syncLock, () => GetHandlerFactory(factory, ref resolveExceptions)); if (!_initialized || _handlerFactory == null) { throw BuildException(request, resolveExceptions); } return(_handlerFactory(request, cancellationToken, context, factory)); }
public override Task <TResponse> Handle(IRequest <TResponse> request, CancellationToken cancellationToken, IMediatorContext context, SingleInstanceFactory singleFactory, MultiInstanceFactory multiFactory) { var handler = GetHandler((TRequest)request, cancellationToken, context, singleFactory); var pipeline = GetPipeline((TRequest)request, context, handler, multiFactory); return(pipeline); }
public abstract Task Handle(INotification notification, CancellationToken cancellationToken, IMediatorContext context, MultiInstanceFactory multiInstanceFactory, Func <IEnumerable <RequestHandlerDelegate <Unit> >, Task> publish);
public override Task Handle(INotification notification, CancellationToken cancellationToken, IMediatorContext context, MultiInstanceFactory multiInstanceFactory, Func <IEnumerable <RequestHandlerDelegate <Unit> >, Task> publish) { var handlers = GetHandlers((TNotification)notification, cancellationToken, context, multiInstanceFactory); var pipeline = GetPipeline((TNotification)notification, context, handlers, multiInstanceFactory, publish); return(pipeline); }
public Task Handle(PingedAsync notification, IMediatorContext context) { return(_writer.WriteLineAsync($"--- Got pinged async. {context?.Items["created-at"]}")); }
private IEnumerable <RequestHandlerDelegate <Unit> > GetHandlers(TNotification notification, CancellationToken cancellationToken, IMediatorContext context, MultiInstanceFactory factory) { var notificationHandlers = GetHandlers <INotificationHandler <TNotification> >(factory) .Select(x => { return(new RequestHandlerDelegate <Unit>(() => { x.Handle(notification); return Task.FromResult(Unit.Value); })); }); var asyncNotificationHandlers = GetHandlers <IAsyncNotificationHandler <TNotification> >(factory) .Select(x => { return(new RequestHandlerDelegate <Unit>(async() => { await x.Handle(notification).ConfigureAwait(false); return Unit.Value; })); }); var cancellableAsyncNotificationHandlers = GetHandlers <ICancellableAsyncNotificationHandler <TNotification> >(factory) .Select(x => { return(new RequestHandlerDelegate <Unit>(async() => { await x.Handle(notification, cancellationToken).ConfigureAwait(false); return Unit.Value; })); }); var contextualAsyncNotificationHandlers = GetHandlers <IContextualAsyncNotificationHandler <TNotification> >(factory) .Select(x => { return(new RequestHandlerDelegate <Unit>(async() => { await x.Handle(notification, context).ConfigureAwait(false); return Unit.Value; })); }); var allHandlers = notificationHandlers .Concat(asyncNotificationHandlers) .Concat(cancellableAsyncNotificationHandlers) .Concat(contextualAsyncNotificationHandlers); return(allHandlers); }
public Task Publish <TNotification>(TNotification notification, CancellationToken cancellationToken = default(CancellationToken), IMediatorContext context = null) where TNotification : INotification { if (notification == null) { throw new ArgumentNullException(nameof(notification)); } context = SetDefaultContext(cancellationToken, context); var notificationType = notification.GetType(); var handler = _notificationHandlers.GetOrAdd(notificationType, t => (NotificationHandler)Activator.CreateInstance(typeof(NotificationHandlerImpl <>).MakeGenericType(notificationType))); return(handler.Handle(notification, cancellationToken, context, _multiInstanceFactory, PublishCore)); }
public async Task <TResponse> Handle(TRequest request, RequestHandlerDelegate <TResponse> next, IMediatorContext context) { _writer.WriteLine("-- Handling Request"); var response = await next(); _writer.WriteLine("-- Finished Request"); return(response); }
public Task Send(IRequest request, CancellationToken cancellationToken = default(CancellationToken), IMediatorContext context = null) { if (request == null) { throw new ArgumentNullException(nameof(request)); } context = SetDefaultContext(cancellationToken, context); var requestType = request.GetType(); var handler = _voidRequestHandlers.GetOrAdd(requestType, t => (RequestHandler)Activator.CreateInstance(typeof(RequestHandlerImpl <>).MakeGenericType(requestType))); return(handler.Handle(request, cancellationToken, context, _singleInstanceFactory, _multiInstanceFactory)); }