Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am working on a project in MVC that has mobile application so one thing is clear that we have to use Web API so it can used in mobile application.

After creating API when we started to develop Web site we are confused and had discussion on whether to use API or directly access to the Business object. And we ended up after having opinion form more experienced developer to consume Web API instead of using Business object directly.

I'm having confusion regarding to this solution structure.

1) Why we should use Web API and make HTTP request (which is time consuming) to get or put data instead of business object directly which is in same solution.

2) After having arguments they said what if client wants to host API and web on different cloud server and apply scaling only on API or may be he want to have different url for accessing API and Web (which is some what logical). So in that case should we call Web API from MVC application in same solution?

3) If we're hosting API and Web on different hosting so it means our Web will use WebClient and have HTTP call on each navigation. Is it right?

4) If we'll business object form both API and Web hosting on different server then if something change in BL will need to update build on both server.

5) Or we should create only one project for API and can add views or html pages to develop Web interface so in that way we can directly call API from ajax.

As per my knowledge #5 is the best solution or API is only for 3rd party access. If we have DB, EF, data layer and business layer in same solution then we should not use API to make HTTP calls and directly access business object. (correct me if I'm wrong)API is needed when mobile application or desktop or any one want to access application so we can have same repository and data layer.

In my scenario I've to create API as we also have mobile application, and in project API side we called business layer (separate project) and business layer communicate to data access layer (separate project). So my question is if we host our API and web to different servers then calling API which is a HTTP request may take longer rather than using method from business layer as we create the project and we've .dll of business layer. In API controller we just convert out put of our business to json format.

I've searched on internet but didn't get convincing answer. I've found a blog http://odetocode.com/blogs/scott/archive/2013/07/01/on-the-coexistence-of-asp-net-mvc-and-webapi.aspx discussing same point but again in that blog my question is why we need to consider scenario #3?

Update: We can have different API project and MVC project and we can call API from web using jvascript or can use MVVM pattern.

share|improve this question
    
MVVM or MVC with view models? –  Andrew Hoffman Jun 30 at 16:50
    
We're using MVC –  Ruchir Shah Jun 30 at 16:55
    
Ok then, then there's no correct answer really. There are benefits to only calling your API when it comes to improving their quality. (eating your own dogfood) There are also performance benefits to making inproc calls instead of calling through services. –  Andrew Hoffman Jun 30 at 17:00
    
Google went through this question once. Their products are both services and websites. I believe they decided upon having their websites consume their own services. –  Andrew Hoffman Jun 30 at 17:03
2  
Yes. programmers.stackexchange.com/questions/148556/… and stackoverflow.com/questions/3590561/… are good resources. Some do, some don't. No real 'correct' way. –  Andrew Hoffman Jun 30 at 17:41
show 1 more comment

1 Answer 1

Directly accessing your business objects directly (I assume you mean in your controller) will be quicker & easier.

what if client wants to host API and web on different cloud server and apply scaling only on API or may be he want to have different url for accessing API and Web (which is somewhat logical)

Then you'll need to host them separately... but that doesn't sound very logical to me, surely you would want to scale both. Are you sure you need to cater for this requirement? That sounds like overkill. Remember YAGNI - if you don't need it, don't build it. When you need it, build it.

If it was me I'd build the site using the technology that fits the best the site, then when (if) you need a service that other people can call build that separately.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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