Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC, where do the classes go?

My understanding of the MVC is as follows (incase it's horribly wrong, I am afterall new to it)

  1. Models are the things that interface with the database
  2. Views are the design/layout of the page
  3. Controllers are where everything starts and are essentially the page logic

I'm using CodeIgniter but I would hazard a guess it's not just limited to that or possibly even just to PHP frameworks.

Where do I put global classes?

I may have a model for Products and I then run a query that collects 20 products from the database. Do I now make 20 models or should I have a separate class for it, if the latter, where do I put this class (other controllers will need to use it too)

like image 391
Teifion Avatar asked Sep 26 '08 09:09

Teifion


People also ask

What is a class in MVC?

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.

Is it hard to learn MVC?

MVC is not all difficult. It is very easy compared to Framework of any other languages. However you should have good skills and understanding of C#. MVC is still very, VERY relevant today, as it was the solution of choice when building new project for quite a while net framework is not 100% out of date.


2 Answers

Model is the wrong word to use when discussing what to do with products: each product is a value object (VO) (or data transfer objet/DTO, whatever fits in your mouth better). Value objects generally have the same fields that a table contains. In your case ProductVO should have the fields that are in Products table.

Model is a Data Access Object (DAO) that has methods like

findByPk --> returns a single value object
findAll --> returns a collection of value objects (0-n)
etc.

In your case you would have a ProductDAO that has something like the above methods. This ProductDAO would then return ProductVO's and collections of them.

Data Access Objects can also return Business Objects (BO) which may contain multiple VO's and additional methods that are business case specific.

Addendum: In your controller you call a ProductDAO to find the products you want. The returned ProductVO(s) are then passed to the view (as request attributes in Java). The view then loops through/displays the data from the productVO's.

like image 176
kosoant Avatar answered Sep 18 '22 23:09

kosoant


Model is part of your application where business logic happens. Model represents real life relations and dependencies between objects, like: Employee reports to a Manager, Manager supervises many Employees, Manager can assign Task to Employee, Task sends out notification when overdue. Model CAN and most often DO interface with database, but this is not a requirement.

View is basically everything that can be displayed or help in displaying. View contains templates, template objects, handles template composition and nesting, wraps with headers and footers, and produces output in one of well known formats (X/HTML, but also XML, RSS/Atom, CSV).

Controller is a translation layer that translates user actions to model operations. In other words, it tells model what to do and returns a response. Controller methods should be as small as possible and all business processing should be done in Model, and view logic processing should take place in View.

Now, back to your question. It really depends if you need separate class for each product. In most cases, one class will suffice and 20 instances of it should be created. As products represent business logic it should belong to Model part of your application.

like image 3
Michał Niedźwiedzki Avatar answered Sep 22 '22 23:09

Michał Niedźwiedzki