TOC

This article is currently in the process of being translated into French (~42% done).

Models:

Introduction

Dans les chapitres précédents , nous avons vu plus en profondeur le C(contrôleur) et le V(vue) du pattern MVC et nous allons donc nous concentrer sur leM(modèle) dans ce chapitre. Nous le présentons en dernier car vous pouvez tout à fait construire un site internet basé sur le pattern MVC sans utiliser de modèle, même si cela restera un site très simpliste.

What is a Model?

Comme nous en avions parlé dans le début de ce tutoriel où nous avons rapidement ajouté un contrôleur, une vue, et un modèle à notre projet, un modèle dans une application MVC doit représenté l'état actuel de l'application ainsi que la logique commerciale et/ou les opérations. Un modèle peut être tout type d'objet trouvé dans le framework. Cela peut être dans le faits, un simple nombre , une chaine de caractère ou bien un objet complexe instancié depuis une classe(tel qu'une classe qui contient les informations à propos d'un utilisateur). Cela veut aussi dire que votre Modèle peut être une classe qui existe déjà (quelque chose qui vient de votre base de donnée) ou ou une classe que vous créez spécifiquement pour devenir un modèle pour une ou plusieurs vues.

A very important aspect of the MVC pattern is the Separation of Concerns. SoC is achieved by encapsulating information inside a section of code, making each section modular, and then having strict control over how each module communicates. For the MVC pattern, this means that both the View and the Controller can depend on the Model, but the Model doesn't depend on neither the View nor the Controller. This turns your Models into a module that can exist even outside of the MVC framework, e.g. for use in a desktop application, and then it has the added benefit of allowing your Models to be tested without the visual representation, e.g. through the use of unit tests.

As mentioned, the Model can be a class already defined in your project, or it can be a new class you add specifically to act as a Model. Therefore, Models in the ASP.NET MVC framework usually exists in a folder called "Models", or they come from outside of the project, e.g. from a class library. A Model doesn't have to inherit from a specific base class or anything like that - they are truly just regular .NET classes. There are a couple of ways to enrich your Model classes with MVC-related information though, but we'll discuss that later.

Summary

The Model in an MVC application should represent the current state of the application, as well as business logic and/or operations. In the upcoming articles of this chapter, we'll dig deeper into the more advanced, Model-related subjects.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!