Web Service Software Factory (July 2006) released

In the last months I’ve been working with a really great team in the Service Factory project at Microsoft. This is a very ambitious project and in the first release you will find a collection of tools to develop web services using the architecture and best practices recommended by the patterns & practices group.
You can download it from the Web Service Software Factory home page and provide your feedback in the community workspace.
Next releases will include integration with .NET 3.0 technologies, like WCF and LINQ, so stay tuned and remember: this is just the beginning!

Problems with LINQ Preview May 2006 installation and its non trivial solution


After loooong time, I decided to bring my weblog back to life. I will try to mantain it more often. Just to begin, I changed the engine with a Wordpress and some plugins that allows me to write posts using wiki syntax. Now I will can share my adventures with new technologies I’m investigating.

I started with C# 3.0 and its wonderful feature LINQ and its friends. I’ve downloaded the may 2006 preview from Microsoft [[http://msdn.microsoft.com/data/ref/linq/|LINQ site]].

After reading some docs and test the examples I wanted to start coding. That’s when I found the suposed (minimal) integration with Visual Studio was not there. Nothing… no templates, no compiler, no syntax highlighting. Finally I found the solution in the [[http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=398051|LINQ forums]]. The problem seems to be that I don’t have VB installed! :S

Installing the MSI with this ‘little’ trick instead of running it directly, fixes the problem without forcing you to install VB:

msiexec.exe /i "LINQ Preview (May 2006).msi"
VCSPROJECTTEMPLATESDIR.C7A0C9D96EB648548BD084A2A4C688EB="C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\ProjectTemplates\CSharp\LINQ Preview\"
VCSPKGDIR.C7A0C9D96EB648548BD084A2A4C688EB="C:\Program Files\Microsoft Visual Studio 8\VC#\VCSPackages\"
VCSIDEDIR.C7A0C9D96EB648548BD084A2A4C688EB="C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\"
DEBUGVISUALIZERDIR.C7A0C9D96EB648548BD084A2A4C688EB="C:\Program Files\Microsoft Visual Studio 8\Common7\Packages\Debugger\Visualizers\"
CSWEBPROJECTTEMPLATESDIR.4AE13B6F621C49B9995EB64016EC2E60="C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\ProjectTemplates\Web\CSharp\1033"
CSITEMTEMPLATEDIR.92D1EB66EA714CFE9C88A25F2388CD0F="C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates\CSharp\1033"
DEVENV="C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\Devenv.exe" DEVENVPATH="C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\"

Hopefuly, this will not be needed in the next versions :P

Generic functors and type inference


C# 2.0 has some features that I see as functional programming features, like anonymous delegates that can be used as “functors” and closure methods in ”Array” and ”IList” classes. Also, some kind of type inference is included when using generics. Here is an example:

string[] ss = new string[] { “One”, “Two”, “Three” };
Array.ForEach(ss, delegate(string s)
{
Console.WriteLine(s);
});

There is no need here to specify the type for the ForEach generic method. It’s infered from parameters, like (really cool!) funcional languages as OCaml or SML does.

However the inference isn’t complete. This will fail because the type cannot be infered from return types :(

string[] ss = new string[] { “1″, “2″, “3″ };
int[] nn = Array.ConvertAll(ss, delegate(string s)
{
return int.Parse(s);
});

Despite of this, all those features are welcomed including closure methods and “functors” delegates like ”Action”, ”Converter” and ”Predicate”.

Hacking Generics


One of the C++ features I ever missed in C# are the templates. Every time I cast the result from an ”IList” or an ”ICollection”, or when I code a custom collection, feels like I’m loosing my time.

I have some math intensive applications developed in C#, and I use some classes like ”Matrix” or ”Vector” to perform some calculations. But no every application needs the same precision for the components of those math entities. Some times I need doubles, but when it’s not the case, using floats or even integers could be a big performance hint. If I were programming in C++, I can use a ”Matrix” class and specify the type I want to use each time, and reuse all the implementation. But there is not solution when programming in C#. It doesn’t exist any pattern or OOP tip that can help us. (Ok… may be some Smalltalk or functional languages programmers start laughing at this point).

Now that Whidbey is coming and generics are become a reality, one can start dreaming how to solve that problem. But, as I can prove later, not all that shines is gold.

I run Visual Studio Whidbey for the first time, and I think that generics are the first feature I will test. I start a new project, and start coding almost without thinking:


public class GenericsTest
{
public T Add(T first, T second)
{
return first + second;
}
}

I try to compile and… Oops! An error explains me the first difference between templates and generics. With generics, “T” will be an ”Object” until I cast to something more representative or restrictions are applied over it using the new keyword: “”where””. I start reading the docs about which restriction will be useful and… oops again! Restrictions can be interfaces, classes or the keyword “”new()”” specifying that “T” must have a default constructor. Nothing useful this time, because numeric types don’t have a common supertype (Smalltalk people smiles again) and even if it existed, operators are implemented as static methods in C# so they will not be inherited.

After some time going into generics undergrounds, I learn how they are implemented in IL and CLR. Removing the problematic return sentence and replacing with a ”return T.default” to avoid more errors the class can be compiled successfully now, and the disassemble looks like that:

.class public auto ansi beforefieldinit
GenericTests<([mscorlib]System.Object) T>
extends [mscorlib]System.Object

{
.method public hidebysig instance !0 Add(!0 first,
!0 second) cil managed
{
// Code size 14 (0xe)
.maxstack 1
.locals init (!0 V_0,
!0 V_1)
IL_0000: ldloca.s V_1
IL_0002: initobj !0
IL_0008: ldloc.1
IL_0009: stloc.0
IL_000a: br.s IL_000c

IL_000c: ldloc.0
IL_000d: ret
} // end of method GenericTests::Add

As you can see, “!0″ means the first type parameter “T”, and the CLR will replace all theirs occurrences with the type specified when an instance is created, and nothing else, like templates do! So, how can we cheat the C# compiler to produce the desired output? Maybe there is no solution at this time, but we can write the code in IL! Or at least, use some ildasm and ilasm.

I rewrite the class thus:

public class GenericsTest
{
public int Add(int first, int second)
{
return first + second;
}
}

After compiling and disassembling, I replace all “”int”” occurrences with “!0″:

.method public hidebysig instance !0
Add(!0 first,
!0 second) cil managed
{
// Code size 8 (0x8)
.maxstack 2
.locals init (!0 V_0)
IL_0000: ldarg.1
IL_0001: ldarg.2
IL_0002: add
IL_0003: stloc.0
IL_0004: br.s IL_0006

IL_0006: ldloc.0
IL_0007: ret
} // end of method GenericTests::Add

I compile again with “ilasm”, and reference it from another project. Now I can create instances like ”GenericTests”, ”GenericTests” to get successful results, or even try ”GenericTests” to see what disastrous can be the trick, but with some precautions it will be so useful.

I know that the solution isn’t so elegant. Maybe nobody wants to disassemble their code each time and recompile again, but some post-build actions can help.

I hope MS enhance generic restrictions and permit something like that:


public class GenericsTest
where T : operator+
{

What do you think?