当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET MVC Model绑定小结

ASP.NET MVC Model绑定小结

2018年09月19日  | 移动技术网IT编程  | 我要评论

自制去皱面膜的方法,后宫动漫无限加强,邻水房产

model绑定是指从url提取数据,生成对应action方法的参数这个过程。前面介绍的一系列descriptor负责提供了控制器,行为方法和参数的元数据,valueprovieder负责获取数据,剩下的moder绑定则是利用上述两者的结果来最终达到model的生成。parameterdescriptor里面的bindinginfo(类型是parameterbindinginfo)的binder是一个实现了imodelbinder接口的对象,modelbinder用于真正的model绑定。

 

modelbinder组件

下面则先看组件的类图

 

 

 

imodelbinder:bindmodel方法,实现model绑定功能的方法,传入controllercontext和modelbindingcontext返回绑定完毕的model。

 

custommodelbinderattribute:抽象特性,完成model与modelbinder之间的绑定处理,里面的getbinder方法用于获取modelbinder。

 

modelbinderattribute:custommodelbinderattribute的唯一继承,可用于参数,接口,枚举,结构,类的特性中,但是只有作为action方法的参数特性时才会在parameterdescriptor解析的时候解析出来。

 

imodelbinderprovider:mvc框架中没有一个类实现这个接口,主要用于进行自定义扩展。getbinder方法用于按指定类型获取modelbinder。

 

modelbinderprovidercollection:对于imodelbinderprovider的一个集合。

 

modelbinderproviders:拥有只读属性binderproviders,是modelbinderprovidercollection类型。

 

modelbinderdictionray:imodelbinder的字典集合,以model的数据类型type为key,对应的modelbinder为value的字典集。

 

modelbinders:具有binders的只读属性,类型是modelbinderdictionray,只用于获取modelbinderdictionray。

 

获取modelbinders的机制:它一般是先从parameterdescription里面检查modelbinder是否存在,如果该modelbinder不存在,就使用modelbinders.binders属性中获取,实际上是从modelbinderdictionary的内部集合_innerdictionary或_modelbinderproviders中获取;如果在上述两个集合中也无法获取,则使用modelbinderdictionary中的默认molderbinder——_defaultbinder。

 

按照上述的获取机制,在定制数据类型的modelbinder时,使用不同的方式,modelbinder获取时也会有不同的优先级:

 

  1)为action方法上的参数加上modelbinderattribute或custommodelbinderattribute特性;

 

  2)自定义modelbinderprovider未某种model数据提供modelbinder,使用modelbinderproviders.binderproviders.add方法添加到集合中去

 

  3)为model类型加上modelbinderattribute或custommodelbinderattribute特性

 

  4)通过modelbinders的binders属性添加modelbinder。

 

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网