<<

MVC Design Pattern Introduction to MVC and compare it with others Gholamhossein Tavasoli @ ZNU Separation of Concerns o All these methods do only one thing “Separation of Concerns” or “Layered Architecture” but in their own way. o All these concepts are pretty old, like idea of MVC was tossed in 1970s. o All these patterns forces a separation of concerns, it means domain model and controller logic are decoupled from user interface (view). As a result maintenance and testing of the application become simpler and easier. MVC Pattern Architecture o MVC stands for Model-View-Controller Explanation of Modules: Model o The Model represents a set of classes that describe the business logic i.e. business model as well as data access operations i.e. data model. o It also defines business rules for data means how the data can be created, sotored, changed and manipulated. Explanation of Modules: View o The View represents the UI components like CSS, jQuery, HTML etc. o It is only responsible for displaying the data that is received from the controller as the result. o This also transforms the model(s) into UI. o Views can be nested Explanation of Modules: Controller o The Controller is responsible to process incoming requests. o It receives input from users via the View, then process the user's data with the help of Model and passing the results back to the View. o Typically, it acts as the coordinator between the View and the Model. o There is One-to-Many relationship between Controller and View, means one controller can handle many views. Sequence Diagram Decoupling views and models o MVC decouples views and models by establishing a subscribe/notify protocol between them. o Whenever the model's data changes, the model notifies views that depend on it. o This approach lets you attach multiple views to a model to provide different presentations. Decoupling views and controllers o Single presentation different response strategies o Single View different controller o The View-Controller relationship -> Strategy pattern Advantages of MVC o Since MVC handles the multiple views using the same enterprise model it is easier to maintain, test and upgrade the multiple system. o It will be easier to add new clients just by adding their views and controllers. o Since the Model is completely decoupled from view it allows lot of flexibilities to design and implement the model considering reusability and modularity. This model also can be extended for further distributed application. oIt is possible to have development process in parallel for model, view and controller. Disadvantages of MVC o This design approach is not suitable for smaller applications o In case of complicated user interface with states of views related to each other o Possible workflow models, this information though not business functionality related o Has to be in the Model layer. In very large systems this may be beneficial, otherwise it is usually transferred to the control layer, somewhat breaching the pattern. Introduction to o Django is a free and open source web application framework, written in Python, which follows the Model–View–Controller architectural pattern. o It is maintained by the Django Software Foundation (DSF), an oindependent organization. Perhaps You’ve Heard Of... o Pinterest o Instagram o Disqus o OpenStack o Mozilla o Bitbucket Features oObject Relational Mapper – ORM oMVC (MVT) Architecture oTemplate System oOut of the box customizable Admin Interface, makes CRUD easy oBuilt-in light weight Web Server oElegant URL design oAuthentication / Authorization oInternationalization support oFast Development oFree, and Great Documentation oCache framework, with multiple cache mechanisms Back to Django: How does Django work? Is Django MVC? o Yes and No! o Model-Template-View (MTV) o The interfacing classes (Model) o Request-processing classes (View) o A templating language for the final presentation (Template) o MVC o Model: Django's Models o View: Django's Templates o Controller: the framework itself that processes an incoming HTTP request and routes it to the correct view function ORM? o Relational Database o Objects o Mapping between Objects & Relational Database Why ORM? o Leverages existing knowledge and skills related to OOP o Level of abstraction o Saves programmer time CRUD Some ORM in Python o SQLObject o o Django's ORM o peewee o SQLAlchemy o PonyORM Some ORM in Python o SQLObject o Storm o Django's ORM o peewee o SQLAlchemy o PonyORM o compare Some ORM in Python o SQLAlchemy o not limited to ORM. It is a DB manipulation framework o connection management & expression framework + ORM o can handle raw SQL o sanitize value (security) o Documentation is somewhat difficult to understand Some ORM in Python o Pony ORM o A very convenient syntax for writing queries o Automatic query optimization o Simplified setup and usage o Not designed to process hundreds of thousands or millions of records Some ORM in Python o Pony ORM o A very convenient syntax for writing queries o Automatic query optimization o Simplified setup and usage o Not designed to process hundreds of thousands or millions of records simultaneously o Tool: Database Designer sample Some ORM in Python o Pony ORM o A very convenient syntax for writing queries o Automatic query optimization o Simplified setup and usage o Not designed to process hundreds of thousands or millions of records simultaneously o Tool: Database Designer sample MVP Pattern o Model-View-Presenter (MVP) is a variation of the Model-View-Controller (MVC) pattern but specifically geared towards a page event model. o The model stores the data o The view shows a representation of the model o The presenter coordinates communications between the layers MVP Pattern o Model, like model in MVC o Presenter o handle user input and use this to manipulate the model data o There is One-to-One relationship between View and Presenter o View o Receive input and updated based on changes in model. It does not contain any logic implemented. Sequence Diagram for MVP MVVM Pattern o The Model-View-ViewModel (MVVM or ViewModel) is a pattern for separating concerns in technologies that use data- binding oTwo-way data binding between View and View-Model o This allows automatic propagation of changes, inside the state of View-Model to the View o Generally, the View-Model utilizes the observer pattern to inform changes in the View-Model to the Model MVVM Pattern o The View holds a reference to the View Model o The View Model has no information about the View o ViewModel o It has a collection which contain the data from the model currently needed for the view oThe bindings between view and ViewModel are simple to construct because a ViewModel object is set as the Data Context of a view. o If property values in the ViewModel change, those new values automatically propagate to the view via data binding. MVVM Architecture

References

Data bindings

Notify property changed Sequence Diagram for MVVM Summary Summary