Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
The colors of MVC
What is MVC?● An amazing pattern that can be found  throughout just about any software● Controller -> input● Model -> thro...
Atomic elements of an application                            Controller                    Model             View
MVC Colorwheel                        Model       Cache                                                   Input           ...
Model● Raw data comes in, raw data goes out● App specific code● Not just storage, all business logic goes in  the model● C...
Controller● Data transport● Non app-specific, common across any app● Http / request / response / headers / ip  address / c...
Views● Takes in raw data, returns visualized data● HTML, XML, JSON, JPEG● Represents what humans want from the  cmputer
Controller -> model -> view ->controllerdef new_blog(request):  # {author: bob, title: Title, body: Body}  data = request....
curl -d "title=my blog&author=bob&body=my blog body" http://myblog.com/new_blog                                           ...
Input Middleware●   Mix between Controller and Model●   Processes the data stream from controller●   Examples: Authenticat...
Session Middlewaredef session_middleware(request):  sid = request.COOKIES.get(session_id)  if sid:      session = Session....
Output Middleware● Modifies the data stream from the controller  after the view● Examples: HTTP minify middleware● Complet...
HTML Minifierdef minify_middleware(response):  if html in response.mimetype:      minified = response.content.replace( , )...
Cache● Short circuit of Model and View● Performance improvement when you can get  away with returning pre-rendered data+ht...
Cachedef view_blog(request, id):  key = "html_blog_%s" % id  html = memcache.get(key)  if html:      return html  else:   ...
curl http://myblog.com/view/45                                                                     HTML minify            ...
MVC is everywhere!                                                   Blog.objects.get        def get(self, id):          t...
I mean, everywhere!                                                       render()         def render(template, context, u...
Try not to mix colors● Keep model code isolated from controller  and view code● Coupling is bad● Reusability, discoverabil...
Giotto● Application framework that does away with  controllers● Not at all like django/pyramid/flask● Completely decoupled...
Giotto exampleclass Multiply(GiottoProgram)   name = multiply   controllers = (http-get, cmd, irc)   model = [multiply]   ...
Decoupled Design                                                                                   Model                  ...
./giotto http-dev
Decoupled Design                                                                                   Model                  ...
Command Line
Giotto exampleclass Multiply(GiottoProgram)   name = multiply   controllers = (http-get, cmd, irc)   model = [multiply]   ...
Automatic Cache                                                                   Model                                   ...
Model Mockingclass Multiply(GiottoProgram)   name = multiply   controllers = (http-get, cmd, irc)   model = [multiply, {x:...
Decoupled Design                                                                                     Model                ...
Model Mocking● Designers dont need to run  rabbitmq/postgres/etc.● Data interchange between model/view is  explicit and si...
Middlewareclass Multiply(GiottoProgram)   name = multiply   controllers = (http-get, cmd, irc)   input_middleware = [AuthM...
Questions?
Upcoming SlideShare
Loading in …5
×

Visualizing MVC, and an introduction to Giotto

  • Login to see the comments

  • Be the first to like this

Visualizing MVC, and an introduction to Giotto

  1. 1. The colors of MVC
  2. 2. What is MVC?● An amazing pattern that can be found throughout just about any software● Controller -> input● Model -> throughput● View -> output● MVP/MVA/etc. are all MVC, just with different names● Not a design pattern
  3. 3. Atomic elements of an application Controller Model View
  4. 4. MVC Colorwheel Model Cache Input Middleware View Controller Output Middleware
  5. 5. Model● Raw data comes in, raw data goes out● App specific code● Not just storage, all business logic goes in the model● Can be reused across any controller● Represents what computers want to give to users
  6. 6. Controller● Data transport● Non app-specific, common across any app● Http / request / response / headers / ip address / cookies / etc.● Represents link between human and computer
  7. 7. Views● Takes in raw data, returns visualized data● HTML, XML, JSON, JPEG● Represents what humans want from the cmputer
  8. 8. Controller -> model -> view ->controllerdef new_blog(request): # {author: bob, title: Title, body: Body} data = request.POST blog = Blog.objects.create(**data) html = render({blog: blog}, blog.html) return HttpResponse(html)
  9. 9. curl -d "title=my blog&author=bob&body=my blog body" http://myblog.com/new_blog HTML minify HttpRedirect("http://myblog.com/view/%d" % blog.id)) Blog.objects.create() "http://myblog.com/view/%d" % blog.idpython manage.py new_blog --title=My blog --author="bob" --body="my blog body"
  10. 10. Input Middleware● Mix between Controller and Model● Processes the data stream from controller● Examples: Authentication middleware● View hs no idea about Input Middleware
  11. 11. Session Middlewaredef session_middleware(request): sid = request.COOKIES.get(session_id) if sid: session = Session.objects.get(id=sid) else: session = Session.objects.create(**) request.session = session return request
  12. 12. Output Middleware● Modifies the data stream from the controller after the view● Examples: HTTP minify middleware● Completely hidden from the model
  13. 13. HTML Minifierdef minify_middleware(response): if html in response.mimetype: minified = response.content.replace( , ) response.content = minified return response else: return response
  14. 14. Cache● Short circuit of Model and View● Performance improvement when you can get away with returning pre-rendered data+html● Primary usage of key/value stores● Performance comes with a price, cache invalidation is tough
  15. 15. Cachedef view_blog(request, id): key = "html_blog_%s" % id html = memcache.get(key) if html: return html else: blog = Blog.objects.get(id=id) html = render(blog.html, blog) return memcache.set(html, key)
  16. 16. curl http://myblog.com/view/45 HTML minify render("blog.html", blog) Blog.objects.get() "%sn%sn%s" % (blog.title, blog.author, blog.body)python manage.py view_blog 5
  17. 17. MVC is everywhere! Blog.objects.get def get(self, id): table_name = blog engine = settings.DB_ENGINE q = "select * from %s where id=%s" % ( table_name, id ) data = engine.execute(q) return Blog(**data)
  18. 18. I mean, everywhere! render() def render(template, context, unicode=True) t = open(template).read() html = do_render(t, context) if unicode: return unicode(html) else: return html
  19. 19. Try not to mix colors● Keep model code isolated from controller and view code● Coupling is bad● Reusability, discoverability, and predictability● If you have to mix colors, its OK! Just remember to fix it later!
  20. 20. Giotto● Application framework that does away with controllers● Not at all like django/pyramid/flask● Completely decoupled models and views● https://github.com/priestc/giotto
  21. 21. Giotto exampleclass Multiply(GiottoProgram) name = multiply controllers = (http-get, cmd, irc) model = [multiply] view = RedBlueMultiplyViewer
  22. 22. Decoupled Design Model Postgres, rabbitmq, validation, algorithms facebook api, etc. 5}) y: : 3, , {x ly ultip {{x: 3, y: 5, result: 15} (m HTTPheaders, cookies,request, ips,response... {body: < html..., m imetype: View ... } HTML, JSON, mimetypes, image formats, templates
  23. 23. ./giotto http-dev
  24. 24. Decoupled Design Model Postgres, rabbitmq, validation, algorithms facebook api, etc. 5}) y: : 3, , {x ly ultip {{x: 3, y: 5, result: 15} (m IRCchannels, privatemessage, server,ops, voice, etc. {body: t ext, mim etype: . View .. } HTML, JSON, mimetypes, image formats, templates
  25. 25. Command Line
  26. 26. Giotto exampleclass Multiply(GiottoProgram) name = multiply controllers = (http-get, cmd, irc) model = [multiply] cache = 3600 view = RedBlueMultiplyViewer
  27. 27. Automatic Cache Model Postgres, rabbitmq, validation, algorithms facebook api, etc. {{x: 3, y: 5, result: 15} (multiply, {x: 3, y: 5})Controller Cache {body: text, mimetype: ... } View HTML, JSON, mimetypes, image formats, templates
  28. 28. Model Mockingclass Multiply(GiottoProgram) name = multiply controllers = (http-get, cmd, irc) model = [multiply, {x: 10, y: 11, result:110}] cache = 3600 view = RedBlueMultiplyViewer
  29. 29. Decoupled Design Model Postgres, rabbitmq, validation, algorithms facebook api, etc. Mock 5}) {{x: 10, y: 11, result: 110} y: 3, x: ly, { ltip ( mu IRCchannels, privatemessage, server,ops, voice, etc. {body: t ext, mim etype: . View .. } HTML, JSON, mimetypes, image formats, templates
  30. 30. Model Mocking● Designers dont need to run rabbitmq/postgres/etc.● Data interchange between model/view is explicit and simply stated
  31. 31. Middlewareclass Multiply(GiottoProgram) name = multiply controllers = (http-get, cmd, irc) input_middleware = [AuthMiddleware] model = [multiply] cache = 3600 view = RedBlueMultiplyViewer output_middleware = [MinifyMiddleware]
  32. 32. Questions?

×