SOA, SaaS, and Traditional Software

Sep 25, 2006 by

Phil Wainewright just wrote an article discussing the effect of service oriented architectures on “old application categories” and the relationship to SaaS. This prompted me to really think about the effects of the SOA proposition on “old category” software. One of the more interesting things to note is that SOA presents the IT industry with a revolutionary approach to developing new applications; it is really one of the few times that a natural transition exists in the IT world. It gives a way to deliver old application categories as “native” SOA components, possibly allowing them to continue to participate in the new SOA/Software as a service economy.

As with any investment and management scenario, IT departments will look to find away to salvage any old yet still viable investments. Standard economic law will force the IT decision makers to see if there is a way to incorporate a new, cost efficient model like SaaS/SOA while continuing to realize return on prior IT investments. These “old categories” could be transformed through SOA to be value chain participants rather than be discarded. SOA defines contractual obligations that a SOA producer has to an SOA consumer. In addition, it provides a guarantee of what I like to call “implementation blindness”. This means that any useful portions of “old category” software can be repackaged as SOA (assuming no other issue like scalability, etc. pose a problem) and reused, without the danger of being tied to the “old category” because of the natural facade presented by SOA. This concept is what will most likely help both SOA and SaaS to smoothly transition into more and more IT departments.

read more

Related Posts

Tags

Share This

An Asyncronous Callback with the Microsoft AJAX Library (aka Atlas)

Sep 19, 2006 by

In this post I will present an example asynchronous callback to a hosted web service using Microsofts Atlas (August 06 CTP), which will be known as the Microsoft Ajax Library going forward.  The example will highlight two things:

  • How to assemble a custom object and prepare it for asynchronous callback
  • How to handle various responses from the called web service

For those out there that have been working with Atlas for awhile now, this post may seem like old hat, but at the very least it can serve as a refresher.  For those that have little experience with Atlas thus far, hopefully this ‘tutorial’ (I use the term loosely) will serve to bolster your interest.  The post assumes you’ve downloaded and installed the Atlas client-side libraries and templates from the Atlas website.

First Things First: The Service

In this example, I’ll show how to pass custom objects in your callbacks, greatly expanding the capability of client-side code.  To do this, we’ll assume you have a server-side class called Person with two members: string Name, and int Age.  You’ll have created a new web service that we’ll call MyService.asmx (the MyService class) where you will include your WebMethods, like this:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class MyService : System.Web.Services.WebService
{
    [WebMethod]
    public Person SavePerson(Person myPerson)
    {
        try
        {
            //code to add myPerson to the database will go here
            //and on success, we return the resulting Person object
            return newPerson;                        
        }
        catch(Exception e)
        {
            return null;
        }        
    }        
}
public class Person { … }
 

What we’ve setup here is a web service containing the SavePerson web method with a return type of Person that also accepts a Person object as its only parameter.  Atlas has built-in handlers for various unexpected service problems such as timeout, error, and so on that would disrupt the roundtrip of the callback (more on this further down).  If the callback has a successful roundtrip, but the service failed to save the Person object to the database, we can catch that too on the client-side because the webmethod SavePerson will return null.  Now we assemble user input into a Person object at the client,
send it in the request to the service asynchronously, and expect the
resulting Person object on success.  Here’s how:

Assemble the Parameter

 

Let’s say we have a form on the page that allows the user to input a person’s name and age.  We then have a button, or link, that fires the asynchronous callback to save that data as a Person.  Here’s the javascript method to assemble the Person object and send it off to the MyService service SavePerson web method:

function SavePerson()
{
    //get the form data
    var name = document.getElementById(‘myNameElement’).value;
    var age = document.getElementById(‘myAgeElement’).value;       
    // new Person object, made available to us by the page’s
    // Atlas ScriptManager control
    var Person = new Person();   
   
    // populate the Person’s member values
    Person.Name = name;
    Person.Age = age;
   
    // Now, here’s where we make the callback to the
    // SavePerson web method   
    callbackSavePerson = new MyService.SavePerson(
        Person,
        {
            onMethodSuccess: SavePersonSuccess,
            onMethodError: SavePersonError,
            onMethodTimeout: SavePersonTimeout,
            timeoutInterval: 5000;
        }          
    );  
}

 

As you can see, we take the form input, create a new Person object and place the values from the form into the Person members Name and Age.  We then do is execute a callback to the SavePerson web method with a couple parameters: the first is our Person object that the web method is expecting as its only input parameter.  The second parameter is an array of callback handlers that every Atlas web method is given.  These handlers provide the actions to perform when the callback either returns or doesn’t return.  Notice that we can handle different failure reasons using seperate methods… allowing us to let the user know why the callback failed (the service threw an unhandled exception, or the callback didn’t return before the timeout interval).  Assuming the callback makes its roundtrip successfully, we’ll have a return result from the service, which will either be a new Person object, or a null value (as specified by our SavePerson web method).  Now we can write the various handlers: SavePersonSuccess, SavePersonError, and SavePersonTimeout.

Handle the Results

 

SavePersonSuccess(result) will have receive the result parameter – either a Person, or null.  So, we simply do a check:

function SavePersonSuccess(result)
{
    if(result != null) {
        alert(result.Name + ” was saved.”);
    }
    else {
        alert(“The service was unable to save the person.”)
    }
}

 

Remember that if the SavePerson web method returns a success, result will be an instance of the Person object – so we have access to its members: result.Name and result.Age. 

I’m not going to go too far into the  handling of the error or timeout conditions because there’s actually some more advanced things we can do there, and I’ll save that for another post.  In this example, though, we would simply write the specified handlers:

function SavePersonError(result) { alert(“The service encountered an error”); }
function SavePersonTimeout(result) { alert(“The service timed out.”); }

 

So there you have it, we created a web service MyService, with one web method SavePerson.  We called it, passed it a custom Person object, and got back a new instance of the Person object based on what was saved by the service – all asynchronously.  If you’re wondering how the client knew what a Person object was, and why we were able to create a new instance of it on the client-side… well that’s exactly the magic of Atlas and its ScriptManager control, which makes the necessary server-side classes available to us on the client-side.  And yes, it made the Person class available to us because it recognizes that the return type of our web method (SavePerson) was a Person.

For information on how the ScriptManager control works, visit http://atlas.asp.net

read more

Related Posts

Tags

Share This

How to Rewrite Standard Recursion through a State Stack & Iteration

Sep 15, 2006 by

Recursion, as a construct, is quite beautiful. It offers an elegant means of acheiving an algorithmic goal and is used in everything from mathematics to text processing and data structure manipulation. The problem is, using it in practice through today’s popular languages (such as my favorite, C#) can prove to be a disaster. At a minimum, standard recursion proves to be inefficient. Worst case scenario, it’ll consume all of your available memory. So how can you reap the benefits of recursive elegance but avoid the side-effects? Convert your recursive methods to use an emulated stack and iterative recursion. This is something you’ll generally learn in early first level CS courses, but nonethless, it seems to not exist in many people’s engineering vocabularies.Before I get into how to do that, let’s look at why these side-effects happen. Below is a snippet of classic ‘Factorial’ code written in C#:

public static long Factorial(long number)
{
      //Breaks the recursion by checking to see if we’ve reached some
      //terminal condition. In this case, you want to prevent ‘number’
      //from ever being 0. If it was zero, this method would always
      //return 0.
      if(number == 1)
      {
            return 1;
      }
      //Here we make the actual recursive call.
      return number * Factorial(number – 1);
}

While this “Get’s it done” it will experience performance and memory footprint side-effects (to the point of overflowing the call stack.) Each time a recursive method calls itself, a return pointer is stored on the call stack (I’m not counting on any sort of tail-end optimizations) and a memory register allows the program to recall the parameters and variables that were being used during each instance of the call. If enough recursive calls are made, all of these variables (and return pointers) end up consuming your memory and exploding your call stack. This creates a limitation on the parameter range your recursive method can work with. For example, although you could make a call like ‘Factorial(10000)’, putting aside the limitation of long, odds are it’ll blow the stack. Second, the function call and frame pause associated with the recursive call will prove to be a performance bottleneck that become more pronounced as the call stack gets larger. These issues arise with even the simplest recursive methods. More complex ones (such as tree navigation) may break quicker. Now on to iterative recursion.

Call-stack emulation is not a new concept. It basically emulates the recursive process using a looping construct and a state stack, but avoids the problems associated with making function calls and extends the life of your available memory. As a result, you end up with psuedo-recursive methods that run faster and that can execute on wider parameter ranges. The problem is, many people have difficulty in converting a standard recursive method to an iterative one. Before we get into the conversion process, below is the same Factorial method, rewritten. Afterwards, we’ll pick it apart:

public static long Factorial(long number)
{
      //This stack acts as a memory register for what would normally
      //be the parameters of a recursive method. The first thing we
      //push is the first parameter value.
      Stack<long> numStack = new Stack<long>();
      numStack.Push(number);      long factorial = 1;
      while(numStack.Count > 0)
      {
            long currNum = numStack.Pop();
            factorial = factorial * currNum;            if(currNum != 1)
            {
                  numStack.Push(currNum – 1);
            }
       }      return factorial;
}

Looks a tad different, no? So how did we get from version1 (crappy) to version 2 (not so crappy)? There are a few rules of thumb that make conversion easier (tweak them to fit your need, of course). First, parameters. Normally, when you make a recursive call, your parameters from previous calls are “remembered” through the use of stack frames. In iteration, we have to build something to remember previous parameters ourselves. As a result, we use our own Stack object. So:

Rule 1: Have either 1 stack for each parameter type in your method or a stack that can store a more complex object that contains all of your parameters. For example, the new Factorial method has a Stack<long> because we have one long parameter. If my method took a long and a string, then I would have either had two stacks, one for the long parameter and one for the string parameter or I would have had one object that stored both and pushed that on the stack.

We use this Stack object to push the parameters of each iteration. Second, you need a place to store the result. This is an instance of the data type being returned.

Rule 2: Have a way of storing your result, which is what is being calculated/arrived at recursively.

Granted, this is applicable only if you are in fact returning something. In the Factorial scenario, we have long factorial = 1 as our storage “device”. Our next rule is:

Rule 3: Have a loop of some sort that loops until the stack is empty.

Looping until the Stack is empty is equivalent to saying “Stop unwinding stack frames when there are none left.” Although the quoted statement is a triviality, we need to explicitly define this since we are emulating the trivial notion. The body of the loop is basically the same as the scope defined by your method body in a standard recursive function, so treat it that way! Do your “work” in there. First, pop a variables(s) off of your stack(s). These are like using the parameters of the current stack frame in traditional recursion. Use these parameters and your algorithm to manipulate your result variable. Last rule:

Rule 4: Map your control condition and recursive calls.

Your control condition (in our case if(number == 1)) in the original function generally stays intact. Your recursive call(s) now maps to stack pushes, however. For each recursive call, you will generally have one stack push. Rather than using the control condition to determine when to stop recursively calling, you now use it to determine when to stop pushing on the stack. In our iterative version, we simply push the current popped variable minus 1 (ala the original recursive method) ontot the stack. The next iteration, it will be popped off and used as the current variable.

You now have a far better version of your recursive method. One thing to note: this example is trivial in the sense that your stack never grows past one because of the nature factorial, which can be calculated using a standard loop. The stack is simply used to to illustrate the state-based requirement of emulating parameter passing in a recursive call. If you implement iterative recursion for something like navigating a tree, you’ll see your stack grow past a count of one.

Thats it! Hopefully, more people take the iterative approach and help in making software better. If you need any help understanding, feel free to comment;)

Update: After a couple of comments, I realize I shouldn’t have used Factorial, which you can calculate quite simply with an iterative loop anyway. Keep in mind that the goal of this post was to provide a lowest common denominator example. The above process works for basic recursive needs.

read more

Related Posts

Tags

Share This

The Pizza Box Metric

Sep 8, 2006 by

A couple of years ago, I read an article about the origins of Yahoo! The article described the infamous Yahoo! trailer and the scene where Michael Moritz of Sequoia Capital first walked in and encountering a mess, complete with drawn shades and pizza boxes strewn across the room. The dwelling seemed fit for dogs, or people who had enough on their plate to worry about silly things such as cleanliness. Although Jerry Yang and Dave Filo popularized this notion of what an entreprenuers den looks like, they definitely are not alone. Many people I’ve talked to either told me “My office is a wreck” or “I know this gal who’s an entrepreneur, and her office is a mess!” At first, one might confuse this with being unorganized, but for a good entrepreneur, quite the contrary is true.

I recently had a friend come to my office, and the first thing he exclaimed was “Holy s***, this place is disgusting, and that’s coming from a messy guy!” I looked around and saw <gasp> …a pizza box! In the struck me; we really are knee deep. I’m normally quite clean, but once I’m in our office, my priorities change. I don’t really care about the junk that is around the room. What I care about is a.) am I on schedule, b.) am I on budget c.) are the tools and data I need organized and d.) do I have food delivery numbers nearby. So next time you walk in to an entrepreneurs office, count how many pizza/chinese food/subwrapper/bottle containers you find, and you’ll be able to gauge with atomic accuracy how busy, enthused, and involved they really are.

FYI: This friend made me help him clean the offfice, so I’m kind of starting from scratch. I’m now behind, making his move the equivalent of sending me directly to jail in Monopoly. Where’s my phone and Domino’s number…

read more

Related Posts

Tags

Share This