MVC Vs n-tier architecture
Asked Answered
G

12

150

I was wondering what exactly is the difference between MVC(which is an architectural pattern) and an n-tier architecture for an application. I searched for it but couldn't find a simple explanation. May be I am a bit naive on MVC concepts, so if anyone can explain the difference then it would be great.

Gorgonian answered 30/3, 2009 at 17:36 Comment(0)
M
107

N-tier architecture usually has each layer separated by the network. I.E. the presentation layer is on some web servers, then that talks to backend app servers over the network for business logic, then that talks to a database server, again over the network, and maybe the app server also calls out to some remote services (say Authorize.net for payment processing).

MVC is a programming design pattern where different portions of code are responsible for representing the Model, View, and controller in some application. These two things are related because, for instance the Model layer may have an internal implementation that calls a database for storing and retrieving data. The controller may reside on the webserver, and remotely call appservers to retrieve data. MVC abstracts away the details of how the architecture of an app is implemented.

N-tier just refers to the physical structure of an implementation. These two are sometimes confused because an MVC design is often implemented using an N-tier architecture.

Murillo answered 30/3, 2009 at 17:42 Comment(7)
N-tier is also a design pattern, you don't need 3 server to do a 3-tier system, in fact, it is possible to do a n-tier system using a single file, separating each tier by a conceptual concept.Adlai
Tier basically implies that an interprocess communication is occurring Usually across a network link. I disagree that an in-process (let alone in the same file) code design flow constitutes a tiered design approach. Of course that is IMHO. "Server" implies that the machine can run several processes on the same box; and they can probably even still talk on the "localhost" network.Murillo
All of the formats discussed are examples of 3 layer designs. Do not confuse the difference between a layer and a tier. It is true that you can run more than one tier on a physical mahcine (e.g. you divide up a big server via hypervisors), but the point here is N-Tier aludes to a physical network hop (e.g. TCP/IP). Locally you would be more efficent to use named pipes, but again, if you run on the same system you are competitng for memory and processing power. All of these are the reasons to consider isolating presentation, Business Logic & Data Access, and database on different machines.Pylorus
I would recommend anyone reading this question to read other answers, this answer is inaccurateIntention
@Adlai , Go with 'Zak', first we need to clear the difference between Tier and Layer Here is very good codeproject article in which there is clear explanationTwitter
N-tier does not represent a physical model only. The term refers to a software architecture model as well. Below extraction explains 3-tier (instance of N-tier) vs MVC. The three layer of an 3-tier architecture refers to the separation of a software on presentation layer (where MVC can be good use anyway), business rules layer and business entities layer, with business rules intermediating all communication between presentation and entity.Despondency
Do we use MVC for the frontend only and 3-tier for the backend?Majewski
C
46

If a 3-tier design were like this:

Client <-> Middle <-> Data

the MVC patter would be:

     Middle
     ^    |
     |    v
Client <- Data

Meaning that:

  • in the 3-tier equivalent, communication between layers is bi-directional and always passes through the Middle tier
  • in the MVC equivalent the communication is in unidirectional; we could say that each "layer" is updated by the one at the left and, in turn, updates the one at the right –where "left" and "right" are merely illustrative

P.S. Client would be the View and Middle the Controller

Claudelle answered 14/7, 2010 at 15:50 Comment(4)
in MVC can the model interacts directly with client(view) ??? I don't think so !Boatbill
@Alaa, I agree and that's why I think it's referring to the flow of data. Update: I just checked in Wikipedia, and the Model can interact the View through observers (not directly).Claudelle
In MVC : MVC architecture is triangular: the view sends updates to the controller, the controller updates the model, and the view gets updated directly from the model In Three Tier : A three tier architecture is the client tier never communicates directly with the data tier In a Three-tier model all communication must pass through the middle tierBuenabuenaventura
Here if Middle is a controller then the communication between Middle,Client and Middle,Data are bidirectional not unidirectional as described in ans..The controller passes the data to model and the model returns back the updated data to controller which then returns it to browser after passing through the view.Counterblow
K
33

This is what say about n-tier architecture

At first glance, the three tiers may seem similar to the MVC (Model View Controller) concept; however, topologically they are different. A fundamental rule in a three-tier architecture is the client tier never communicates directly with the data tier; in a three-tier model all communication must pass through the middleware tier. Conceptually the three-tier architecture is linear. However, the MVC architecture is triangular: the View sends updates to the Controller, the Controller updates the Model, and the View gets updated directly from the Model.

Knotting answered 4/1, 2010 at 15:19 Comment(5)
Sounds good, but I don't believe "the View gets updated directly from the Model" is a good idea. It doesn't make sense to use the Controller for updates and inserts but not for selects and filters, and I don't see the point of separation of concerns only to bind the view to the model anyway! Conclusion - MVC is another one of those obfuscations created by .... have a guess. I don't recall 3-tier ever being limited to "system architecture" or "application design". The central concept is separation of concerns.Subalpine
It would depend on what you're doing. An MVC app for an iOS application (which likely would not allow the the view to be updated directly from the Model) will be different than a Web app (which might). MVC is a paradigm, not an absolute way of doing things.Unmake
@Subalpine totally agree. Too many jargon for an intuitive concept.Onesided
sounds good, doesn't work. Wikipedia is not the ultimate source of truthRockling
Its the way you interpret the truth, and again it depends on what your goals areKnotting
M
21

