asp.net-mvc-2 – ASP.NET MVC2 ModelMetadataProviders:重写Create
|
从MetadataProviders开始,我很想探索框架的扩展点.我目前已经成功实施了
populating ModelMetadata.IsRequired property using 通常,我见过的示例会覆盖CreateMetadata(). >使用这两种选择的利弊是什么? 作为额外的:是否有任何好的资源(博客,书籍)可以从这个扩展点学习? 解决方法GetMetadataForProperty()在类ModelMetadataProvider上声明.AssociatedMetadataProvider派生自ModelMetadataProvider. CreateMetadata()在AssociatedMetadataProvider上声明.在您提供的链接中重写的DataAnnotationsMetadataProvider派生自AssociatedMetadataProvider. MVC框架调用ModelMetadataProvider的GetMetadataForProperty()方法. 覆盖CreateMetadata()的原因是因为AssociatedModelMetadataProvider的默认GetMetadataForProperty()实现了对CreateMetadata()的调用.它看起来像这样: public override ModelMetadata GetMetadataForProperty(Func<object> modelAccessor,Type containerType,string propertyName)
{
if (containerType == null)
{
throw new ArgumentNullException("containerType");
}
if (string.IsNullOrEmpty(propertyName))
{
throw new ArgumentException(MvcResources.Common_NullOrEmpty,"propertyName");
}
PropertyDescriptor propertyDescriptor = this.GetTypeDescriptor(containerType).GetProperties().Find(propertyName,true);
if (propertyDescriptor == null)
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,MvcResources.Common_PropertyNotFound,new object[] { containerType.FullName,propertyName }));
}
return this.GetMetadataForProperty(modelAccessor,containerType,propertyDescriptor);
} protected virtual ModelMetadata GetMetadataForProperty(Func<object> modelAccessor,PropertyDescriptor propertyDescriptor)
{
IEnumerable<Attribute> attributes = this.FilterAttributes(containerType,propertyDescriptor,propertyDescriptor.Attributes.Cast<Attribute>());
return this.CreateMetadata(attributes,modelAccessor,propertyDescriptor.PropertyType,propertyDescriptor.Name);
}
如果您正在将AssociatedMetadataProvider子类化为您提供的链接,则首选的可扩展性点是CreateMetadata方法,因为AssociatedMetadataProvider.GetMetadataForProperty()方法预先验证CreateMetadata()方法的合约.这样,您就知道如果您的CreateMetadata()方法中存在错误,您已经知道错误的来源在您的方法中,而不是在传递给它的参数中. 另外,这里是FilterAttributes()方法的源代码,以防你想知道: protected virtual IEnumerable<Attribute> FilterAttributes(Type containerType,PropertyDescriptor propertyDescriptor,IEnumerable<Attribute> attributes)
{
if (!typeof(ViewPage).IsAssignableFrom(containerType) && !typeof(ViewUserControl).IsAssignableFrom(containerType))
{
return attributes;
}
return attributes.Where<Attribute>(delegate (Attribute a) {
return !(a is ReadOnlyAttribute);
});
} (编辑:日照站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 在ASP.NET中拒绝用户时,’CustomIdentity’上的Serializati
- asp.net-mvc – webgrid中的Mvc 3 texbox(razor)
- linq – ASP.NET Web API GET方法:为单个参数传递多个值
- asp.net-mvc – 如何在我的Asp.net Mvc中使用linq2sql存储库
- asp.net-mvc – 我可以获取html.HiddenFor / Html.Hidden创
- asp.net-mvc – 我需要有关HandleError的更多信息
- asp.net – 为什么Global.asax事件在我的ASP.NET网站没有触
- asp.net-mvc-3 – 为什么两个类,视图模型和域模型?
- asp.net-mvc – 如何将XML作为POST传递给ASP MVC .NET中的A
- asp.net – 最后修改标头在MVC
- asp.net – 检查IE浏览器 – .NET
- asp.net – 为每个网站/应用程序创建单独的IIS应
- asp.net-mvc-3 – 为MVC3应用程序配置Ninject的正
- asp.net-mvc – ASP.NET MVC中的Windows Live ID
- asp.net中XML如何做增删改查操作
- asp.net – CalendarExtender定位问题
- asp.net-mvc – MVC 3布局页面,Razor模板和下拉列
- ASP.NET中读取XML文件信息的4种方法与示例代码
- asp.net-mvc – 使用CORS在WebAPI中将text / pla
- 使用Asp.net Web API时,使用DataContract和DataM
