2

What is a proper use of model in asp mvc?

Should a model contain only data that will be accessed inside a form, or it is also a good idea to put data that are static and would be used in a view, but not inisde a form of a view.

I am asking this because I am curious about relevance of a model size on performance. If this static data should not be kept in a model, what would be a proper place for it - maybe ViewData, ViewBag or TempData. I still find it easy to store it in a model, and specify what data not to update on model update.

I would appreciate your comments..

  • Is there a reason you tagged your question as “asp-classic”? ASP is this old thing used during the Windows 98 era and discontinued 15 years ago. – Arseni Mourzenko Feb 24 '15 at 13:54
  • @MainMa Not actually discontinued, you can still run it on Windows 2012 Server so it is still supported. :-) But yes, it certainly is not MVC/.Net. The issue is that ASP is a Synonym for asp-classic so anyone entering asp gets auto changed to asp-classic. – Dijkgraaf Dec 23 '15 at 3:08
3

Your confusion is understandable. Different MVC frameworks have different approaches when it comes to models: some encourage lightweight models, while others welcome models which contain a lot of information.

In ASP.NET MVC, the common practice I observe in many projects is the following:

Business logic:

  • Models don't contain business logic. In other words, they don't perform anything which involves business rules—this is the task of a controller and the classes used by the controller.

  • Models don't contain elementary logic which is usually performed by a view. For instance, if the page should display a date in a human-friendly format, such as “November 15th, 2013”, the controller will just use DateTime object which will remain as-is in the model. It's the view which will be in charge of the conversion from DateTime to a specific format (eventually using localization).

Database access:

  • Models don't access database directly. This may be obvious for you, but the predecessor—ASP.NET, had often SQL queries within the templates, something which would be considered insane by most developers today.

  • Models can contain lazy IEnumerable<T> elements which lead to the database; neither the model, nor the view are defining how the data is loaded, but still, the loading itself is postponed until the view is displayed, instead of being done by the controller.

Data:

Remains obviously the data itself, that is the data loaded by the controller and transferred to a view through a model. In a model, you'll generally find all the data required to generate the page, except the static content. For instance, on a web page which displays the products, the model could contain:

  • The list of products (in a form of a lazy IEnumerable<T>),
  • The information about the user, if the user is authenticated,
  • The cart,

and won't contain elements such as:

  • The logotype of the website,
  • The site menu,
  • The footer with copyright notice,

since they can reside in the view.

Model, ViewData, ViewBag and TempData

Since the release of the DLR, it is not unusual to see data in places other than the model, resulting in more confusion. Even the ASP.NET MVC demo project moves a lot of info from the model into the ViewBag.

The choice here is basically the same as the choice between static typing and dynamic typing, with one difference.

  • What is the same is that you can if you want move everything to ViewBag and avoid using a model, or keep everything in the model and avoid using the ViewBag: in a case of a model, you have the benefit of static typing, which often means stricter code with bugs found at compile time instead of run time.

    For instance, if you are expecting a date in your model and your view is extracting information from this date (such as displaying it in humanly readable format), the controller cannot possibly pass a string. Visual Studio will alert you by underlining the string, and if you try to compile, you'll see an error.

    If, instead, the date is in the ViewBag and instead of a DateTime object, you put a string, Visual Studio will be fine with that, and the compiler won't tell you anything. You'll have to wait until you run the page to see that something went wrong.

  • The difference, on the other hand, is that ASP.NET MVC's views have a special status of being second-class citizens. Since by default, they are not compiled, it's very easy to make mistakes in the views themselves (such as expecting a DateTime while the model indicates that it contains a string).

    This means that there is nothing wrong to heavily rely on the dynamic aspects of ViewBag in ASP.NET MVC. If you know that your model and your view will change a lot, go with the dynamic ViewBag. If you know that the model is well defined and is not subject to change unless the requirements change, the real, statically typed model would probably be a better choice.

Another aspect which should be noted is performance. If you're building a large scale web application, you should definitively compare the performance of statically-typed models with the performance of the dynamic ViewBag. I remember that my benchmarks for a project which had nothing to do with ASP.NET MVC showed that switching between static types and dynamic has practically no difference in terms of performance, but still, make sure you do the actual benchmarks.

  • This is very interesting explanation. But somehow I think I wasn't clear enough. I was referring to a use of @model (referenced in View). Basically i am using ViewModel technique that is declared with ViewModel classed and populated from MVC controller. I really wanted to know what should be a part of @@model (ViewModel)? I hope my questions is better structured now. – John Feb 24 '15 at 14:05
  • 1
    @John: When I talk about a model in my answer, I am actually talking about the class behind the @model. So I think we are talking about the same thing. – Arseni Mourzenko Feb 24 '15 at 14:07
  • Could you help me where is data for @model stored after being populated in controller? – John Feb 24 '15 at 14:34
  • @John: on the heap, as any other object. The model (as an instance) is just an object initialized by the controller and passed through MVC's pipeline to the view. – Arseni Mourzenko Feb 25 '15 at 11:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.