Is there a reason why the default modelbinder doesn't bind to fields?

谁说我不能喝 提交于 2019-12-18 08:29:07

问题


I'm using ASP.NET MVC3 and i'm wondering that the default modelbinder binds to public properties but not to public fields.

Normally i just define the model classes with properties but sometimes i use some predefined classes which contains some fields. And everytime i have to debug and remember that the modelbinder just don't like fields.

The question: Whats the reason behind it?


回答1:


but sometimes i use some predefined classes which contains some fields

While I cannot answer your question about the exact reason why the default model binder works only with properties (my guess is that it respects better encapsulation this way and avoids modifying internal state of the object which is what fields represent) I can say that what you call predefined classes should normally be view models. You should always use view models to and from your controller actions. Those view models are classes that are specifically defined to meet the requirements of the given view.

So back to the main point: fields are supposed to be modified only from within the given class. They should not be accessed directly from the outside. They represent and hold internal state of the class. Properties on the other hand is what should be exposed to the outside world. Imagine that in the property getter/setter you had some custom logic. By modifying directly the field this custom logic would be broken and potentially bring the object into an inconsistent state.




回答2:


Maybe the reason for ignoring fields is to increase performance of the binder. Instead of searching all the Fields and properties. The Model Binder search for Properties only.

Though I think the Model Binder use cache to improve performance.




回答3:


DefaultModelBinder exposes a public method: DefaultModelBinder.BindModel, and a number of protected method available for overriding. All of them listed here.

Besides the model, these method refer to properties only, not fields, like

  • GetModelProperties,
  • GetFilteredModelProperties,
  • GetPropertyValue,
  • OnXYZValidating,
  • OnXYZValidated,
  • OnXYZUpdating,
  • OnXYZUpdated,
  • GetXYZValue,

where XYZ stands for either Model, or Property/ies, or both, and so on.

As you can see there is no Fields mentioned with these names whatsoever. As Darin explained no direct changes to Model's state are tolerated by the Binder. Hence no Field in its methods.

And also, you may wish to take a look at another important class: ModelBindingContext. An instance of this class gets passed to the BindModel, and subsequently to BindSimpleModel, and BindComplexModel, depending on model type (string, int,... are considered simple, everything else is complex).

So, this context has the following properties:

  • ModelXYZ, and
  • PropertyXYZ.

In other words you have no means to reference the fields in your ViewModel unless you do not override these classes and undertake special actions to do so.

But again, beware of fighting the framework, its always easier to follow it instead.

EDIT: The ModelMetadata class holds all the data needed to bind the model. Its code however, shows no sign of fields, field names, etc. Only properties are referenced and accessed. So, even if you try to inherit and override DefaultModelBinder and ModelBinderContext, you still won't be able to access fiellds, nevermind what their access modifier is: public, private, etc.

Hope this explains most of it.



来源:https://stackoverflow.com/questions/8559060/is-there-a-reason-why-the-default-modelbinder-doesnt-bind-to-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!