Wednesday, July 2, 2008

The SOA Supermarket

Dojo does a lot of things, but it can't pull data from just anywhere. The browser won't let it. That's the first heartbreaking lesson you learn in Ajax-land, your bar.com page can't send XHR requests directly to foo.com.

There are ways around it. The hacky-but-in-vogue method is JSONP, which says "run me this little JavaScript script at foo.com, and come back when you're done with the data." Yahoo and Google web services offer JSONP counterparts to their search services, and Dojo has really nice plumbing for calling them. We go over this in Mastering Dojo chapters 3 and 8. The bad news: a very small number of web services support JSONP.

A more universal solution is a proxy server. Your bar.com page will call the server, "Go fetch me this request at foo.com, will you?" bar.com, not subject to the cross-domain restrictions of a browser, will do that and serve back the results. And it's easy. One line of PHP nets you a proxy server. That's fine for starting out, but as you start taking advantage of more services, all the little details start becoming a PITA: security, routing, data conversion, etc.

And that's where Service Oriented Architecture (SOA) comes in. Here I don't mean just SOAP-based web services, but rather services as a way-of-life: loosely-coupled components from the Internet cloud, connected with orchestration glue in the middle. Your Dojo application shouldn't have to know about foo.com. Instead, it should know "answer this question and give me the answer in JSON format." The SOA server knows to ask foo.com, and convert the answer to JSON. And if foo.com is unreachable, it could contact anotherfoo.com for the same question. And so forth.

The point is SOA saves you coding on the middle tier. And let's face it - you really need to write less code there. Back in 1998, you could spend 20 hours a week on ASP, 20 hours a week on the database, let's say. Then Ajax comes along, and you need to do 15 hours of JavaScript (Dojo helps you here - it could be worse!) How do you squeeze 55 hours into a 40 hour work week? Eliminate your social life?

What I advocate is cutting ASP out altogether - adding 5 hours a week to moving logic back to the database tier, and 5 hours a week of SOA configuration. As you create more reusable Dojo components, you can cut 5 hours from your client-programming time. Voila! You're back to 40 hours, and you're building better apps.

So now your middle tier is a combined POWS (plain ol' web server) and SOA server. What do SOA servers look like? What do they do for you? And how do you choose from the veritable swamp of alternatives? I have some thoughts, which I'll share in the next post.