Drawing the line between the model and the Controller

model-view-controller, ruby, ruby-on-rails

Solution

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!

Problem

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

Original source

Related problems