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.

If you were building a new Saas project, would it make sense to start with all of the backend services returning xml/json?

Because these days you need to build for both the web and mobile devices, and having a backend that is build from the start to return xml and json, you are ready to go mobile (all services have the business logic, so you won't be repeating anything).

Now the web would be MVC, so the controller would just be routing the request to your service backend, and converting the json or xml to html.

The obviousl downside is that you have to build a backend, and then another web project that calls your backend. But this also goes to you favor as it forces you to seperate your concerns, and not leak business logic in your controller/view layer.

Thoughts?

share|improve this question
4  
It's hard to say anything definitive about a group like "all new web projects." As written, this question doesn't seem very constructive. –  Caleb Sep 17 '12 at 4:29
add comment

closed as not constructive by Walter, Caleb, GrandmasterB, gnat, Bill the Lizard Sep 17 '12 at 19:55

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

8 Answers

There is not a YES or NO answer to your question. The decision to introduce a second level of indirection in the form of an xml/json API depends on what you are about to do with your project.

As you mentioned, many projects go mobile these days, however, isn't there a simpler way to provide mobile functionality by simply presenting a customized version of your web interface? I can't answer this but maybe you can.

Other questions you may ask yourself include: Are there any others willing to write clients to my application? If the answer is yes, than an xml/json API may be a good way to go.

EDIT: PS - whatever way you go, separating the business logic from the MVC has the same importance. An API would force you to do that, but also without a second level of encapsulation you should be able to do it just fine.

share|improve this answer
add comment

No.

XML/JSON are just data formats.

It makes sense to have a clear separation between business logic and presentation (unless it's a small project). If you have procedures/functions/methods whatever that expose the operations in you application, which are not "contaminated" with presentational matters (i.e. they take primitives, business-logic objects instead of HTTP requests, GUI events, etc.), it's fairly easy to expose them in different manners- XML, JSON, whatever, and of course you can invoke them directly from the applicable presentation layers.

You don't need to force a "hard" separation- it really doesn't require that much discipline, and even if you required separate projects for other reasons (e.g. different teams, etc.), I would favor using the backend as a "library" instead of as a service if possible- direct method invocation has less overhead in all senses and is highly desirable.

share|improve this answer
add comment

It depends on your application. A lot of businesses simply don't need a web API services layer and can use standard MVC practices to serve up rendered pages from well designed thin controllers. This can be much faster to create and easier to maintain than something like a view layer written entirely in unmaintainable javascript(most is) that hits your API.

Most frameworks assume the view will be rendered in the same application so you'll be fighting that assumption and losing out on a lot of 'free' functionality. Business logic can leak either way so it's a question whether the extra layer of abstraction gives you anything.

share|improve this answer
add comment

Yes, I think it makes really good sense and is an excellent architecture. It's so simple to return JSON/XML to the client transparently from the server - it does not require writing an additional layer. Javascript clients easily consume JSON. You have a clean separation of concerns. You can generate code from the shared model for compile-time validation if you choose. And of course you have the flexibility mentioned in the question of supporting additional clients e.g. mobile. Don't forget the backend technology can change too. We swapped out a .NET backend for a java/spring/tomcat backend at my last job.

share|improve this answer
add comment

No, don't expose all business services 'blindly', with the one possible exception is if your app has a 100% smart-client front-end (i.e. only consider this if NONE of your dynamic Html is rendered from the server).

Rationale is:

  • This will slow down the performance of Controller rendered screens unnecessarily due to the additional TCP/IP stack 'hop'
  • It creates a larger surface area for attackers - you may forget to lock down services which you aren't actually using.
  • Exposing of services must be done in a formalised manner (e.g. off your controllers for JSON / browser clients, or a formal set of integration services, e.g. web services). If you don't do this, in an enterprise, you may find yourself with a bunch of consumers of your services who you don't even know about.
  • In many instances (in .NET anyway), exposing JSON services to a browser is best done from the MVC controller, since Ajax services are often tightly coupled to views. This isn't the right place for exposing integration services (e.g. SOAP / WSE) to other systems.

In short, you are right about moving logic out of your views and controller into a 'back end layer', but the coupling between controller and back end isn't necessarily across a web service boundary.

share|improve this answer
add comment

With the rise of the Javascript client-side UI rendering frameworks like backbone.js and other various tricks like pjax, this option is becoming more and more popular.

It's always a good idea to separate business logic from presentation concerns.

Depending on your budget/time, you may want to skip the public API (fiddling with OAUTH stuff is a big time sink by itself), and build your controllers and views to consume the json result set returned by your application specific code. I'm going with this approach for some time, and it pays back sooner than I expected.

You might want to read The Clean Architecture, a great blog post from Robert Martin(UncleBob). Some good advice in there.

share|improve this answer
add comment

Most probably yes, if your clients are going to consume Restful HTTP services.

There is a growing trend in web/mobile applications to extensively use HTTP services with json/xml formatted data exchange. Thus, if you are developing on .NET platform there is ASP.NET Web API technology that will ease your development, and provide a most fundamental features out of the box.

Even if you are developing for different platform, still http services and mentioned data formatted data exchange type is starting to play important role, and it is getting more and more important to move toward that direction.

share|improve this answer
add comment

Every time I have had to re-purpose a screen in a web application, the new purpose has needed slightly different queries and different business logic. The idea of reuse is great, but I haven't seen it play out that way in the 12 years I've been a professional Web Application Developer. Your mileage may vary.

share|improve this answer
add comment

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