The only similarity is that the two patterns have three boxes in their diagrams. Fundamentally they are completely different in their uses. If fact, it is not usually a choice between which pattern to use, but both patterns can be use together harmoniously. Here is a good comparison of the two: http://allthingscs.blogspot.com/2011/03/mvc-vs-3-tier-pattern.html

This diagram shows how both patterns can be used together, with MVC used solely within the Presentation/UI Tier:

mvc3tiercombo

Miksen answered 23/3, 2011 at 17:17 Comment(1)
I think this is by far the best answer, especially as MVC is really focused on the UI, and it can be your UI tier in a 3 tier design. The diagram in the link demonstrates this very well.Stubblefield
O
7

Give yourself a break. And don't restrict yourself to certain patterns when solving real-world problems. Just remember some general principles, one of which is SEPARATION OF CONCERNS.

Onesided answered 18/12, 2014 at 9:33 Comment(0)
V
6

A fundamental rule in three-tier architecture is the client tier never communicates directly with the data tier; in a three-tier model all communication must pass through the middleware tier.

It’s liner architecture. This addresses the question of how to pass information between a user and a database. Where as MVC is a triangular architecture: the View sends updates to the Controller, the Controller updates the Model, and the View gets updated directly from the Model. This addresses questions of how a user interface manages the components on the screen.

Vice answered 16/2, 2010 at 10:10 Comment(0)
T
6

@Cherry Middle ware works more like a request handler or redirector in MVC Pattern.

I would like to explain a bit about MVC, According to me Model View Controller works like this.

  1. Client initiates the session by requesting for any service.
  2. This request is received and handled by Controller (Request handler, redirector etc)
  3. Controller process a basic info on the request and redirect it to the relevant Model which can fill up the data request.
  4. Model fill up the request according to the parameters passed by Controller and send back the results to Controller. (Note: Here i like to clear that data is not directly returned to client in true MVC architecture, rather it fills up and returned to controller.)
  5. Controller than send that data to View(Client).
  6. Client has the requested service in front of him.

That's all about MVC that i know.

Thynne answered 16/11, 2010 at 6:33 Comment(1)
I think that, like said above, MVC is triangular, so the View can sometimes talk directly to the Model and vice versa, as explained in this document : msdn.microsoft.com/en-us/library/ms978748.aspxTomfool
E
5

An N-Tier architecture is best defined using a Deployment Diagram.

An MVC architecture is best defined using a Sequence Diagram.

The 2 are not the same and are not related and you can combine the two architectures together. A lot of companies have taken the steps to create N Tier'd architecture for not only deployment and scalability, but for code reuse as well.

For example, your Business Entity objects may need to be consumed by a desktop app, a web service exposed for a client, a web app, or a mobile app. Simply using an MVC approach will not help you reuse anything at all.

Eolic answered 28/12, 2010 at 16:20 Comment(1)
If mvc is not reusing anything for your given scenario then are there any benefits of mvc as compared to n tier arch.Counterblow
Z
5

Besides being linear, another major difference that was not emphasized enough here is that in the N-tier model, N is not necessarily 3-tiers! It is most often implemented as three tiers (presentation, app, data) with the middle layer having two sub-tiers (business logic and data access). Also, the model in MVC can contain both data and business logic for data manipulation, whereas these would be in separate tiers in n-tier.

Zinazinah answered 18/7, 2011 at 18:47 Comment(0)
T
3

Conclusion : N-tier is an architecture, MVC a design pattern. They are the same metaphore applied in two different fields.

Tomfool answered 29/12, 2010 at 8:51 Comment(0)
R
2

Jerry: Here's a simple example of how the two are related:


Tier 1 - Consists of Models that communicate with Tier 2 through some sort of network service or similar, controllers to handle input validation, calculations and other things relevant for the views. And it contains the views themselves, ofcourse - which can be the GUI in a desktop-app, or the web-interface in a web-app.


Tier 2 - Contains some sort of service or other way of recieving messages from Tier 1. Does not/should not know about Tier 1, so can only answer to calls from above - never ask for things by itself. Also contains all business-logic.


Tier 3 - Contains the domain model, object representation of the database and all logic to communicate and update database-entries.

Rickert answered 14/9, 2009 at 14:29 Comment(0)
G
2

N-tier architecture never communicates directly to the data access layer. In a 3-tier architecture:

  • Presentation layer presents the related UI,
  • Business layer contains related logic and
  • then the data access layer.

All the data communicates through the middle tier. Presentation <-> Business <-> Data.

MVC (Model-View-Controller) architecture is triangular.

  • The View sends updates to the controller,
  • The Controller updates the Model and
  • then the View directly gets updates from the model.

Model(Data), View(UI), Controller(Logic).

Gleet answered 28/10, 2021 at 12:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.