Exemple #1
0
        public async Task Send(IEventHandlingContext context, IPipe <IEventHandlingContext> next)
        {
            var eventContext = (EventHandlingContext)context;

            var eventHandlerInvoker = HandlerInvokerFactory <IEventHandlingContext> .GetOrCreate(
                eventContext.GetType(),
                eventContext.EventHandlerInterface);

            var eventHandlers = _resolverAccessor.Current.ResolveServices(eventHandlerInvoker.HandlerInterface);

            var handleTasks = eventHandlers?
                              .Select(handler =>
                                      eventHandlerInvoker.HandleAsync(handler, context, context.CancellationToken))
                              ?? Enumerable.Empty <Task>();

            var tcs = new TaskCompletionSource <object>();

            context.CancellationToken.Register(
                () => tcs.TrySetCanceled(),
                false);

            await await Task.WhenAny(
                Task.WhenAll(handleTasks),
                tcs.Task);
        }
        public Task Send(IQueryHandlingContext context, IPipe <IQueryHandlingContext> next)
        {
            var queryContext = (QueryHandlingContext)context;

            var queryHandlerInvoker = HandlerInvokerFactory <IQueryHandlingContext> .GetOrCreate(
                queryContext.GetType(),
                queryContext.QueryHandlerInterface);

            var queryHandlerInstance = _resolverAccessor.Current.ResolveService(queryHandlerInvoker.HandlerInterface);

            queryContext.SetResultTask(queryHandlerInvoker.HandleAsync(
                                           queryHandlerInstance,
                                           context,
                                           context.CancellationToken));

            return(queryContext.ResultTask);
        }
        public Task Send(ICommandHandlingContext context, IPipe <ICommandHandlingContext> next)
        {
            var commandHandlerInvoker = HandlerInvokerFactory <ICommandHandlingContext> .GetOrCreate(
                context.GetType(),
                context.CommandHandlerInterface);

            var commandHandlerInstance = _resolverAccessor.Current.ResolveService(commandHandlerInvoker.HandlerInterface);

            if (context is IResultingHandlingContext resultingContext)
            {
                resultingContext.SetResultTask(commandHandlerInvoker.HandleAsync(
                                                   commandHandlerInstance,
                                                   context,
                                                   context.CancellationToken));

                return(resultingContext.ResultTask);
            }

            return(commandHandlerInvoker.HandleAsync(
                       commandHandlerInstance,
                       context,
                       context.CancellationToken));
        }