protected override GeneratedMethod GenerateCode(Type serviceBase, MethodInfo method)
        {
            var parameters = method.GetParameters();

            var requestType  = parameters[0].ParameterType;
            var responseType = method.ReturnType.GetGenericArguments().Single(); // get the actual return type

            // try to find a query or command handler
            var handlerInterface = typeof(IHandler <,>).MakeGenericType(requestType, responseType);
            var handler          = _handlerStore.Find(serviceBase, handlerInterface, method.Name);

            if (handler != null)
            {
                return(GenerateMethod(Code.MethodHandlerCode, method.Name, requestType, responseType, handler));
            }

            if (responseType != typeof(Proto.Empty))
            {
                throw new InvalidOperationException($"Couldn't find a type that implements {handlerInterface} to handle method {method}.");
            }

            var commandHandlerInterface = typeof(ICommandHandler <>).MakeGenericType(requestType);

            handler = _handlerStore.Find(serviceBase, commandHandlerInterface, method.Name);

            if (handler != null)
            {
                return(GenerateMethod(Code.MethodCommandHandlerCode, method.Name, requestType, responseType, handler));
            }

            throw new InvalidOperationException($"Couldn't find a type that implements {handlerInterface} or {commandHandlerInterface} to handle method {method}.");
        }
示例#2
0
        public void Generate_calls_generator_for_IHandler_with_correct_parameters()
        {
            // arrange
            var methodInfo = GetMethodInfo(nameof(ValidMethodForHandler));

            var expectedHandlerInfo = new HandlerInfo(null, null);

            A.CallTo(() => _handlerStore.Find(typeof(string), typeof(IServerStreamHandler <int, string>), nameof(ValidMethodForHandler)))
            .Returns(expectedHandlerInfo);

            // act
            var result = _generator.Generate(typeof(string), methodInfo);

            // assert
            A.CallTo(() => _methodCodeGen.GenerateMethod(Code.MethodServerStreamHandlerCode, nameof(ValidMethodForHandler), typeof(int), typeof(string), expectedHandlerInfo))
            .MustHaveHappenedOnceExactly();
        }
        protected override GeneratedMethod GenerateCode(Type serviceBase, MethodInfo method)
        {
            var parameters = method.GetParameters();

            var parameterType = parameters[0].ParameterType;
            var streamType    = parameters[1].ParameterType.GetGenericArguments().Single();

            var handlerInterface = typeof(IServerStreamHandler <,>).MakeGenericType(parameterType, streamType);
            var handler          = _handlerStore.Find(serviceBase, handlerInterface, method.Name);

            if (handler == null)
            {
                throw new InvalidOperationException($"Couldn't find a type that implements {handlerInterface} to handle method {method}.");
            }

            return(GenerateMethod(Code.MethodServerStreamHandlerCode, method.Name, parameterType, streamType, handler));
        }
示例#4
0
        protected override GeneratedMethod GenerateCode(Type serviceBase, MethodInfo method)
        {
            var parameters = method.GetParameters();

            var requestType         = parameters[0].ParameterType;
            var responseType        = method.ReturnType.GetGenericArguments().Single(); // get the actual return type
            var streamParameterType = requestType.GetGenericArguments().Single();

            // try to find a query with IAsyncStreamReader
            var handlerInterface = typeof(IClientStreamHandler <,>).MakeGenericType(streamParameterType, responseType);
            var handler          = _handlerStore.Find(serviceBase, handlerInterface, method.Name);

            if (handler == null)
            {
                throw new InvalidOperationException($"Couldn't find a type that implements {handlerInterface} to handle method {method}.");
            }

            return(GenerateMethod(Code.MethodClientStreamHandlerCode, method.Name, streamParameterType, responseType, handler));
        }