50 Shades of MVC

50 Shades of MVC

Video: https://vimeo.com/89377195

We at the Rails world are very proud of embracing the Model-View-Controller design pattern, it is integral to the way we build web applications and there is a general consensus that all of us generally know what we are talking about in regards to it, hell , most of our code files are under a model, controller or views directories! We've got to know what we are doing, right?

Much like Object Orientation the Model-View-Controller pattern is one of the most popular-yet-heavily-altered concepts in modern computer science, what were the original propositions of the pattern? How was it applied back in the 70's, when it was proposed as a part of Smalltalk? How much have we changed it to adapt it to the web application scene? How can we apply this to our day to day work in 2013? is altering the original pattern necessarily a bad thing?

On this talk I present different aspects of the MVC pattern and its changes from its inception to modern day use, what to keep in mind about the pattern as opposed to a single framework's implementation of it and how knowing this should make us all around better programmers.

6ed7c9c01ba124114f965ad3660bc276?s=128

Pablo Astigarraga

June 07, 2013
Tweet

Transcript

  1. 50 Shades of MVC

  2. Pablo Astigarraga

  3. ! @poteland github.com/pote

  4. tarmac.io

  5. Surculus Fructum

  6. Uruguay

  7. Uruguay

  8. Argentina

  9. None
  10. ! Thank you RubyConf Ar <3

  11. Model View Controller

  12. 2004 - 2005

  13. None
  14. Models Data + Behavior

  15. Views User Interface !

  16. Controllers Glue. (?)

  17. None
  18. Browser GET / HTTP/1.1

  19. Web Server

  20. Router MyApp::Application.routes.draw do! get '/' => 'sessions#new'! root :to =>

    'sessions#new' ! end!
  21. Router MyApp::Application.routes.draw do! get '/' => 'sessions#new'! root :to =>

    'sessions#new' ! end!
  22. Router MyApp::Application.routes.draw do! get '/' => 'sessions#new'! root :to =>

    'sessions#new' ! end!
  23. Controller class SessionsController < ApplicationController! def new! @model = FictionalModel.last!

    render :new! end! end!
  24. Model class FictionalModel < ActiveRecord::Base! # validations! # other stuff

    (?)! end!
  25. Database SELECT * FROM fictional_models ORDER BY fictional_models.id DESC LIMIT

    1
  26. Model # => #<FictionalModel id: 221, name: "Derek Zoolander">

  27. Controller class SessionsController < ApplicationController! def new! @model = FictionalModel.last!

    render :new! end! end!
  28. Controller class SessionsController < ApplicationController! def new! @model = FictionalModel.last!

    render :new! end! end!
  29. View <div id="sign_in">! <h3>! Our last registeded model is: <%=

    @model.name %>! </h3>! ! <a href="/auth/google">Sign in with Google</a>! <div>!
  30. Browser <div id="sign_in">! <h3>! Our last registeded model is: Derek

    Zoolander! </h3>! ! <a href="/auth/google">Sign in with Google</a>! <div>!
  31. None
  32. What about Logic?

  33. Logic in Views? (›°□°)›ớ sʍǝıᴧ

  34. Logic in Controllers? (›°□°)›ớ sɹǝʃʃoɹʇuoↃ

  35. Logic in Controllers? Controllers ϊ( ʄ-ʄϊ)

  36. Logic in Models? ¯\_(π)_/¯

  37. ಠ_ಠ

  38. ?

  39. ಠ_ಠ

  40. 197x - 1988

  41. “Everything  is an Object”

  42. Smalltalk

  43. Xerox PARC

  44. Message Passing

  45. Integrated Development Environment

  46. Inspired:

  47. Graphical Interfaces (pretty much)

  48. Graphical Interfaces are why MVC exists.

  49. MVC in Smalltalk

  50. Interactions

  51. None
  52. Controller Sends commands to its associated view to change the

    view's presentation ! Sends commands to the model to update the model's state
  53. View Requests from the model the information that it needs

    to generate an output representation to the user.
  54. Model Notifies its associated views and controllers when there has

    been a change in its state.
  55. Model A passive implementation of MVC omits these notifications, because

    the application does not require them or the software platform does not support them
  56. None
  57. “Thin Client”

  58. We can do this.

  59. Websockets

  60. None
  61. “Active” MVC. class Timeline < ActiveRecord::Base! ! after_save :notify_views! !

    def websockets_channel! "#{ self.class }#{ self.id }".hash! end! ! private! ! def notify_views! Pusher.trigger(self.websockets_channel, 'update', { self: self.to_json })! end! end!
  62. class Timeline < ActiveRecord::Base! ! after_save :notify_views! ! def websockets_channel!

    "#{ self.class } #{ self.id }".hash! “Active” MVC.
  63. “Active” MVC. class Timeline < ActiveRecord::Base! ! after_save :notify_views! !

    def websockets_channel! "#{ self.class }#{ self.id }".hash! end! ! private! ! def notify_views! Pusher.trigger(self.websockets_channel,
  64. class Timeline < ActiveRecord::B ! after_save :notify_views! ! def websockets_channel!

    "#{ self.class }#{ self.id } end! ! “Active” MVC.
  65. “Active” MVC. end! ! private! ! def notify_views! Pusher.trigger(! self.websockets_channel,!

    'update',! { self: self.to_json }! )! end! end!
  66. None
  67. GET /timelines/1 http http

  68. PUT /timelines/1 http

  69. Web Server

  70. Router MyApp::Application.routes.draw do! get '/' => 'sessions#new'! root :to =>

    'sessions#new' ! end!
  71. Controller class TimelinesController < ApplicationController! def update! @timeline = Timeline.find(params[:id])!

    if @timeline.update_attributes(params[:timeline])! #...! #...! end! end!
  72. Model after_save :notify_views

  73. Model def notify_views! Pusher.trigger(! self.websockets_channel,! 'update',! { self: self.to_json }!

    )! end!
  74. None
  75. None
  76. None
  77. Websockets <3

  78. (Never said it was a good idea)

  79. 2009 - 2013

  80. JavaScript

  81. MVC in the browser.

  82. Full Circle.

  83. View Requests from the model the information that it needs

    to generate an output representation to the user.
  84. Empower your Views

  85. Backbone.js

  86. Angular.js

  87. None
  88. So what is MVC’s purpose?

  89. Separation of Concerns ! (for graphical interface apps)

  90. Guideline.

  91. Tailor MVC to your needs.

  92. You > MVC

  93. Gracias! @poteland

  94. None
  95. Questions? @poteland References: