I am at a point where I must make a decision about models. I am aware that models are what you use to do all your database manipulation. But are models restricted to this?
Are they meant only for database interaction or are they meant for all external data manipulation (e.g. data from external APIs etc)?
The Model is the part of MVC which implements the domain logic. In simple terms, this logic is used to handle the data passed between the database and the user interface (UI). The Model is known as domain object or domain entity. The domain objects are stored under the Models folder in ASP.NET.
In MVC M stands for Model and Model is a normal C# class. Model is responsible for handling data and business logic. A model represents the shape of the data. Model is responsible for handling database related changes.
The model classes represents domain-specific data and business logic in the MVC application. It represents the shape of the data as public properties and business logic as methods. In the ASP.NET MVC Application, all the Model classes must be created in the Model folder.
The central component of MVC, the model, captures the application's behavior in terms of its problem domain, independent of the user interface. The model directly manages the application's data, logic and rules. So the Model is the biggest, most important layer in most MVC applications.
The MVC paradigm is a design pattern where you organize your application in with the following structure.
The Model: this is where you should keep your data model, the algorithms. For example if you write a spreadsheet application, you would keep the data structure of your spreadsheet. You would have the computation engine in your model, you would have the code to save and load your spreadsheet in your model. These model class could potentially be reused in other applications, for example if you have code to do compression of data.
The View or views: these are the part of your code to visualize the data (the UI), for a spreadsheet you have a typical spreadsheet view with cells A1 to Z100 etc. You can also visualize your data using a chart view. Etc. A view could be reused in other application as well for example you could reuse your fancy chart view.
The controller is what connects the views to the model. This is probably the least reusable piece, the controller know about the model, know which views to displays. Typically the controller will setup the callback that the view will call when the user interact with the app. The controller will then get the information from the model and update the view.
If you follow these guidelines you might be able to change your model, for example change from a model that save files to a disk to a model that save files in the cloud without changing the UI... in theory. You could also be able to add new views without changing your model. You can also write unit tests or regression test for your models.
There is no strict rules, the best is to use common sense and your own judgment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With