18

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?

4
  • In that particular layer breakdown, I would say Presentation .. insofar as the Web API uses the service layer.
    – user166390
    Jul 12, 2012 at 3:56
  • 2
    @pst, this is what i did initially, but it just seems odd to me as it doesn't present any content. So, would Web API simply act as the HTTP wrapper for the service layer? Jul 12, 2012 at 3:59
  • @pst, your comment pretty much confirmed what i was thinking. Why don't you post as answer and i'll accept it. Thanks :) Jul 12, 2012 at 4:07
  • I think someone could flush it out (possibly something different!) with better justification.
    – user166390
    Jul 12, 2012 at 4:17

2 Answers 2

12

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.

4
  • I think the first approach is something along the lines of what i was thinking. You mentioned that HttpClient sits in the Data Layer and this is a bit unclear to me. Should the API sit on top of the Service layer which is on top of the Data layer and essentially act as an HTTP wrapper? I think what you're describing is that the API should replace both layers and the Data Layer is a simple HttpClient or some sort of SDK which queries the API. Is my understanding correct? Jul 12, 2012 at 14:06
  • 2
    If you go with the first approach think of the Web API as something completely external. Inside this API you could once again have a data access layer, service access layer and view models that are exposed. But in terms of your Web API clients this doesn't matter. It's an implementation detail. Jul 12, 2012 at 14:42
  • Aha, i think your last comment made it click. I'm thinking I can build the API on top of the service tier and probably just expose a .NET SDK using RestSharp for the Application tier and any external clients. Thank you for the answer! Jul 12, 2012 at 18:24
  • 1
    The new HttpClient offers a nicer approach than RestSharp in many respects. We've successfully built a couple of REST API clients using HttpClient and exposing IObservable<T> for .NET consumption. Couple that with IApiExplorer (blogs.msdn.com/b/yaohuang1/archive/2012/05/13/…) and you pretty much have a decent mechanism for auto-generating the majority of the required code!
    – Dean Ward
    Jul 12, 2012 at 22:16
1

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.
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.