public void CreateHandlerMethods_CopiesPropertiesFromHandlerModel()
        {
            // Arrange
            var actionDescriptor = new PageActionDescriptor();
            var handlerTypeInfo  = typeof(ModelWithHandler).GetTypeInfo();
            var handlerModel     = new PageHandlerModel(handlerTypeInfo.GetMethod(nameof(ModelWithHandler.OnGetCustomerAsync)), new object[0])
            {
                HttpMethod  = "GET",
                HandlerName = "Customer",
            };
            var pageApplicationModel = new PageApplicationModel(actionDescriptor, handlerTypeInfo, new object[0])
            {
                HandlerMethods =
                {
                    handlerModel,
                }
            };

            // Act
            var handlerDescriptors = CompiledPageActionDescriptorBuilder.CreateHandlerMethods(pageApplicationModel);

            // Assert
            Assert.Collection(
                handlerDescriptors,
                d =>
            {
                Assert.Equal(handlerModel.MethodInfo, d.MethodInfo);
                Assert.Equal(handlerModel.HttpMethod, d.HttpMethod);
                Assert.Equal(handlerModel.HandlerName, d.Name);
            });
        }
        public void CreateHandlerMethods_CopiesParameterDescriptorsFromParameterModel()
        {
            // Arrange
            var actionDescriptor = new PageActionDescriptor();
            var handlerTypeInfo  = typeof(HandlerWithParameters).GetTypeInfo();
            var handlerMethod    = handlerTypeInfo.GetMethod(nameof(HandlerWithParameters.OnPost));
            var parameters       = handlerMethod.GetParameters();
            var parameterModel1  = new PageParameterModel(parameters[0], new object[0])
            {
                ParameterName = "test-name"
            };
            var parameterModel2 = new PageParameterModel(parameters[1], new object[0])
            {
                BindingInfo = new BindingInfo(),
            };
            var handlerModel = new PageHandlerModel(handlerMethod, new object[0])
            {
                Parameters =
                {
                    parameterModel1,
                    parameterModel2,
                }
            };
            var pageApplicationModel = new PageApplicationModel(actionDescriptor, handlerTypeInfo, new object[0])
            {
                HandlerMethods =
                {
                    handlerModel,
                }
            };

            // Act
            var handlerDescriptors = CompiledPageActionDescriptorBuilder.CreateHandlerMethods(pageApplicationModel);

            // Assert
            Assert.Collection(
                Assert.Single(handlerDescriptors).Parameters,
                p =>
            {
                Assert.Equal(parameters[0], p.ParameterInfo);
                Assert.Equal(typeof(string), p.ParameterType);
                Assert.Equal(parameterModel1.ParameterName, p.Name);
            },
                p =>
            {
                Assert.Equal(parameters[1], p.ParameterInfo);
                Assert.Equal(typeof(int), p.ParameterType);
                Assert.Same(parameterModel2.BindingInfo, p.BindingInfo);
            });
        }
예제 #3
0
        // internal for unit testing
        internal static HandlerParameterDescriptor[] CreateHandlerParameters(PageHandlerModel handlerModel)
        {
            var methodParameters = handlerModel.Parameters;
            var parameters       = new HandlerParameterDescriptor[methodParameters.Count];

            for (var i = 0; i < parameters.Length; i++)
            {
                var parameterModel = methodParameters[i];

                parameters[i] = new HandlerParameterDescriptor
                {
                    BindingInfo   = parameterModel.BindingInfo,
                    Name          = parameterModel.ParameterName,
                    ParameterInfo = parameterModel.ParameterInfo,
                    ParameterType = parameterModel.ParameterInfo.ParameterType,
                };
            }

            return(parameters);
        }
        /// <summary>
        /// Creates a <see cref="PageHandlerModel"/> for the specified <paramref name="method"/>.s
        /// </summary>
        /// <param name="method">The <see cref="MethodInfo"/>.</param>
        /// <returns>The <see cref="PageHandlerModel"/>.</returns>
        protected virtual PageHandlerModel CreateHandlerModel(MethodInfo method)
        {
            if (method == null)
            {
                throw new ArgumentNullException(nameof(method));
            }

            if (!IsHandler(method))
            {
                return(null);
            }

            if (!TryParseHandlerMethod(method.Name, out var httpMethod, out var handlerName))
            {
                return(null);
            }

            var handlerModel = new PageHandlerModel(
                method,
                method.GetCustomAttributes(inherit: true))
            {
                Name        = method.Name,
                HandlerName = handlerName,
                HttpMethod  = httpMethod,
            };

            var methodParameters = handlerModel.MethodInfo.GetParameters();

            for (var i = 0; i < methodParameters.Length; i++)
            {
                var parameter      = methodParameters[i];
                var parameterModel = CreateParameterModel(parameter);
                parameterModel.Handler = handlerModel;

                handlerModel.Parameters.Add(parameterModel);
            }

            return(handlerModel);
        }
예제 #5
0
        /// <summary>
        /// Creats a new instance of <see cref="PageHandlerModel"/> from a given <see cref="PageHandlerModel"/>.
        /// </summary>
        /// <param name="other">The <see cref="PageHandlerModel"/> which needs to be copied.</param>
        public PageHandlerModel(PageHandlerModel other)
        {
            if (other == null)
            {
                throw new ArgumentNullException(nameof(other));
            }

            MethodInfo  = other.MethodInfo;
            HandlerName = other.HandlerName;
            HttpMethod  = other.HttpMethod;
            Name        = other.Name;

            Page = other.Page;

            // These are just metadata, safe to create new collections
            Attributes = new List <object>(other.Attributes);
            Properties = new Dictionary <object, object>(other.Properties);

            // Make a deep copy of other 'model' types.
            Parameters = new List <PageParameterModel>(other.Parameters.Select(p => new PageParameterModel(p)
            {
                Handler = this
            }));
        }