Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC: Where should I put this data access logic?

I've been trying to adhere to a strict interpretation of MVC in rebuilding a personal web application at home, but I'm having some issues.

The application is a financial tracking application. I'm stuck at the first page, which is just a list of the bank account transactions for the month. The transaction list is my Model. The Controller and View are easy enough to envision at this point.

However, I also have two buttons at the top of the page. They are arrows, one corresponding to the previous month, and the other corresponding to the next month's transaction list. I don't want these buttons enabled if there are no transactions for the previous/next month, so I need to talk to the database to figure out whether each button should actually link somewhere.

From most of what I've read, to the largest extent possible database access should be encapsulated in models, with little to no database access in Controllers and Views.

However, these buttons essentially need to ask the database, "Are there any transactions for the next/previous months?" The answer will determine whether or not their links are disabled, as well as where to send the user.

Strictly speaking, putting their logic into the Transaction List model does not seem appropriate, as whether or not transactions exist outside the requested range is beyond the concern of the Transaction List Model.

I guess I could also make another Model that would correspond to all Year-Month combinations where transactions exist. I could then pass this Model and the Transaction List on to the proper View.

Or should I deviate from the no-database-access-outside-the-model paradigm, and just throw some quick DB querying into the View (since the result is essentially a UI concern, making navigation easier)?

What do you guys think about this conceptual issue?

BTW, I am not using any framework. This is pet project designed to get me more familiar with the nuts and bolts of the MVC pattern.

like image 394
Clayton Roth Avatar asked Aug 21 '12 18:08

Clayton Roth


People also ask

Where do you put business logic in MVC?

The business logic should be placed in the model, and we should be aiming for fat models and skinny controllers. As a start point, we should start from the controller logic. For example: on update, your controller should direct your code to the method/service that delivers your changes to the model.

Where does logic for the view belong in MVC?

The Purpose of MVC Framework Thus, the information (most reusable) logic belongs in the model, the GUI belongs in the view. Input logic belongs in the controller.

Should the controller interact with the database?

The controller should not need to know anything about the underlying structure of the model in order to interact with it. The model should have no knowledge of how the data is to be displayed (e.g., formatting) or the workflow.

Should business logic go in controller?

Let me tell you this, controllers shouldn't do anything remotely related to business logic, and directly access data stores. The controller's only purpose is to receive a request and return a response. Everything that goes in between is not its responsibility.


2 Answers

... should I deviate from the no-database-access-outside-the-model paradigm, and just throw some quick DB querying into the View ...

The short answer is: no. If you're truly interested in learning MVC and better coding in general, taking shortcuts "just because" is a very bad idea. Domain logic separation is a fundamental concept of MVC, and to violate it would essentially be moving out of MVC into territory that is dominated by terrible code and "MVC Frameworks." If you want to learn "the nuts and bolts of MVC," you'll have to understand that the nuts and bolts are things like separation of concerns, and they are very important.

A lot of the text in your question suggest a few mixed ideas about what a model should be, so I'm going to redirect you to the most linked-to answer regarding MVC models. That answer encompasses everything you'll want to know about the concept of models. To summarize the most important concept from it, a model is not a class, it's a layer. The model layer has many components that all do their own thing, which leaves you with a testable, extensible, semantic, and logical domain logic layer.

Just remember to be careful: There is way more mis-information out there about MVC than there is reliable information. The worst thing you can do to learn proper MVC is look inside of a framework that claims to be MVC.

edit: I assumed the language was PHP while responding, so some of my answer might reflect that assumption. However the concepts are applicable across nearly all languages

like image 164
orourkek Avatar answered Oct 16 '22 14:10

orourkek


The application is a financial tracking application. I'm stuck at the first page, which is just a list of the bank account transactions for the month. The transaction list is my Model. The Controller and View are easy enough to envision at this point.

Everything on your web page, is categorised in the View domain. A "list" is a visual object.

For a web application/page, The model exists on your server, within your database. Everything that exists on the web page, (Buttons, Forms, List boxes etc) is you View domain.

From most of what I've read, to the largest extent possible database access should be encapsulated in models, with little to no database access in Controllers and Views.

The View objects traditionally talk directly to your Model objects. Which means you have to provide a way for your View objects to talk to your database.

An example,

  • http//server/transactions (Get all transactions)
  • http//server/transactions?from=x (Get transactions from x)
  • http//server/transactions?from=x&to=y (Get transactions from x to y)

Could be your interface to your Model by your View.

like image 1
Michael Brown Avatar answered Oct 16 '22 15:10

Michael Brown