Model Binding within a Model Binder

我与影子孤独终老i 提交于 2019-12-04 22:03:12

My take on this is that model binders should be constructing presentation models, not entity types from your repository. The model binder should be a very simple mapping from the key/value collection of the form to a presentation model which is mostly scalar values with possibly some relationships to other types that are mostly scalar values or lists. Having to materialize entity instances from a repository adds a lot of complication, as you have found.

Moreover, it's unnecessary. Using a presentation model has a large number of advantages, including:

  • There is never a need to whitelist the fields the user is allowed to update, since the presentation model contains only those fields.
  • The default model binder will work for all but the most complex model binding scenarios. In practice, I find that I only need to use a custom model binder when the value the user sees has to be bound to some other value in a conditional manner. When using a presentation model the structure of your presentation model should match the structure of the page, so you do not need to use a custom model binder for structural reasons.
  • You will be able to create your views and controllers before creating a database or entity model. This means you can get customer buy-in on your design before doing a large amount of the work to create the final system. This helps to sort out structural issues in the entity model before they happen. Just create a presentation model which matches the page you think the customer wants to see, build the general outline of the page using a made-up instance of this presentation model, and show it to the customer. If they're happy, you can then build the repository/entity model and write a LINQ query to map that to your presentation model.

So in your example, the subcategories would come in from the form collection as a list of integers. Therefore, the presentation model should have the same list of integers. In the controller, after binding, you can call a method to transfer the model values from the presentation model to a materialized category instance from the repository.

I would recommend taking a look at this Singing Eels post, which gives an example of another approach. Using the sample StatefulObjectBinder approach, it is possible to bind collections of business objects which must be retrieved from the database. Since the controller is implementing IModelBinder, you have access to a repository, which can be used to hydrate the required objects and add them to the object collection.

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