Exemplo n.º 1
0
        internal static void TrySubscribeDiscoveredHandler <T>(
            Configuration configuration,
            Type commandType,
            PocketContainer c,
            ICommandReceiver <T> receiver)
        {
            foreach (var handlerDescription in
                     configuration.CommandHandlerDescriptions
                     .Where(t => t.HandledCommandType == commandType))
            {
                var handler = c.Resolve(handlerDescription.HandlerInterface) as ICommandHandler <T>;

                if (configuration.Properties.TracingEnabled)
                {
                    handler = handler.Trace();
                }

                var subscription = receiver.Subscribe(handler);

                configuration.RegisterForDisposal(subscription);

                Logger <Configuration> .Log.Trace(
                    "Subscribing discovered command handler: {handler} to handle commands of type {commandType}",
                    handlerDescription.ConcreteHandlerType,
                    handlerDescription.HandledCommandType);
            }
        }
Exemplo n.º 2
0
        public static IDisposable Subscribe <TReceive, THandle>(
            this ICommandReceiver <TReceive> receiver,
            ICommandHandler <THandle> handler)
            where THandle : TReceive
        {
            async Task <ICommandDeliveryResult> OnNext(ICommandDelivery <TReceive> delivery)
            {
                switch (delivery)
                {
                case ICommandDelivery <THandle> handled:
                    return(await handler.Handle(handled));

                default:
                    if (!(delivery.Command is THandle command))
                    {
                        return(null);
                    }

                    var clone = new CommandDelivery <THandle>(
                        command,
                        delivery.DueTime,
                        delivery.OriginalDueTime,
                        delivery.IdempotencyToken,
                        delivery.NumberOfPreviousAttempts);

                    return(await handler.Handle(clone));
                }
            }

            return(receiver.Subscribe(OnNext));
        }