2023-04-28 - MVC - Cookbook


main topics


Reassistindo

video

OOP Concept

OOP is all about state and behavior.

Class method vs. Intance method

Class method:

class Cat
  def self.branch
    "Mammals"
  end
end

puts Cat.branch
# => Mammals

felix = Cat.new
felix.branch
# NoMethodError

Instance method

class Cat
  def branch
    "Mammals"
  end
end

felix = Cat.new
felix.branch
# => Mammals

puts Cat.branch
# NoMethodError

Task Manager with MVC

  1. create Task class (Model).
  2. create Repository - holds instances of Task model.
  3. implemented Controller for CRUD actions.
  4. create View to do puts+gets
  5. create Router to send user to where they wanna go

My notes

All these files can look like an unnecessary extra complexity, but, trust me, they are useful.

It looks overkill here because we're using all these files for such a simple thing like a "todo manager", but in real life we're going to solve more complex problems (like financial transactions, booking apartments, etc.)


Video

MVC

UML diagram for the Task Manager:

MVC-UML.png

Development flow

First the Model, then the Repository (abstraction layer on top of the persistence), then the View, then the Controller

Model

You should always start with your model. The most important thing in your app is your data, and using models allows you to manipulate whatever data you have.

Repository

Structure to store user's data. It's an abstraction layer on top of the persistence.

Controller

Gather data from the Repository to hand them over to the View. Also asks the view for information to get more data.

View

Responsible to interact with the user.