Saturday, March 5, 2011

MVC vs 3-Tier Pattern

I have had several people ask me what the difference is between MVC (Model View Controller) and Three-Tier architectural patterns. It is my intent to clarify the confusion by comparing the two patterns side-by-side. At least in part, I believe the source of some of the confusion is that they both have three distinct layers or nodes in their respective diagrams.

Three-Tier MVC
If you look carefully at each diagram you'll notice the associations (arrow connectors) between the boxes are set up a little differently.

Three-Tier

A 3-tiered system really is made up of layers (think of cake layers). The UI Layer has access to the Business Logic Layer, and the Business Layer has access to the Data Layer. But the UI Layer cannot directly access the Data Layer. In order for the UI Layer to access data, it must go through the Business Logic Layer via some kind of interface. If it helps, you could think of each layer as one big loosely coupled component with strict design rules of access between layers.

MVC (Model View Controller)

In contrast, the MVC pattern obviously does not keep a layered system. The Contoller accesses the Model (a runtime data repository) and the View. The View then accesses the Model. Exactly how does that work? The Controller ultimately is the logical decision point. What sort of logic? Typically, the Controller will retrieve, build, or modify a Model base on some triggered action. The Controller then decides which View is appropriate via some internal logic. At that point the Controller will push the Model to View.

Note: Because I mostly develop with .NET, Microsoft has adopted the MVC pattern for ASP.NET with their own platform (see http://www.asp.net/mvc). You can certainly use the MVC pattern without Microsoft's platform, but why reinvent the wheel? I have been very happy using it so far.

When Do I Choose Which Pattern?

First of all, these two patterns are definitely not mutually exclusive. In fact in my experience they are quite harmonious. Often I use a multi-tiered architecture, such as a three-tiered architecture, for the overall architectural structure. Then, within the UI Layer, I use MVC. Something like the diagram below.

3 comments:

  1. Hi Suma,

    I'm not sure if there is a good code example that will clearly show this. Your MVC project is your UI layer. Your business logic or mid-tier layer technically could be in your MVC project, but I prefer to have any other combination of linked projects or services externally or in your solution. This keeps some defined boundary (though not perfect) for other developers to have the UI and mid-tier layer separated. Your data layer is typically something like a database or file system. You keep your three-tier system in line by never allowing your UI and data layers to communicate directly with each other. The UI layer must make calls to the business logic layer to get data from the data layer.

    I hope that helps. Let me know if you need any further clarification.

    ReplyDelete
  2. Hi BRYAN recently i attended one interview in that they asked me what is MVC architecture and give one example to illustrate.I just explained what is MVC. but i cant able to explain the example.Can u pls put ur effort to give simple example based on MVC architecture.Thanks in advance BRYAN.

    ReplyDelete
  3. Hii This is sandeep Rath(software developer in mvc) any body wants asp.net mvc realtime projects then mail me.my mailid is sr034937@gmail.com

    ReplyDelete