Library tutorials & articles

The Zen of Volta

Refactoring

Refactoring is a common programmer technique to cleanup and reorganize source code to add new features or make it more maintainable. Volta extends this concept to the compiled code as well by taking the application and changing the code to add new functionality or change the architecture into multiple tiers. This type of IL re-writing has the most potential for making a big impact on the .NET development world today. The Volta team showcased two examples of refactoring: Async and TierSplitting.

Async Refactoring

Async rewriting allows a developer to write a normal, synchronous method and then declare a method signature for the asynchronous version with no implementation. They can then annotate that declaration with the [Async] attribute and Volta will generate the implementation for it automatically. For example, imagine we had a class whose job it was to take a generated log file and reformat it to XML. For a large file this may be a time consuming task so it would be good to be able to run this asynchronously.

With Volta, we can focus on the basic functionality of converting the logfile and not worry about the asynchronous version at all:

class LogfileReformatter {
    public string Reformat(string logFilename){
        string newFilename = null;
        using (FileStream fs = File.OpenRead(logFileNames)) {
            // reformat log file contents
        }
        return newFilename;
    }

    [Async]
    public extern void Reformat(string logFilename, Callback<string&rt; callback);
}

When rewriting the IL, the Volta compiler sees the [Async] attribute and generates the actual code for making the method asynchronous (taken from Reflector):

[Async]
public void Reformat(string logFilename, Callback callback) {
    new __ReformatAsyncHelper(
        new __ReformatAsyncHelper.SyncDelegate(this.Reformat),
                                    callback).Invoke(logFilename);
}

So what is this code actually doing? First, it creates a new object passing in a delegate that is wrapping the synchronous version of the Reformat method and the Callback. Next, it calls Invoke on the new object – let’s have a look at what Invoke does:

public void invoke(string logFilename) {
    this.syncMethod.BeginInvoke(logFilename,
                                new AsyncCallback(this.Handle),null);
}

This should look pretty familiar to most .NET developers – it is simply calling the SyncDelegate instance asynchronously passing a method called Handle that will be called when the method completes. Let’s finally have a look at the Handle method:

private void Handle(IAsyncResult result) {
    this.callback(this.syncMethod.EndInvoke(result));
}

is called on the SyncDelegate instance to get the result of the synchronous call and then the callback is fired passing this result.

So although Volta isn’t generating particularly complex code, it is allowing the developer to concentrate on the business problem at hand and indicate a desire for the operation to support asynchronous execution. Volta takes care of providing the actual implementation of the asynchronous version.

Comments

  1. 01 Jan 1999 at 00:00

    This thread is for discussions of The Zen of Volta.

Leave a comment

Sign in or Join us (it's free).

AddThis

Related podcasts

  • CodeCast Episode 4: State of .NET, IE8, ASP.NET MVC, and O'Reilly Media

    CodeCast Episode 4: State of .NET, IE8, ASP.NET MVC, and O'Reilly MediaHosts Ken Levy and Markus Egger discuss the new State of .NET events, IE8, ASP.NET MVC, followed by an interview from PDC with two editors from O'Reilly Media. More on ASP.NET MVC can be found at http://asp.net/mvc. Interview...

Related jobs

Events coming up

  • Dec 6

    Developing AJAX Web Applications with Castle Monorail

    London, United Kingdom

    Monorail is the model-view-controller engine of the Castle Project, bringing many of the best ideas of Ruby on Rails to the .NET world. In this talk, David De Florinier and Gojko Adzic show how Monorail makes it easy to develop .NET based AJAX applications, and how to use the Castle Project to build Web 2.0 applications effectively. Come to this session if you are a .NET web developer. Everyone is welcome!