Esempio n. 1
0
        public async Task <Result <dynamic> > DispatchCommandAsync(string commandName, object command, CancellationToken cancellationToken = default)
        {
            var payload = JsonSerializer.Serialize(command, _jsonOptions.JsonSerializerOptions);

            var  subscription = CqrsCommandSubscriptionManager.GetSubscriptionsForCommand(commandName).First();
            Type handlerType  = subscription.HandlerType;

            //dynamic handler = _provider.GetService(handlerType);
            dynamic handler = subscription.CreateHandler(_provider);

            dynamic typedPayload = null;

            if (subscription.IsDynamic)
            {
                typedPayload = JsonSerializer.Deserialize <dynamic>(payload, _jsonOptions.JsonSerializerOptions);
            }
            else
            {
                typedPayload = JsonSerializer.Deserialize(payload, subscription.CommandType, _jsonOptions.JsonSerializerOptions);
            }

            Result <dynamic> result = await handler.HandleAsync(commandName, typedPayload, cancellationToken);

            return(result);
        }
        public async Task <Result <R> > DispatchAsync <R>(ICommand <R> command, CancellationToken cancellationToken = default)
        {
            var  subscription = CqrsCommandSubscriptionManager.GetSubscriptionsForCommand(command).First();
            Type handlerType  = subscription.HandlerType;//Interface OR Concrete.
            var  commandName  = subscription.CommandName;

            //dynamic handler = _provider.GetService(handlerType);
            dynamic handler = subscription.CreateHandler(_provider);

            Result <R> result = await handler.HandleAsync(commandName, (dynamic)command, cancellationToken);

            return(result);
        }