public void CreateBoundProperties_CopiesPropertyDescriptorsFromPagePropertyModel() { // Arrange var actionDescriptor = new PageActionDescriptor(); var handlerTypeInfo = typeof(HandlerWithProperty).GetTypeInfo(); var propertyModel = new PagePropertyModel( handlerTypeInfo.GetProperty(nameof(HandlerWithProperty.Property)), new object[0]) { PropertyName = nameof(HandlerWithProperty.Property), BindingInfo = new BindingInfo(), }; var pageApplicationModel = new PageApplicationModel(actionDescriptor, handlerTypeInfo, new object[0]) { HandlerProperties = { propertyModel, } }; // Act var propertyDescriptors = CompiledPageActionDescriptorBuilder.CreateBoundProperties(pageApplicationModel); // Assert Assert.Collection( propertyDescriptors, p => { Assert.Same(propertyModel.PropertyName, p.Name); Assert.Same(typeof(int), p.ParameterType); Assert.Same(propertyModel.PropertyInfo, p.Property); Assert.Same(propertyModel.BindingInfo, p.BindingInfo); }); }
public void ApplyConventions_InvokesPropertyModelConventions_DeclaredGlobally() { // Arrange var descriptor = new PageActionDescriptor(); var methodInfo = GetType().GetMethod(nameof(OnGet), BindingFlags.Instance | BindingFlags.NonPublic); var propertyInfo = GetType().GetProperty(nameof(TestProperty), BindingFlags.Instance | BindingFlags.NonPublic); var applicationModel = new PageApplicationModel(descriptor, typeof(object).GetTypeInfo(), Array.Empty <object>()); var handlerModel = new PageHandlerModel(methodInfo, Array.Empty <object>()); var propertyModel = new PagePropertyModel(propertyInfo, Array.Empty <object>()); applicationModel.HandlerMethods.Add(handlerModel); applicationModel.HandlerProperties.Add(propertyModel); var propertyModelConvention = new Mock <IParameterModelBaseConvention>(); propertyModelConvention.Setup(p => p.Apply(It.IsAny <ParameterModelBase>())) .Callback((ParameterModelBase m) => { Assert.Same(propertyModel, m); }) .Verifiable(); var conventionCollection = new PageConventionCollection { propertyModelConvention.Object }; // Act DefaultPageLoader.ApplyConventions(conventionCollection, applicationModel); // Assert propertyModelConvention.Verify(); }
public void ApplyConventions_RemovingPropertyModelAsPartOfConvention_Works() { // Arrange var descriptor = new PageActionDescriptor(); var propertyInfo = GetType().GetProperty(nameof(TestProperty), BindingFlags.Instance | BindingFlags.NonPublic); var applicationModel = new PageApplicationModel(descriptor, typeof(object).GetTypeInfo(), Array.Empty <object>()); var propertyModelConvention = new Mock <IParameterModelBaseConvention>(); var propertyModel = new PagePropertyModel(propertyInfo, new[] { propertyModelConvention.Object }) { Page = applicationModel, }; applicationModel.HandlerProperties.Add(propertyModel); propertyModelConvention.Setup(p => p.Apply(It.IsAny <ParameterModelBase>())) .Callback((ParameterModelBase m) => { var model = Assert.IsType <PagePropertyModel>(m); model.Page.HandlerProperties.Remove(model); }) .Verifiable(); var conventionCollection = new PageConventionCollection(); // Act DefaultPageLoader.ApplyConventions(conventionCollection, applicationModel); // Assert propertyModelConvention.Verify(); }
public void ApplyConventions_InvokesPropertyModelConventions() { // Arrange var descriptor = new PageActionDescriptor(); var methodInfo = GetType().GetMethod(nameof(OnGet), BindingFlags.Instance | BindingFlags.NonPublic); var propertyInfo = GetType().GetProperty(nameof(TestProperty), BindingFlags.Instance | BindingFlags.NonPublic); var parameterInfo = methodInfo.GetParameters()[0]; var applicationModel = new PageApplicationModel(descriptor, typeof(object).GetTypeInfo(), Array.Empty <object>()); var handlerModel = new PageHandlerModel(methodInfo, Array.Empty <object>()); var parameterModel = new PageParameterModel(parameterInfo, Array.Empty <object>()); var propertyModelConvention = new Mock <IParameterModelBaseConvention>(); var propertyModel = new PagePropertyModel(propertyInfo, new[] { propertyModelConvention.Object }); applicationModel.HandlerMethods.Add(handlerModel); applicationModel.HandlerProperties.Add(propertyModel); handlerModel.Parameters.Add(parameterModel); propertyModelConvention.Setup(p => p.Apply(It.IsAny <ParameterModelBase>())) .Callback((ParameterModelBase m) => { Assert.Same(propertyModel, m); }) .Verifiable(); var conventionCollection = new PageConventionCollection(Mock.Of <IServiceProvider>()); // Act CompiledPageActionDescriptorFactory.ApplyConventions(conventionCollection, applicationModel); // Assert propertyModelConvention.Verify(); }
/// <summary> /// Creates a <see cref="PagePropertyModel"/> for the <paramref name="property"/>. /// </summary> /// <param name="property">The <see cref="PropertyInfo"/>.</param> /// <returns>The <see cref="PagePropertyModel"/>.</returns> protected virtual PagePropertyModel CreatePropertyModel(PropertyInfo property) { if (property == null) { throw new ArgumentNullException(nameof(property)); } var propertyAttributes = property.GetCustomAttributes(inherit: true); var propertyMetadata = _modelMetadataProvider.GetMetadataForProperty(property.DeclaringType, property.Name); var bindingInfo = BindingInfo.GetBindingInfo(propertyAttributes, propertyMetadata); if (bindingInfo == null) { // Look for binding info on the handler if nothing is specified on the property. // This allows BindProperty attributes on handlers to apply to properties. var handlerType = property.DeclaringType; var handlerAttributes = handlerType.GetCustomAttributes(inherit: true); var handlerMetadata = _modelMetadataProvider.GetMetadataForType(property.DeclaringType); bindingInfo = BindingInfo.GetBindingInfo(handlerAttributes, handlerMetadata); } var model = new PagePropertyModel(property, propertyAttributes) { PropertyName = property.Name, BindingInfo = bindingInfo, }; return(model); }
/// <summary> /// Creates a new instance of <see cref="PagePropertyModel"/> from a given <see cref="PagePropertyModel"/>. /// </summary> /// <param name="other">The <see cref="PagePropertyModel"/> which needs to be copied.</param> public PagePropertyModel(PagePropertyModel other) : base(other) { if (other == null) { throw new ArgumentNullException(nameof(other)); } Page = other.Page; BindingInfo = other.BindingInfo == null ? null : new BindingInfo(other.BindingInfo); PropertyInfo = other.PropertyInfo; }
/// <summary> /// Creates a <see cref="PagePropertyModel"/> for the <paramref name="property"/>. /// </summary> /// <param name="property">The <see cref="PropertyInfo"/>.</param> /// <returns>The <see cref="PagePropertyModel"/>.</returns> protected virtual PagePropertyModel CreatePropertyModel(PropertyInfo property) { if (property == null) { throw new ArgumentNullException(nameof(property)); } var attributes = property.GetCustomAttributes(inherit: true); var bindingInfo = BindingInfo.GetBindingInfo(attributes); var model = new PagePropertyModel(property, property.GetCustomAttributes(inherit: true)) { PropertyName = property.Name, BindingInfo = bindingInfo, }; return(model); }
/// <summary> /// Creates a <see cref="PagePropertyModel"/> for the <paramref name="property"/>. /// </summary> /// <param name="property">The <see cref="PropertyInfo"/>.</param> /// <returns>The <see cref="PagePropertyModel"/>.</returns> protected virtual PagePropertyModel CreatePropertyModel(PropertyInfo property) { if (property == null) { throw new ArgumentNullException(nameof(property)); } var propertyAttributes = property.GetCustomAttributes(inherit: true); // BindingInfo for properties can be either specified by decorating the property with binding-specific attributes. // ModelMetadata also adds information from the property's type and any configured IBindingMetadataProvider. var propertyMetadata = _modelMetadataProvider.GetMetadataForProperty(property.DeclaringType, property.Name); var bindingInfo = BindingInfo.GetBindingInfo(propertyAttributes, propertyMetadata); if (bindingInfo == null) { // Look for BindPropertiesAttribute on the handler type if no BindingInfo was inferred for the property. // This allows a user to enable model binding on properties by decorating the controller type with BindPropertiesAttribute. var declaringType = property.DeclaringType; var bindPropertiesAttribute = declaringType.GetCustomAttribute <BindPropertiesAttribute>(inherit: true); if (bindPropertiesAttribute != null) { var requestPredicate = bindPropertiesAttribute.SupportsGet ? _supportsAllRequests : _supportsNonGetRequests; bindingInfo = new BindingInfo { RequestPredicate = requestPredicate, }; } } var model = new PagePropertyModel(property, propertyAttributes) { PropertyName = property.Name, BindingInfo = bindingInfo, }; return(model); }