예제 #1
0
 public Application(IMediatorContext mediator)
 {
     _mediator   = mediator;
     _parameters = new List <string> {
         "1", "2", "3", "4", "5"
     };
 }
예제 #2
0
 public SendEmailHandler(IMediatorContext mediator, IEmailService emailService, ILogger logger, IEmailRetryPolicy emailPolicy)
 {
     _mediator     = mediator;
     _emailService = emailService;
     _logger       = logger;
     _emailPolicy  = emailPolicy;
 }
예제 #3
0
        public MaintainTimer(IMediatorContext mediatorContext, IClientContext clientContext)
        {
            this.mediatorContext = mediatorContext;
            this.clientContext   = clientContext;

            mediatorContext.Completed += MediatorContext_Completed;

            timer = new Timer(Tick);
        }
예제 #4
0
        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());
        }
예제 #5
0
        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;
 }
예제 #8
0
 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));
 }
예제 #10
0
 private static IMediatorContext SetDefaultContext(CancellationToken cancellationToken, IMediatorContext context)
 {
     context = context ?? new DefaultMediatorContext();
     if (context.CancellationToken == default(CancellationToken))
     {
         context.CancellationToken = cancellationToken;
     }
     return(context);
 }
예제 #11
0
 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));
            }
예제 #13
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);
            }
예제 #14
0
            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);
            }
예제 #15
0
 public ReportGeneratedHandler(IMediatorContext mediator, ILogger logger)
 {
     _mediator = mediator;
     _logger   = logger;
 }
예제 #16
0
            public Task Process(Ping request, IMediatorContext context)
            {
                request.Message = request.Message + " Ping";

                return(Task.FromResult(0));
            }
예제 #17
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));
        }
예제 #18
0
        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);
        }
예제 #19
0
 public abstract Task Handle(INotification notification, CancellationToken cancellationToken, IMediatorContext context, MultiInstanceFactory multiInstanceFactory, Func <IEnumerable <RequestHandlerDelegate <Unit> >, Task> publish);
예제 #20
0
        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);
        }
예제 #21
0
 public Task Handle(PingedAsync notification, IMediatorContext context)
 {
     return(_writer.WriteLineAsync($"--- Got pinged async. {context?.Items["created-at"]}"));
 }
예제 #22
0
        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);
        }
예제 #23
0
        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));
        }
예제 #24
0
        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);
        }
예제 #25
0
        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));
        }