Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 1,348 times

Contents

Related Categories

The Zen of Volta - Refactoring

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<string>string> 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.

Richard is the CTO of DevelopMentor UK. He began life as a mainframe programmer writing ALGOL and COBOL, but jumped to OS/2 after a year. In 1995, he started developing under Windows and got his first taste of COM. He then spent quite a few years working with COM from both C++ and VB. In 2000, he discovered .NET and has been living in the managed world ever since. Richard spends most of his time these days digging around in the internals of the runtime and living the Service-Orientated Lifestyle with Biztalk, WCF, and WF. He has worked on a number of high profile projects, including being the middle-tier architect of the UK National Police Systems. Other clients have included investment banks, software houses, and financial services companies.

Comments