Download - Model View Controllers (MVC)
![Page 1: Model View Controllers (MVC)](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815ff8550346895dcef829/html5/thumbnails/1.jpg)
Model View Controllers (MVC)
Barack Karavani
![Page 2: Model View Controllers (MVC)](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815ff8550346895dcef829/html5/thumbnails/2.jpg)
Outline
• What is MVC?• Why use the MVC Pattern?• MVC and the Web• Benefits of MVC in Web Applications• Modern MVC Frameworks
![Page 3: Model View Controllers (MVC)](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815ff8550346895dcef829/html5/thumbnails/3.jpg)
What is MVC
• MVC is a patterno Patterns are commonality that we find amongst
software Architectural Pattern - Provide solutions to
various issues in Software Engineering Design Patterns - Provide solutions to repeated
issues in Software Development
• MVC solves common issues by separating important parts into three subsystems.
![Page 4: Model View Controllers (MVC)](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815ff8550346895dcef829/html5/thumbnails/4.jpg)
Why MVC
• Decouple the interaction between user interfaces and data stores.
• Provide a clear separation of concern between components.
• Promote the development of reusable components.
![Page 5: Model View Controllers (MVC)](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815ff8550346895dcef829/html5/thumbnails/5.jpg)
MVC and the Web
View
Controller
Model
![Page 6: Model View Controllers (MVC)](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815ff8550346895dcef829/html5/thumbnails/6.jpg)
Benefits of MVC in Web Applications
• Websites and Web Applications are great candidates for the MVC Pattern.o Inherently clear distinction for models,
views, and controllers.• Multiple frameworks exist in various
languages which support and encourage the MVC Pattern and other great coding principles!
![Page 7: Model View Controllers (MVC)](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815ff8550346895dcef829/html5/thumbnails/7.jpg)
Popular MVC Web Frameworks
• Open Source frameworks are actively being developed
• Promote the MVC Patterno Definitions of MVC may change depending on the
framework philosophy, READ THE DOCS!
![Page 8: Model View Controllers (MVC)](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815ff8550346895dcef829/html5/thumbnails/8.jpg)
Popular MVC Web Frameworks
• MVC Frameworks exist both on the client-side and server-side
Client Side (JS):● Backbone● AngularJS● EmberJS
Server Side:● Django (Python)● Laravel (PHP)● Rails (Ruby)
![Page 9: Model View Controllers (MVC)](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815ff8550346895dcef829/html5/thumbnails/9.jpg)
Benefits of MVC in Web Applications
• By promoting the use of reusable components, we prevent ourselves from writing “spaghetti” code. o Spaghetti Code - One-off code which interleaves business logic
(model/controller) with view responsibilities Hard to maintain Hard to read Does not scale well
• Provide commonly used tools to web developers
![Page 10: Model View Controllers (MVC)](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815ff8550346895dcef829/html5/thumbnails/10.jpg)
Example of Spaghetti Code <? if(($child_assent || $adol_assent) && $form['short_name'] == 'parental_consent' && $canEdit): ?><div class="form-helpers"><? else: ?><div class="form-helpers hide"><? endif; ?>
![Page 11: Model View Controllers (MVC)](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815ff8550346895dcef829/html5/thumbnails/11.jpg)
Common Tools
• Active Record and Object Relational Mapping (ORM)
• Routes• Templating• Form and Input Validation• Security and Authentication• Testing & Test Cases
![Page 12: Model View Controllers (MVC)](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815ff8550346895dcef829/html5/thumbnails/12.jpg)
Views in Laravel, Django, Rails
• Typically views for web applications consist of the HTML/CSS/JS side of thingso Do not interact directly with the model
• All three frameworks implement their version of a templating system.o Contain either simple logic or none at allo Prevents spaghetti code (by not allowing ANY code)o Promotes developing small templates to be reused
and combined in multiple places
![Page 13: Model View Controllers (MVC)](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815ff8550346895dcef829/html5/thumbnails/13.jpg)
Templating{% if latest_poll_list %} <ul> {% for poll in latest_poll_list %} <li>{{ poll.question }}</li> {% endfor %} </ul>{% else %} <p>No polls are available.</p>{% endif %}
<% @books.each do |book| %> <tr> <td><%= book.title %></td> <td><%= book.content %></td> </tr><% end %>
Django Rails
![Page 14: Model View Controllers (MVC)](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815ff8550346895dcef829/html5/thumbnails/14.jpg)
Models in Laravel, Django, Rails
• Models are defined as objects.o Instances of models represent rows within the
database table.
• Frameworks will automatically build database tables based upon models.
• Controller can interact with the models as native language objects.
![Page 15: Model View Controllers (MVC)](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815ff8550346895dcef829/html5/thumbnails/15.jpg)
Controllers in Laravel, Django, Rails
• Controllers are functions that are called when a user accesses certain pages.o One controller can be called for multiple URLs.
• Processes input data from the user.• Returns data to be styled in the view for the
user to interpret.
![Page 16: Model View Controllers (MVC)](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815ff8550346895dcef829/html5/thumbnails/16.jpg)
Questions?