Library tutorials & articles
The Zen of Volta
- IL Re-writing
- Refactoring
- Tier Splitting
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.
Related articles
Related discussion
-
Binary Studio | software development outsourcing Ukraine
by shane124 (4 replies)
-
Research topic in software
by reachsangeethamathew (0 replies)
-
career improvement advice
by hnasr82 (0 replies)
-
Advice on studying and preparing for interviews
by caryatid (0 replies)
-
Chart insertation in a windows form...
by pdhanik (1 replies)
Related podcasts
-
More jQuery in ASP.NET
In this episode Chris Brandsma, Rick Strahl, Dave Ward, Bertrand Le Roy, and Scott Koon conclude their discussion of Microsoft's jQuery in ASP.NET announcement1.This episode of the Alt.NET Podcast is brought to you by LLBLGen Pro, the most mature O/R mapper and code generator out there.Are ...
Events coming up
-
Nov
18
15 Minutes of Fame
Dresher, United States
This is a yearly tradition. We select 10 of the favorite speakers from monthly meetings, code camps, and hands on labs. Each one does a 15 minute talk on their favorite .NET technology. This is our 10th anniversary so we plan a gala event with special prizes and refreshments.
This thread is for discussions of The Zen of Volta.