Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does Web API fit in a typical n-tier architecture?

Usually when i layout an n-tier architecture for a project I have the following layers:

  • Domain (domain model, repository contracts)
  • Data (repositories working on top of domain model)
  • Service (aggregates repos, caching, validation)
  • Presentation (the mvc app)

Where would ASP.NET MVC 4 Web API fit into this considering that it will be used by the actual application and outside clients? Is it part of the service layer or does it use the service layer and sits at the same level with the MVC app?

like image 985
Sergey Akopov Avatar asked Jul 12 '12 03:07

Sergey Akopov


People also ask

What is the architecture of Web API?

This is an architectural pattern used for exchanging data over a distributed environment. In Rest, there is something called Client and Server, and the data can be exchanged between the client and server over a distributed environment. Distributed environment means the client can be on any platform like Java, .

What are n-tier Web applications?

N-tier data applications are data applications that are separated into multiple tiers. Also called "distributed applications" and "multitier applications", n-tier applications separate processing into discrete tiers that are distributed between the client and the server.

What is an n-tier architectural style?

An N-tier architecture divides an application into logical layers and physical tiers. Layers are a way to separate responsibilities and manage dependencies. Each layer has a specific responsibility. A higher layer can use services in a lower layer, but not the other way around.

What is an example of N-Tier architecture?

And there are n-tier architecture models that have more than three tiers. Examples are applications that have these tiers: Services – such as print, directory, or database services. Business domain – the tier that would host Java, DCOM, CORBA, and other application server object.


2 Answers

There could be 2 approaches:

  1. You decide to consume your Web API from the MVC application through HTTP calls. In this case the calling code (HttpClient) sits in your Data layer. Whether you are fetching your data from a database or a remote web service call it shouldn't really matter. In this case since the Web API probably already encapsulate much of the business logic your service layer will be very thin, just a wrapper around the data access layer, or even non-existent if it doesn't bring any additional value.

  2. Since the Web API is written in .NET you could decide to directly reference the assembly containing the service layer of this API in your MVC application. In this case the service layer of your Web API application becomes the service layer of your MVC application.

like image 155
Darin Dimitrov Avatar answered Oct 12 '22 03:10

Darin Dimitrov


There are two possibilities

  • Middle-tier or middleware: this is where typically web services and WCF Services have been working. Using REST is much lighter than SOAP so this is de-facto use case. Web and WCF services are better in respect to client generation but Web API will gradually catch up.
  • Presentation layer: this will provide data to the Single-Page-Applications or any modern web site/application that uses data and renders on the client.
like image 20
Aliostad Avatar answered Oct 12 '22 01:10

Aliostad