Thursday, December 18, 2014

Model View Controller Architecture

One of the main architectures of web applications is MVC. This architecture is a reliable and most common architecture that is used for web application development. Most of the systems either uses these layers as the appear or uses a frameworks that consists of MVC directly.

The need for this pattern is due some general reasons:
  • Data should be manipulated
  • There is a need for user interface
  • Data is not necessarily dependent to the user view
  • Development can be divided into parts just by experience level
  • Technology independent solutions can be produced
  • Layers can communicate to each other with standards and technologies can be changed
  • It is possible to separate business logic from other rules

So what are these layers stands for:
  • Model : This layer is responsible for data. Data in web platforms can be used as a state. This makes the Model layer very crucial since data manipulation is always risky and transactions should be secured. It is a fact that every relational database have their ACID properties but this layer doesn't always based on relational databases. If a nosql database is selected due to the project specifications it is necessary to secure most of the ACID property development in this layer. Also the data should be serializable to make it transferable through different layers. This layer has only interaction with the Controller layer.
  • View : This layer is responsible for the presentation of the application. It seems simple but it is well known that web gui development can be a pain. Independent of which presentation library you are using, it is necessary to use javascript and jQuery to manipulate the view. These manipulations can be considered as user rules and behaviours. The technology used in this layer is the most variable one among the others since there exists many alternatives. This layer has only interaction with the Controller layer.
  • Controller : This layer is responsible for the transformation of data, user requests and responses. In this layer, the business logic is implemented. Any user action, that is executed, should properly interpreted and send to the Model layer. The response that comes from the Model layer should also be formed properly and send back to the View layer. This layer is also responsible for the communication to the external systems if any. This layer has interaction with external systems, Controller and View layers.
What happens inside a MVC application:
  1. User executes an action
  2. View layer recognizes event
  3. View layer calls appropriate method on controller layer
  4. Controller layer accesses model layer, possibly updating it
  5. If state(Model) has been changed, View is updated (via the controller layer)
It is possible to extend the MVC approach with observer pattern. Obviously, with the current innovations in the internet, the state(Model) can be changed by other events(like Facebook). So there is a need to update the View independent of users actions. Observer pattern is one of the most usefull patterns when it comes to interactive views. Every time a user joins, s/he registers to the application and whenever an action which affects her/is view is executed the view should be updated.
PS: Kudos to Facebook if you consider their number of users and how they managed to do this in an efficient way.

With this update, it is necessary to have another interaction. The View layer should be linked to Model layer and it should be bidirectional since Model will inform View for the state change and View should be able to get related data without requiring any business logic.
What happens inside this extended MVC application:
  1. User executes an action
  2. View layer recognizes event
  3. „View layer calls appropriate method on controller laye
  4. „Controller layer accesses model layer, possibly updating it
  5. „If state(Model) has been changed, it notifies all registered views
  6. View layer then interact with Model layer for the change, get and interpret the new information appropriately

It is also possible to update this approach with a delegate pattern which leads us to one of the most wide approaches of today. In this approach the Controller and View layers are combined to Delegate layer which is responsible for  the business logic and user interface. They are still separate but located in the same layer. This is commonly referred as UI layer. A well known framework is Angular.js.
What happens inside this extended MVC application:
  1. User executes an action
  2. Delegate layer recognizes event and executes the event listener
  3. Delegate layer accesses the Model layer, possibly updating it
  4. If model has been changed, UI is updated

Of course, these are not the only way to extend this architecture. MVC is a well known approach for building web applications and it is widely used among enterprise development.

Most common techniques to better the performance are multi-threading and asynchronous approaches as always. The problem is when to use which approach.

No comments:

Post a Comment