コード例 #1
0
        public async Task CanExecuteInheritedResultAsync()
        {
            var type = typeof(ResultExecutorClassTest);

            var method = type.GetMethod(nameof(ResultExecutorClassTest.TestMethodInheritedResult));

            await using var context = new CommandContext();

            var moduleBuilderMock = new Mock <IModuleBuilder>();

            moduleBuilderMock.SetupGet(a => a.Type).Returns(type);

            var commandBuilderMock = new Mock <ICommandBuilder>();

            commandBuilderMock.SetupGet(a => a.IsAsync).Returns(false);
            commandBuilderMock.SetupGet(a => a.MethodInfo).Returns(method);

            var moduleBuilder  = moduleBuilderMock.Object;
            var commandBuilder = commandBuilderMock.Object;

            var executor = ResultExecutor.Create(moduleBuilder, commandBuilder);

            var module = new ResultExecutorClassTest();

            (module as IModuleBase).SetContext(context);

            var result = await executor.ExecuteAsync(module, new object[0]);

            Assert.NotNull(result);
            Assert.True(result.Success);
            Assert.IsType <SuccessObjectResult>(result);
            Assert.Equal(ResultExecutorClassTest.LogValue, (result as SuccessObjectResult).Value);
        }
コード例 #2
0
        public ICommandExecutor GetCommandExecutor(IModuleBuilder moduleBuilder, ICommandBuilder commandBuilder)
        {
            var type            = commandBuilder.MethodInfo.ReturnType;
            var asyncResultType = commandBuilder.AsyncResultType;

            if (commandBuilder.IsAsync)
            {
                if (type == typeof(Task) && commandBuilder.AsyncResultType.HasNoContent())
                {
                    return(VoidTaskExecutor.Create(moduleBuilder, commandBuilder));
                }

                if (type == typeof(ValueTask) && commandBuilder.AsyncResultType.HasNoContent())
                {
                    return(VoidValueTaskExecutor.Create(moduleBuilder, commandBuilder));
                }

                var genericDefinition = type.GetGenericTypeDefinition();

                if (genericDefinition == typeof(Task <>) && typeof(IResult).IsAssignableFrom(asyncResultType))
                {
                    return(ResultTaskExecutor.Create(moduleBuilder, commandBuilder));
                }

                if (genericDefinition == typeof(ValueTask <>) && typeof(IResult).IsAssignableFrom(asyncResultType))
                {
                    return(ResultValueTaskExecutor.Create(moduleBuilder, commandBuilder));
                }

                if (genericDefinition == typeof(ValueTask <>))
                {
                    return(ObjectValueTaskExecutor.Create(moduleBuilder, commandBuilder));
                }

                return(ObjectTaskExecutor.Create(moduleBuilder, commandBuilder));
            }

            if (type == typeof(void))
            {
                return(VoidExecutor.Create(moduleBuilder, commandBuilder));
            }

            if (typeof(IResult).IsAssignableFrom(type))
            {
                return(ResultExecutor.Create(moduleBuilder, commandBuilder));
            }

            return(ObjectExecutor.Create(moduleBuilder, commandBuilder));
        }