1

Is there any benefit to using a Web API in a normal CRUD based ASP.NET MVC application? I understand that using the Web API does give us the benefits of the RESTful service layer but I do not favor replacing the entire service layer (and the underlying business and data access layers) and using only the service to get to the database. Instead, I feel that we should just expose the few features as services as and when required in case the client wants to support other applications like a SPA or mobile version of the app.

The question is whether in my situation when most of the work is returning complex HTML views to mainly one application whereas the internal operations are mainly going to be CRUD and maybe some business logic it makes sense to go for a complete service layer. Are there any performance concerns because of having to create a proxy? What benefits does this kind of architecture give in scaling up? Too many questions. Just confused. I don’t see the need but maybe I am missing something?

The idea of having an SOA seems good but worried that it has a performance downside.

2 Answers 2

3

I believe it is a much better option to share business logic library between the web site and the web API than it is to have the server side web site code call the web API.

HTTP is protocol optimized for low latency and independently evolving components. If the same team control both the web site and the web API, and they are deployed simultaneously, and they both live in the same data center there is little value in using HTTP to talk to each other.

The other problem you will face if you try using your Web API to supply data to your web site is that you are likely to build a chatty Web API that works fine in your datacenter but when remote clients try and use it you can have performance issues.

If you want your Web APIs consumed by third parties to be efficient and effective they should be built to expose use-cases, not the domain entities that your web site needs access to.

Sign up to request clarification or add additional context in comments.

2 Comments

So, do you think its a good strategy to use the business logic layer as a componenent in the website and then later on add a web api layer to expose use cases for third-party, or maybe even consumed by an SPA, mobile version of our application? Strategy 2 in the following link is i guess what you are suggesting - odetocode.com/blogs/scott/archive/2013/07/01/…
@user1473638 Yes. One way to do this is to publish your business layer as an internally available Nuget package. This can make managing different versions of your business logic easier.
1

WebAPI would be a good idea for you in two cases:

  1. Other applications may need to access the same data, and you only want to write the business logic / data access logic once.
  2. You want to write browser-side javascript that will make AJAX calls back to the server-side of your application to retrieve data.

For #2, you can use regular MVC and JsonResponse handlers too. In my applications I use both. If I'm just returning data and I want to directly serialize my model objects, I'll set up a WebAPI controller. But if I need to run the model through Razor templates, I have an extension method on my controller which lets me render a view into a string, and then I return that in a JSON object as one property, with some other properties set for any metadata I need to return like success codes, error messages, serialized exception objects, etc.

2 Comments

yes, for 1. I was just wondering if I can write a service layer (more like an API) which will inturn call the same business logic layer(class library). Is that possible? Would I have to rewrite the business layer incase I decide later that some business features need to be available as a service? In my mind, the web api service layer can sit on top of the business layer without composing the entire business layer and the DAL layer. Is that even feasible?
No, there should be no need to use a different business layer or make modifications to it later in order to add a WebAPI service layer, so long as you've cleanly separated UI logic from business logic. For example, I often use business layer models as the models I pass to my views, but I also create UI-specific model classes so I can pass UI-specific information to my views. Those models go into the MVC project's /Models folder, while the others are generally in a separate library project for the business layer. This helps keep the layers separate and the business models usable in services.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.