Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing the line between the model and the Controller

I'm building this restful application using RoR, and am finding it a bit difficult to draw a line between things that should go on the model, and things that should go on the controller.

As an example, I have 7 methods on my controller (the ones that make it restful i.e. index(), show(), create(), update()...), and often find that it's necessary to add extra methods, and do so by creating them as members.

What I'd like to accomplish here, is gather experience from you guys on what goes where (i.e. should I stick all the database interactions on the model, and simply call those methods from the controller?)

Also, by adding things that don't involve DB to my controller i.e. I want to make an HTTP call to screen-scrape some data from a website.

HTTP calls can get big and messy. Should all this go to my controller, or should this go an a separate class or module, and only be included to my controller so it can be called?

If so, what would be the best approach to do that?

I'm a bit confused with all this, so it would be great to have somebody's input.

Thanks in advance

like image 774
Marcos Placona Avatar asked Mar 02 '10 12:03

Marcos Placona


People also ask

What is the difference between model and controller?

The model is responsible for managing the data of the application. It receives user input from the controller. The view renders presentation of the model in a particular format. The controller responds to the user input and performs interactions on the data model objects.

How model view and controller interact with each other?

First, the browser sends a request to the Controller. Then, the Controller interacts with the Model to send and receive data. The Controller then interacts with the View to render the data. The View is only concerned about how to present the information and not the final presentation.

What is the purpose of separating model view and controller?

MVC (Model-View-Controller) is a pattern in software design commonly used to implement user interfaces, data, and controlling logic. It emphasizes a separation between the software's business logic and display. This "separation of concerns" provides for a better division of labor and improved maintenance.

What are the three 3 parts of model-view-controller pattern?

-MVC is an architectural pattern consisting of three parts: Model, View, Controller. Model: Handles data logic. View: It displays the information from the model to the user. Controller: It controls the data flow into a model object and updates the view whenever data changes.


4 Answers

It is a part of Domain Driven Design.

The Domain is the sphere of knowledge that defines the area that the application is attempting to work with and solve problems for.

The Model layer is considered the Domain layer. It is here that all the rules that define the Domain or business logic are kept. The model acts as a filter between the real data and the rest of the app in a way that defines the domain.

The implementation details of the Domain (mySQL or MSSql or Webservice or Xml file or external web page or whatever) are hidden by the Model.

The Controller is just the messenger. Its job is to take messages from the user and pass them to the Model. The Model comes up with a reply and the Controller works out the best way to pass this on to the user.

The View is like the make up artist, just making sure that the data looks good and presentable for the user.

The website that you are scraping could be considered to be part of the Domain. This site contains knowledge that defines the world that your app is defining. The screenscraping process is sculpting that knowledge in a way such that it will relate to the rest of the world view that your app is defining. The Controller doesnt care where this knowledge comes from, it will just pass the message on to the View. The View will fuss over the data and make it pretty and send it off to the user, who is completely oblivious to the whole process and just sees a pretty web page!

like image 200
Mongus Pong Avatar answered Oct 03 '22 10:10

Mongus Pong


Generally speaking, a good approach (no religion flame, plz) is to have thin controllers and fat models. In that way you can also easily test "the things" the models are supposed to do...

like image 24
Carlo Pecchia Avatar answered Oct 03 '22 09:10

Carlo Pecchia


The scraping stuff should go in a non ActiveRecord model, and the controller should just invoke methods of this model.

There is a nice example of a tableless AR model, and one of a non-AR model on railscasts.

So, your controller would do something like:

def index
  @some_things = SomeThing.all
end

def show
  @some_thing = SomeThing.find params[......]
end

And your SomeThing model would implement the needed logic.

like image 39
giorgian Avatar answered Oct 03 '22 09:10

giorgian


I stick to the rules of thin controller and fat models.

Given a restful approach I keep all controllers to this standard and have no further methods in them outside the default. If I require new functionality and note the methods do similar things (or would have a similar naming e.g group_add , group_remove ) I create a new controller and use existing models and create new methods in the model.

This also allows a restful approach to these further actions in a meaningful way and specifically each action has a narrowly defined spec for its operation, it will never do more than one thing. Nothing worse than using an API call that does two things dependent upon how its called.

An approach I use is to handle the request as 'fast' as possible by using as little code as needed and abstracting complex tasks/operations to the model.

like image 45
David Lyod Avatar answered Oct 03 '22 11:10

David Lyod