Are strongly typed ASP.NET MVC views smelly for composite models

Consider 2 options for a real application for which you want to build a model, a composite model, if necessary, from a variety of sources for which strictly typed representations are used:

  • Create a new custom class for the model with a property for each piece of information that should be passed to the viewing stage,

  • Limit the models to the source classes of the domain and connect several controllers when using Html.RenderAction.

The disadvantage of option 1 is the need to create many "plumbing" classes. The downside of option 2 is the dirty sense of breaking the separation of problems and the more specific problem of tracking controller chains to find the one that selected a certain piece of HTML.

Before ASP.Net MVC, I had been using Maverick.Net for many years, and with it the model was a piece of XML code for the XSL lookup phase. In ASP.Net MVC, which would be the equivalent of passing the model to the presentation stage as a set of key value pairs in the ViewDictionary. The disadvantage of this is that key key name checks are not performed, but in my experience, key names change very rarely.

An interesting proposed solution to this problem is to invent dynamic partial classes .

So, a strongly typed view with ASP.Net MVC is used - a poor approach for composite models and a better, simpler and more efficient way to use the rude ViewDictionary approach?

+3
source share
2 answers

On the contrary, you should always strive to use strongly typed representations if you do not have a model at all. By creating an explicit presentation model, you not only get compile-time checks, but also improve the intent, openness, and readability of your code.

+4
source

Automapper ViewModels, .. " ", , , Automapper , .

Automapper .

, , ViewDictionary .

0

Source: https://habr.com/ru/post/1754529/


All Articles