<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Blog</title><link>http://www.erik-kallen.se:80/blog</link><description>Blog</description><item><title>Topological sorting in .net</title><link>http://www.erik-kallen.se:80/blog/topological-sorting-in-.net</link><description>&lt;p&gt;Every now and then I need to &lt;a href="http://en.wikipedia.org/wiki/Topological_sorting"&gt;topologically sort&lt;/a&gt; some data. Usually I end up just writing a trivial implementation of the algorithm. I have done this countless times, but my last problem had a new twitch: there might be nodes with circular references in my graph. So this time the problem was hard enough to warrant some googling, and when I found an algorithm (&lt;a href="http://en.wikipedia.org/wiki/Tarjan's_strongly_connected_components_algorithm"&gt;Tarjan's&lt;/a&gt;) and coded it up, I decided to make it generic enough to solve all my topological sort problems for all future. Presumably I am not the only one who occasionally needs topological sorting, so it might be useful for a general public. Enjoy (BSD license)!&lt;/p&gt;
&lt;pre class="brush:csharp"&gt;public static class TopologicalSorter {
	// Tarjan's algorithm: http://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm
	private class Algorithm&amp;lt;T&amp;gt; {
		private readonly ILookup&amp;lt;T, T&amp;gt; _edges;
		private readonly IEqualityComparer&amp;lt;T&amp;gt; _comparer;

		private Dictionary&amp;lt;T, int&amp;gt; _indices;
		private Dictionary&amp;lt;T, int&amp;gt; _lowlink;
		private List&amp;lt;IList&amp;lt;T&amp;gt;&amp;gt; _result;
		private Stack&amp;lt;T&amp;gt; _s;
		private int _index;

		public Algorithm(ILookup&amp;lt;T, T&amp;gt; edges, IEqualityComparer&amp;lt;T&amp;gt; comparer) {
			_edges = edges;
			_comparer = comparer ?? EqualityComparer&amp;lt;T&amp;gt;.Default;
		}

		public List&amp;lt;IList&amp;lt;T&amp;gt;&amp;gt; MainLoop(IEnumerable&amp;lt;T&amp;gt; vertices) {
			_result = new List&amp;lt;IList&amp;lt;T&amp;gt;&amp;gt;();
			_indices = new Dictionary&amp;lt;T, int&amp;gt;(_comparer);
			_lowlink = new Dictionary&amp;lt;T, int&amp;gt;(_comparer);
			_s = new Stack&amp;lt;T&amp;gt;();
			_index = 0;
			foreach (var v in vertices) {
				if (!_indices.ContainsKey(v)) {
					StrongConnect(v);
				}
			}
			return _result;
		}

		private void StrongConnect(T v) {
			_indices[v] = _index;
			_lowlink[v] = _index;
			_index++;
			_s.Push(v);

			foreach (var w in _edges[v]) {
				if (!_indices.ContainsKey(w)) {
					StrongConnect(w);
					_lowlink[v] = Math.Min(_lowlink[v], _lowlink[w]);
				}
				else if (_s.Contains(w)) {
					_lowlink[v] = Math.Min(_lowlink[v], _indices[w]);
				}
			}

			if (_lowlink[v] == _indices[v]) {
				var scc = new List&amp;lt;T&amp;gt;();
				T w;
				do {
					w = _s.Pop();
					scc.Add(w);
				} while (!_comparer.Equals(v, w));
				_result.Add(scc);
			}
		}
    }

	public static IList&amp;lt;IList&amp;lt;T&amp;gt;&amp;gt; FindAndTopologicallySortStronglyConnectedComponents&amp;lt;T&amp;gt;(IEnumerable&amp;lt;T&amp;gt; source, IEnumerable&amp;lt;Tuple&amp;lt;T, T&amp;gt;&amp;gt; edges, IEqualityComparer&amp;lt;T&amp;gt; comparer = null) {
		var result = new Algorithm&amp;lt;T&amp;gt;(edges.ToLookup(e =&amp;gt; e.Item1, e =&amp;gt; e.Item2), comparer).MainLoop(source);
		return result;
	}

	public static IList&amp;lt;IList&amp;lt;TSource&amp;gt;&amp;gt; FindAndTopologicallySortStronglyConnectedComponents&amp;lt;TSource, TVertex&amp;gt;(IEnumerable&amp;lt;TSource&amp;gt; source, Func&amp;lt;TSource, TVertex&amp;gt; getVertex, IEnumerable&amp;lt;Tuple&amp;lt;TVertex, TVertex&amp;gt;&amp;gt; edges, IEqualityComparer&amp;lt;TVertex&amp;gt; comparer = null) {
		var backref = source.ToDictionary(getVertex, comparer ?? EqualityComparer&amp;lt;TVertex&amp;gt;.Default);
		return FindAndTopologicallySortStronglyConnectedComponents(backref.Keys, edges, comparer).Select(l =&amp;gt; (IList&amp;lt;TSource&amp;gt;)l.Select(t =&amp;gt; backref[t]).ToList()).ToList();
	}

	public static IEnumerable&amp;lt;T&amp;gt; TopologicalSort&amp;lt;T&amp;gt;(IEnumerable&amp;lt;T&amp;gt; source, IEnumerable&amp;lt;Tuple&amp;lt;T, T&amp;gt;&amp;gt; edges, IEqualityComparer&amp;lt;T&amp;gt; comparer = null) {
		var result = FindAndTopologicallySortStronglyConnectedComponents(source, edges, comparer);
		if (result.Any(x =&amp;gt; x.Count &amp;gt; 1))
			throw new InvalidOperationException("Cycles in graph");
		return result.Select(x =&amp;gt; x[0]);
	}

	public static IEnumerable&amp;lt;TSource&amp;gt; TopologicalSort&amp;lt;TSource, TVertex&amp;gt;(IEnumerable&amp;lt;TSource&amp;gt; source, Func&amp;lt;TSource, TVertex&amp;gt; getVertex, IEnumerable&amp;lt;Tuple&amp;lt;TVertex, TVertex&amp;gt;&amp;gt; edges, IEqualityComparer&amp;lt;TVertex&amp;gt; comparer = null) {
		var backref = source.ToDictionary(getVertex, comparer ?? EqualityComparer&amp;lt;TVertex&amp;gt;.Default);
		return TopologicalSort(backref.Keys, edges, comparer).Select(t =&amp;gt; backref[t]);
	}
}
&lt;/pre&gt;</description><pubDate>Sat, 12 Jan 2013 19:37:03 GMT</pubDate><guid isPermaLink="true">http://www.erik-kallen.se:80/blog/topological-sorting-in-.net</guid></item><item><title>Refactoring the node.js file system browser</title><link>http://www.erik-kallen.se:80/blog/refactoring-the-nodejs-file-system-browser</link><description>&lt;p&gt;In the &lt;a href="writing-nodejs-applications-in-csharp"&gt;last post&lt;/a&gt; we used the &lt;a href="http://www.saltarelle-compiler.com"&gt;Saltarelle C# to Javascript compiler&lt;/a&gt; to create a simple file system browser. Although it worked, the code was quite messy and a clear violation of the SRP since the same method was responsible for both reading the contents of a directory as well as rendering the output. In this post we will refactor the code that does the directory reading into a separate module.&lt;/p&gt;
&lt;h2&gt;Creating the project&lt;/h2&gt;
&lt;p&gt;Start with following the instructions in the last post, but this time call the project ModularizedNodeJSFileBrowser.&lt;/p&gt;
&lt;p&gt;Now add a new class library called DirectoryEnumerator and install the Saltarelle.NodeJS package into this project as well. Unload and reload, and remove the attributes it complains about when building. Now open Properties\AssemblyInfo.cs and add&lt;/p&gt;
&lt;pre class="brush:csharp;gutter:false"&gt;[assembly: ModuleName("DirectoryEnumerator")]&lt;/pre&gt;
&lt;p&gt;to it. This instructs the compiler to add all exported symbols to the exports variable rather than to the global object, as well as it tells any assembly that references this assembly which module it should require() in order to get access to the exported symbols. However, there is no magic that tells node where to find the script, so we set up a post-build task to copy it into the node_modules folder of the application project:&lt;/p&gt;
&lt;pre class="brush:text;gutter:false"&gt;copy "$(TargetDir)$(TargetName).js" "$(ProjectDir)..\ModularizedNodeJSFileBrowser\node_modules"
&lt;/pre&gt;
&lt;p&gt;Now also add a reference from the application project to the class library.&lt;/p&gt;
&lt;h2&gt;Implementing the directory enumerator&lt;/h2&gt;
&lt;p&gt;Now it is time for us to actually implement the directory enumerator module. First create a type to hold the returned results by creating a file called DirectoryEntry.cs with the following content:&lt;/p&gt;
&lt;pre class="brush:csharp"&gt;using System;
using System.Runtime.CompilerServices;

namespace DirectoryEnumerator {
	[Imported]
	[Serializable]
	public class DirectoryEntry {
		public string Name { get; private set; }
		public bool? IsDirectory { get; private set; }
		public int? Size {  get; private set; }
		public Error Error { get; private set; }

		public DirectoryEntry(string name, bool? isDirectory, int? size, Error error) {
		}

		public DirectoryEntry(string name, Error error) {
		}
	}
}
&lt;/pre&gt;
&lt;p&gt;If you used the Visual Studio Add Class command to do this, it will have inserted a reference to System, whcih you have to remove. This is really annoying, but it has to be done every time the Add Class command is used.&lt;/p&gt;
&lt;p&gt;The Imported and Serializable attributes on this type will ensure that it will not appear in the generated script and that Javascript object literal syntax, &lt;span style="font-family: courier new;"&gt;{ name: value }&lt;/span&gt;, will be used whenever we create an instance (for more information on these and other useful attributes that control the generated script, see the &lt;a href="http://www.saltarelle-compiler.com/documentation/attributes" target="_blank"&gt;attribute list&lt;/a&gt;). The code would have worked perfectly fine without these attributes, but the generated code would have been different.&lt;/p&gt;
&lt;p&gt;Now create a file called DirectoryEnumerator.cs with the following content:&lt;/p&gt;
&lt;pre class="brush:csharp"&gt;using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using NodeJS.FSModule;

namespace DirectoryEnumerator {
    [IgnoreNamespace]
    public class Enumerator {
        public static async Task&amp;lt;List&amp;lt;DirectoryEntry&amp;gt;&amp;gt; EnumerateDirectory(string physicalPath) {
            string[] files = await FS.ReaddirTask(physicalPath);

            Task&amp;lt;Stats&amp;gt;[] statTasks = files.Map(f =&amp;gt; FS.StatTask(physicalPath + "\\" + f));

            try {
                await Task.WhenAll(statTasks);
            }
            catch (AggregateException) {
            }

            var result = new List&amp;lt;DirectoryEntry&amp;gt;();
            for (int i = 0; i &amp;lt; files.Length; i++) {
                var t = statTasks[i];
                if (t.IsFaulted) {
                    result.Add(new DirectoryEntry(files[i], ((JsErrorException)t.Exception.InnerExceptions[0]).Error));
                }
                else {
                    result.Add(new DirectoryEntry(files[i], t.Result.IsDirectory(), t.Result.Size, null));
                }
            }

            return result;
        }
    }
}
&lt;/pre&gt;
&lt;p&gt;The single method in this class will take a physical path as an argument and asynchronously return all items in the directory. The [IgnoreNamespace] is another one of those attributes that control the generated script. If you want to experiment, you can change this to [GlobalMethods] and see what happens to the generated script. The content of the method is very similar to what was covered in the last post.&lt;/p&gt;
&lt;h2&gt;Implementing the application&lt;/h2&gt;
&lt;p&gt;Now replace the content of the Program.cs file in the application project with this:&lt;/p&gt;
&lt;pre class="brush:csharp"&gt;using System;
using System.Collections.Generic;
using DirectoryEnumerator;
using NodeJS.HttpModule;
using NodeJS.PathModule;
using NodeJS.UrlModule;

namespace ModularizedNodeJSFileBrowser {
    class Program {
        static void Main() {
        Http.CreateServer(async (req, res) =&amp;gt; {
                try {
                    var url = Url.Parse(req.Url);
                    var path = url.Path;
                    if (path.StartsWith("/"))
                        path = path.Substr(1);
                    if (path.EndsWith("/"))
                        path = path.Substr(0, path.Length - 1);

                    var physicalPath = "C:\\" + string.DecodeUri(Path.Normalize(path));

                    List&amp;lt;DirectoryEntry&amp;gt; content;
                    try {
                        content = await Enumerator.EnumerateDirectory(physicalPath);
                    }
                    catch (Exception ex) {
                        if (ex is AggregateException)
                            ex = ((AggregateException)ex).InnerExceptions[0];
                        res.Write("&amp;lt;html&amp;gt;&amp;lt;body&amp;gt;");
                        res.Write("Error reading directory:&amp;lt;br&amp;gt;");
                        res.Write(ex.Message + "&amp;lt;br&amp;gt;");
                        res.Write("&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;&amp;gt;");
                        return;
                    }

                    res.Write("&amp;lt;html&amp;gt;&amp;lt;body&amp;gt;");

                    if (content.Count == 0) {
                        res.Write("The directory is empty");
                    }
                    else {
                        res.Write("&amp;lt;ul&amp;gt;");
                        if (path != "") {
                            res.Write("&amp;lt;li&amp;gt;&amp;lt;a href=\"/" + Path.Normalize(path + Path.Sep + "..") + "\"&amp;gt;..&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;");
                        }

                        foreach (var entry in content) {
                            res.Write("&amp;lt;li&amp;gt;");
                            if (entry.Error != null) {
                                res.Write(entry + ": " + entry.Error.Message);
                            }
                            else {
                                if (entry.IsDirectory.Value) {
                                    var newPath = "/" + (path != "" ? path + "/" : "") + entry.Name;
                                    res.Write("&amp;lt;a href=\"" + newPath + "\"&amp;gt;" + entry.Name + "&amp;lt;/a&amp;gt;");
                                }
                                else {
                                    res.Write(entry.Name + ": " + entry.Size + "B");
                                }
                            }
                            res.Write("&amp;lt;/li&amp;gt;");
                        }
                        res.Write("&amp;lt;/ul&amp;gt;");
                    }
                    res.Write("&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;");
                }
                finally {
                    res.End();
                }
            }).Listen(8000);
        }
    }
}
&lt;/pre&gt;
&lt;h2&gt;Final Words&lt;/h2&gt;
&lt;p&gt;This code is available in the&amp;nbsp;&lt;a href="https://github.com/erik-kallen/SaltarelleCompilerSamples" target="_blank"&gt;Saltarelle Compiler Samples&lt;/a&gt;. Of course there are other possible refactorings to do with this code, but those are left as an exercise to the reader.&lt;/p&gt;</description><pubDate>Thu, 11 Oct 2012 09:58:00 GMT</pubDate><guid isPermaLink="true">http://www.erik-kallen.se:80/blog/refactoring-the-nodejs-file-system-browser</guid></item><item><title>Writing node.js applications in C#</title><link>http://www.erik-kallen.se:80/blog/writing-nodejs-applications-in-csharp</link><description>&lt;p&gt;&lt;a href="http://www.nodejs.org" target="_blank"&gt;Node.js&lt;/a&gt;&amp;nbsp;has come from nothing to being one of the most watched projects on GitHub incredibly quickly. and I think that by today everyone knows, and has an opinion, it. Personally, I find it interesting and I really like the simplicity it provides. But what I don't like is that I have to code my application in Javascript in order to use it. I have tried it, and being a .net developer used to Visual Studio with Resharper, I was frustrated every single minute because none of the IDEs I tried (and I think I tried all the major Javascript IDEs) was able to give me IntelliSense good enough to help more than it hurt.&lt;/p&gt;
&lt;p&gt;If you are like me, you probably wish that you can write a Node.js application in C#. Say, for example, that there were a tool that would allow you to write, in a .cs file in Visual Studio, with (working) IntelliSense at every point, a program like this:&lt;/p&gt;
&lt;pre class="brush:csharp"&gt;using NodeJS.HttpModule;

class Program {
    static void Main() {
        Http.CreateServer((req, res) =&amp;gt; {
            res.Write("Hello, world");
            res.End();
        }).Listen(8000);
    }
}
&lt;/pre&gt;
&lt;p&gt;and translate it into something like:&lt;/p&gt;
&lt;pre class="brush:javascript"&gt;var http = require('http');
http.createServer(function(req, res) {
    res.write('Hello, world');
    res.end();
}).listen(8000);
&lt;/pre&gt;
&lt;p&gt;I don't know about such zero-overhead tool, but there is a tool which will transform it to&lt;/p&gt;
&lt;pre class="brush:javascript"&gt;require('mscorlib');
var http = require('http');
////////////////////////////////////////////////////////////////////////////////
// Program
var $$Program = function() {
};
$$Program.$main = function() {
    http.createServer(function(req, res) {
        res.write('Hello, world');
        res.end();
    }).listen(8000);
};
Type.registerClass(null, '$Program', $$Program, Object);
$$Program.$main();
&lt;/pre&gt;
&lt;p&gt;The &lt;a href="http://www.saltarelle-compiler.com" target="_blank"&gt;Saltarelle C# to Javascript compiler&lt;/a&gt;&amp;nbsp;now has support for Node.js. You probably also feel that the new C#5.0 keywords async and await would be a good fit for Node.js programs. So do I, and many of the Node.js APIs have counterparts that return a Task, which can be awaited.&lt;/p&gt;
&lt;h2&gt;A Simple File Browser&lt;/h2&gt;
&lt;p&gt;To demonstrate the capabilities of Saltarelle with Node.js, here is a tutorial wich creates a fully asynchronous (on the server) file browser by writing the source code in C#.&lt;/p&gt;
&lt;h3&gt;Creating the project&lt;/h3&gt;
&lt;p&gt;First we need to create a project for our application. Create a new Visual Studio project of type Console Application and call it SimpleNodeJSFileBrowser. Then open the package manager console and&lt;/p&gt;
&lt;pre class="brush:plain;gutter:false"&gt;&amp;gt; Install-Package Saltarelle.NodeJS&lt;/pre&gt;
&lt;p&gt;Now unload the project and reload it again (we need to do this because of a Visual Studio issue when replacing the standard mscorlib with a custom one), and remove the attributes it complains about when you try to compile. Now, in order to make the project run, and work, when we hit Ctrl+F5 we need to do a few more things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href="http://nodejs.org/download/" target="_blank"&gt;Download&lt;/a&gt; the correct node executable to the project folder.&lt;/li&gt;
&lt;li&gt;The Script generated by Saltarelle requires a module called 'mscorlib' to be included. This module is available with the Saltarelle.Runtime package (which was installed as a dependency when we installed Saltarelle.NodeJS). Therefore, create a directory called node_modules in the project directory. Unload the project from the solution explorer and add the following code to the BeforeBuild target:
&lt;pre class="brush:xml"&gt;  &amp;lt;Target Name="BeforeBuild"&amp;gt;
    &amp;lt;ItemGroup&amp;gt;
      &amp;lt;MscorlibScript Include="$(SolutionDir)packages\**\mscorlib.js"/&amp;gt;
    &amp;lt;/ItemGroup&amp;gt;
    &amp;lt;Copy SourceFiles="@(MscorlibScript)" DestinationFolder="$(ProjectDir)node_modules"/&amp;gt;
  &amp;lt;/Target&amp;gt;
&lt;/pre&gt;
This will ensure that whenever we update the Saltarelle.Runtime package, the correct mscorlib.js file will be copied to our node_modules directory so it can be found when we run our project.&lt;/li&gt;
&lt;li&gt;In the project properties, on the Debug tab enter the following values:
&lt;ul&gt;
&lt;li&gt;Start action: Start external program "C:\Windows\System32\cmd.exe"&lt;/li&gt;
&lt;li&gt;Command line arguments:&amp;nbsp;/K "C:\&amp;lt;path to your project&amp;gt;\node.exe"&amp;nbsp;SimpleNodeJSFileBrowser.js&lt;/li&gt;
&lt;/ul&gt;
The reason to do it like this rather than put node.exe directly in the "Start external program" option is that by doing it in the above way, the console window will prevail in case of an error that terminates the node process. Unfortunately, msbuild properties such as $(ProjectName) do not work in these options.&lt;/li&gt;
&lt;li&gt;Test that everything works by pasting the simple program above into the Program.cs file, hit Ctrl+F5 and navigate to http://localhost:8000. The browser should display "Hello, world".&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;Adding the file browser code&lt;/h3&gt;
&lt;p&gt;Now that we have a working skeleton, it is time for us to create the actual file browser that we want. This code will use the C# 5.0 features 'async' and 'await', so you need Visual Studio 2012 to compile this (the RC works). Now it is time to actually type in our program, line by line (no, you won't do this because you will use editor features, but this is for explanation; Additionally, the syntax highlighter I use does unfortunately not support indenting a whole block of code so you have to pretend that the indentation is correct:&lt;/p&gt;
&lt;pre class="brush:csharp"&gt;using System;
using System.Threading.Tasks;
using NodeJS.FSModule;
using NodeJS.HttpModule;
using NodeJS.PathModule;
using NodeJS.UrlModule;
&lt;/pre&gt;
&lt;p&gt;All the different node modules reside in their own namespaces. Adding a using directive does not cause the module to be require()d, that only happens when you use something from a module.&lt;/p&gt;
&lt;pre class="&amp;quot;brush:csharp;first-line:7"&gt;namespace SimpleNodeJSFileBrowser {
    public class Program {
        public static void Main() {
            var server = Http.CreateServer(async (req, res) =&amp;gt; {
                try {
                    var url = Url.Parse(req.Url);
                    var path = url.Path;
                    if (path.StartsWith("/"))
                        path = path.Substr(1);
                    if (path.EndsWith("/"))
                        path = path.Substr(0, path.Length - 1);
                    
                    var physicalPath = "C:\\" + string.DecodeUri(Path.Normalize(path.Replace("/", Path.Sep)));
                    
                    res.Write("&amp;lt;html&amp;gt;&amp;lt;body&amp;gt;");
&lt;/pre&gt;
&lt;p&gt;This is just some boring code to ensure that the path neither starts nor ends with a slash, and to convert the URL to a physical path. Of course, if you want to browse something that is not C:\, you can change this.&lt;/p&gt;
&lt;pre class="&amp;quot;brush:csharp;first-line:22"&gt;                    string[] files;
                    try {
                        files = await FS.ReaddirTask(physicalPath);
                    }
                    catch (AggregateException ex) {
                        res.Write("Error reading directory:&amp;lt;br&amp;gt;");
                        foreach (var m in ex.InnerExceptions) {
                            res.Write(m.Message + "&amp;lt;br&amp;gt;");
                        }
                        res.Write("&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;");
                        return;
                    }
&lt;/pre&gt;
&lt;p&gt;This is where it gets interesting. The FS.ReaddirTask call will do some magic to transform the call to a Node.js "fs.readdir()". Awaiting the task will return either the list of files, or throw an exception if the node call failed (meaning that the callback was invoked with a non-null error argument). If this happens, we just print the error.&lt;/p&gt;
&lt;pre class="&amp;quot;brush:csharp;first-line:34"&gt;                    if (files.Length == 0) {
                        res.Write("The directory is empty");
                        res.Write("&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;");
                        return;
                    }
                    
                    Task&amp;lt;Stats&amp;gt;[] statTasks = files.Map(f =&amp;gt; FS.StatTask(physicalPath + "\\" + f));
                    try {
                        await Task.WhenAll(statTasks);
                    }
                    catch (AggregateException) {
                    }
&lt;/pre&gt;
&lt;p&gt;This code uses Javascript's Array.map method to start tasks to invoke the node stat() method on each file we previously found. Note that all these tasks will be executed in parallell. Then we wait for all the tasks to finish with an "await Task.WhenAll(...)". This await will throw an AggregateException if any of the involved tasks failed. We ignore this exception because we will check the outcome of each individual task later.&amp;nbsp;&lt;/p&gt;
&lt;pre class="&amp;quot;brush:csharp;first-line:46"&gt;                    res.Write("&amp;lt;ul&amp;gt;");
                    
                    if (path != "") {
                        res.Write("&amp;lt;li&amp;gt;&amp;lt;a href=\"/" + Path.Normalize(path + Path.Sep + "..") + "\"&amp;gt;..&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;");
                    }
                    
                    for (int i = 0; i &amp;lt; files.Length; i++) {
                        res.Write("&amp;lt;li&amp;gt;");
                        if (statTasks[i].IsFaulted) {
                            res.Write(files[i] + ": Error retrieving stats: " + statTasks[i].Exception.InnerExceptions[0].Message);
                        }
                        else {
                            var stats = statTasks[i].Result;
                            if (stats.IsDirectory()) {
                                var newPath = "/" + (path != "" ? path + "/" : "") + files[i];
                                res.Write("&amp;lt;a href=\"" + newPath + "\"&amp;gt;" + files[i] + "&amp;lt;/a&amp;gt;");
                            }
                            else {
                                res.Write(files[i] + ": " + statTasks[i].Result.Size + "B");
                            }
                        }
                        res.Write("&amp;lt;/li&amp;gt;");
                    }
                    
                    res.Write("&amp;lt;/ul&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;");
&lt;/pre&gt;
&lt;p&gt;Again, this code is rather boring. It just loops over each of the tasks and writes different strings depending on whether the task was successful, whether the item is a directory, etc.&lt;/p&gt;
&lt;pre class="&amp;quot;brush:csharp;first-line:71"&gt;                }
                finally {
                    res.End();
                }
            });
            server.Listen(8000);
        }
    }
}
&lt;/pre&gt;
&lt;p&gt;This finally block will ensure that whenever an exception is thrown, the res.end() method will be invoked. It does not matter whether this is due to normal completion, an unhandled exception, or a failing task. Finally (no pun intended), we tell the server to listen to port 8000, just as you would write in Javascript directly.&lt;/p&gt;
&lt;h3&gt;A look at the generated code&lt;/h3&gt;
&lt;p&gt;Warning: This is not for the faint hearted, the generated code can look quite indimidating at first. Bear in mind, though, that we are invoking an asynchronous method, using the result to dispatch a bunch of parallell asynchronous calls and waiting for them all to finish before we process the result. If you did not use Saltarelle, this would get you into the mess of nested callbacks, or using one of the many control flow libraries available, which can also be a pain to debug and step into. I'm not saying the generated code is "better" than the alternatives, but I don't think it's far worse. And the source C# is quite nice.&lt;/p&gt;
&lt;p&gt;The generated file is in the project output directory (by default bin\Debug) and called SimpleNodeJSFileBrowser.js. Open it and investigate. I will not go through the entire contents here, but I will make a few comments:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The compiler knows which modules we require and inserts the appropriate calls to import them.&lt;/li&gt;
&lt;li&gt;The entire body of our async lambda has been replaced with a nested function called $sm. The only real operations performed by the method is to set the initial state and invoke this inner function.&lt;/li&gt;
&lt;li&gt;Whenever there is an await, it is replaced with code like
&lt;pre class="brush:javascript"&gt;$state = NEW_VALUE;
$t1.continueWith($sm);
$doFinally = false;
return;
&lt;/pre&gt;
This code will cause the state machine to continue from its current position as soon as the awaited task finishes. It also ensures that any finally blocks are not run when awaiting a task.&lt;/li&gt;
&lt;li&gt;The FS.ReaddirTask (and FS.StatTask) calls are replaced with code like &lt;span style="font-family: courier new;"&gt;ss.Task.fromNode(fs, 'readdir', physicalPath)&lt;/span&gt;. This is the magic call that will invoke the given method with a set of arguments, and will add a callback that does the right things.&lt;/li&gt;
&lt;li&gt;Code blocks that do not contain any awaits are translated quite literally.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Final Words&lt;/h2&gt;
&lt;p&gt;This code is available in the &lt;a href="https://github.com/erik-kallen/SaltarelleCompilerSamples" target="_blank"&gt;Saltarelle Compiler Samples&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The (C# source) code, although it works, is quite messy. In the &lt;a href="refactoring-the-nodejs-file-system-browser"&gt;next post&lt;/a&gt; we will refactor it so the actual directory lookup will become a Node.js module.&lt;/p&gt;
&lt;p&gt;And, of course, if you prefer nested callbacks over the auto-generated state machine, there is of course metadata for those methods as well, so you can choose the style you prefer.&lt;/p&gt;</description><pubDate>Thu, 11 Oct 2012 09:57:56 GMT</pubDate><guid isPermaLink="true">http://www.erik-kallen.se:80/blog/writing-nodejs-applications-in-csharp</guid></item><item><title>Forum for Saltarelle Compiler users set up</title><link>http://www.erik-kallen.se:80/blog/forum-for-saltarelle-compiler-users-set-up</link><description>&lt;p&gt;Due to public request, I have set up a forum dedicated to users of the &lt;a href="http://www.saltarelle-compiler.com" target="_blank"&gt;Saltarelle C# to Javascript compiler&lt;/a&gt;. It is found at&amp;nbsp;&lt;a href="http://saltarellecompiler.freeforums.org/"&gt;http://saltarellecompiler.freeforums.org/&lt;/a&gt;.&lt;/p&gt;</description><pubDate>Fri, 21 Sep 2012 12:34:00 GMT</pubDate><guid isPermaLink="true">http://www.erik-kallen.se:80/blog/forum-for-saltarelle-compiler-users-set-up</guid></item><item><title>My new favorite MSBuild hack</title><link>http://www.erik-kallen.se:80/blog/my-new-favorite-msbuild-hack</link><description>&lt;p&gt;Every now and then, when building my Visual Studio projects, I run into problems such as auto-generated files not being generated before the compiler is run, or build failing due to locked files. The solution to these problems is often to add&lt;/p&gt;
&lt;pre class="brush:xml"&gt;&amp;lt;UseHostCompilerIfAvailable&amp;gt;false&amp;lt;/UseHostCompilerIfAvailable&amp;gt;&lt;/pre&gt;
&lt;p&gt;to the first PropertyGroup in the .csproj file. This is to Visual Studio what "zoom: 1" is to IE6!&lt;/p&gt;</description><pubDate>Fri, 21 Sep 2012 10:28:00 GMT</pubDate><guid isPermaLink="true">http://www.erik-kallen.se:80/blog/my-new-favorite-msbuild-hack</guid></item><item><title>Mentioned by Miguel de Icaza</title><link>http://www.erik-kallen.se:80/blog/mentioned-by-miguel-de-icaza</link><description>&lt;p&gt;No, this post does not contain anything interesting, it is just intended to boost my ego and tell everyone about it. Just 4 days after officially introducing the Saltarelle compiler, it has been mentioned by Xamarin founder and Mono guru Miguel de Icaza. This project is really getting attention!&lt;/p&gt;</description><pubDate>Fri, 07 Sep 2012 13:45:34 GMT</pubDate><guid isPermaLink="true">http://www.erik-kallen.se:80/blog/mentioned-by-miguel-de-icaza</guid></item><item><title>Aftermath of the compiler introduction</title><link>http://www.erik-kallen.se:80/blog/aftermath-of-the-compiler-introduction</link><description>&lt;p&gt;When I wrote my &lt;a href="http://erik-kallen.se/blog/saltarelle-open-source-c-to-javascript-compiler"&gt;post&lt;/a&gt; introducing&amp;nbsp;the Saltarelle C# to Javascript compiler and submitted it to &lt;a href="http://news.ycombinator.com/item?id=4470055"&gt;Hacker News&lt;/a&gt;, I was a little worried that it would just get lost in the noise and disappear into the void immediately without many people noticing. One can easily say that this was not the case. It got to second place for a few hours and stayed on the first page for many hours. And this newly started blog has now had more than 7,500 unique visitors! It would be an understatement to say that this exceeded my expectations by quite a bit. And it also resulted in quite a few followers to the project on gitub. Thank you everyone for caring!&lt;/p&gt;
&lt;p&gt;It also turned into quite a debate on Hacker News on whether the whole thing is a good or a bad idea. For many projects it is not, if all you want to do is to add some dynamic behaviour to a website, using a compiler like this would be like killing mosquitos with an elephant rifle. For example, the compiler's own &lt;a href="http://www.saltarelle-compiler.com"&gt;website&lt;/a&gt; does not actually use it. That being said, for larger projects, it is my strong belief that a tool like Saltarelle (or Script# or GWT or something similar) will help you achieve a working, maintainable, application with less effort. This shouldn't be too controversial, most programming languages are suitable for certain thing but not for other and Javascript is the exception in that people use it for all tasks ranging from adding fade effect to an ad to writing multi-100-kloc single-page applications.&lt;/p&gt;
&lt;p&gt;I do feel like, however, that some of the arguments against cross-compiling just stem from lack of knowledge about the concept and deserve to be countered by someone who has actually used the technology in question, so here are my answers to a few of them:&lt;/p&gt;
&lt;blockquote&gt;"The very best of luck when it comes to debugging the code when it is running in the browser"&lt;/blockquote&gt;
&lt;p&gt;This argument I do not really understand. As an example, we can take this code (from the features page of the compiler home page):&lt;/p&gt;
&lt;pre class="brush:csharp"&gt;public class C {
	public string Property;
	public List&amp;lt;string&amp;gt; Collection;

	public C() {
		Collection = new List&amp;lt;string&amp;gt;();
	}
}

public class Driver {
	public void Main() {
		var c = new C() { Property = "Property", Collection = { "Value 1", "Value 2", "Value 3" } };
		var sb = new StringBuilder();
		sb.AppendLine("c.Property = " + c.Property);
		sb.AppendLine("c.Collection = " + c.Collection);

		Document.Instance.GetElementById("output").InnerText = sb.ToString();
	}
}&lt;/pre&gt;
&lt;p&gt;This compiles to the JavaScript&lt;/p&gt;
&lt;pre class="brush:javascript"&gt;C = function() {
	this.property = null;
	this.collection = null;
	this.collection = new Array();
};

Driver = function() {
};
Driver.prototype = {
	main: function() {
		var $t1 = new C();
		$t1.property = 'Property';
		$t1.collection.add('Value 1');
		$t1.collection.add('Value 2');
		$t1.collection.add('Value 3');
		var c = $t1;
		var sb = new ss.StringBuilder();
		sb.appendLine('c.Property = ' + c.property);
		sb.appendLine('c.Collection = ' + c.collection);
		(document).getElementById('output').innerText = sb.toString();
	}
};&lt;/pre&gt;
&lt;p&gt;I do not see how this is particularly hard to debug. Of course, if you want to use features that you simply cannot do in JS (yield, goto), the code loses similarity to the C# source, but if this is a concern to you, you can opt not to use those features. Also, usually the translation is even more direct than this, many statements are identical in C# and Javascript, except that the JS version, by default, uses camel-case for all members. Of course, source maps can help, but I honestly don't think it's necessary, and it might also hurt your debugging experience if the problem you are debugging is the result of a leaky abstraction or incorrect metadata in an import library.&lt;/p&gt;
&lt;blockquote&gt;"I fear projects like these tend to attract programmers who know C#, Java but don't quite want to learn javascript and they don't even try to do it once they run out of runway in this tool. And, the code becomes undebuggable, bloated on the browser."&lt;/blockquote&gt;
&lt;p&gt;So what are you trying to say with this? That people should not be allowed to write code to make a website dynamic without fully understanding the Javascript type system (a concept which is probably about as well understood among the crowd as quantum physics). Is it not just good that everyone can be productive in a web environment? Are you fearing for the average code quality on the web? If so, why? Also, I think the code quality on the web is already low enough that it can hardly get worse. Being able to code web but not have to struggle with Javascript&amp;nbsp;intricacies might even evolve the web; organizations debate whether to do their internal tools as client applications or web apps, with Javascript almost always being an argument against doing it on the web. For the debuggability I touched on that above. And who cares if the code is bloated?&lt;/p&gt;
&lt;blockquote&gt;"I don't see how you can really use this without already being very familiar with Javascript"&lt;/blockquote&gt;
&lt;p&gt;This argument does have some validity. If you use a tool like Saltarelle, you will be bitten by leaky abstractions. Not very often, perhaps not even in every project, but it will happen. If I had the choice, I would much rather execute the MSIL directly inside the browser, but unfortunately I do not have that choice. As long as you are working in a web environment you need some basic Javascript skills, but with a tool like Saltarelle there are things you don't have to care about, eg. forgetting to use strict instead of loose equality. This means that instead of everyone having to be a Javascript guru, you can have only one such guru per project, or perhaps even only one in your organization.&lt;/p&gt;
&lt;blockquote&gt;"My largest criticism for projects like this is that I absolutely abhor running into compiler errors"&lt;/blockquote&gt;
&lt;p&gt;(Assuming the OP meant compiler bugs rather than compiler errors)&lt;br /&gt;Running into compiler bugs is always nasty. But this might happen every time you use a compiler. I remember myself having discovered my shared of gcc bugs many years ago, and this is not fun. Does this mean you write all your code in assembler? Saltarelle compiler bugs should be relatively easy to identify, if there is one you can just check the generated script and verify. Hopefully there are not many and those that do exist should be fewer and fewer by the week. You'll just have to ask yourself whether it is likely that you will spend more time tracking down compiler bugs than you gain by being able to use static typechecking and IntelliSense.&lt;/p&gt;
&lt;p&gt;Anyway, thanks everyone for reading and showing interest in the project!&lt;/p&gt;</description><pubDate>Tue, 04 Sep 2012 20:13:16 GMT</pubDate><guid isPermaLink="true">http://www.erik-kallen.se:80/blog/aftermath-of-the-compiler-introduction</guid></item><item><title>Introducing the Saltarelle open source C# to JavaScript compiler.</title><link>http://www.erik-kallen.se:80/blog/saltarelle-open-source-c-to-javascript-compiler</link><description>&lt;p&gt;I have been writing web applications for a really long time. In my early days I remember looking forward to my employer upgrading from IE3 to IE4 so I could use the brand new feature of dynamically adding rows to a table in what was then called DHTML. Then, later, came Remote Scripting, which was Ajax (just 8 years earlier), implemented with IFRAMEs or Java applets. During all these years, it has become apparent to me that Javascript, while excellent for achieving smallish effects like form validation or conditionally showing text, is not well suited for developing large systems. Of course it is possible to create a great large system in Javascript, just as it is possible to build a &lt;a href="http://en.wikipedia.org/wiki/Rangekeeper" target="_blank"&gt;computer that directs ship fire from gears and disks&lt;/a&gt;, but it is my strong belief that it is not the most cost-effective way of doing it if other alternatives are available. Then, in 2007 or so, I found &lt;a href="http://projects.nikhilk.net/ScriptSharp" target="_blank"&gt;Script#&lt;/a&gt;, a project that takes a C# project and generates a Javascript file as output. I have used this extensively over the past few years, and the idea is excellent! It brings all the productivity tools you have in C# to web development. You can use ReSharper refactorings, get (correct) IntelliSense and you won't ever se an "object does not support this property" error message again.&lt;/p&gt;
&lt;p&gt;Unfortunately, though, development of Script# has since then all but ceased, with hardly any compiler features being added over the past 3 or so years. When .net 3.5 was new, not having implicitly typed variables or lambda expressions (or quite a lot of other features) wasn't too bad, but today it feels very old. Once again, server-side programming is an order of magnitude less annoying than client-side programming.&lt;/p&gt;
&lt;p&gt;But no more. About a month ago I released the &lt;a href="http://www.saltarelle-compiler.com" target="_blank"&gt;Saltarelle&lt;/a&gt; compiler. It is intended as an almost drop-in replacement for Script#, but with &lt;a href="http://www.saltarelle-compiler.com/documentation/supported-c-language-features" target="_blank"&gt;almost full support&lt;/a&gt; for C# 4. And it is also &lt;a href="https://github.com/erik-kallen/SaltarelleCompiler" target="_blank"&gt;open source&lt;/a&gt;! Over the past weeks it has matured to the point that it is now stable and ready for public use. There is a &lt;a href="http://www.saltarelle-compiler.com/getting-started" target="_blank"&gt;Getting started guide&lt;/a&gt;&amp;nbsp;for those new to the concept, and also a &lt;a href="http://www.saltarelle-compiler.com/documentation/migrating-from-scriptsharp" target="_blank"&gt;guide for migrating from Script#&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I do not think that this is a tool that is suitable for everything but if you are developing a fat client application in HTML (especially, but not only, if your serverside code is .net) it will make your life easier.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;</description><pubDate>Mon, 03 Sep 2012 09:31:33 GMT</pubDate><guid isPermaLink="true">http://www.erik-kallen.se:80/blog/saltarelle-open-source-c-to-javascript-compiler</guid></item><item><title>Generating typed proxies for your data access layer</title><link>http://www.erik-kallen.se:80/blog/generating-typed-proxies-for-your-data-access-layer</link><description>&lt;p&gt;In the realm of database development, there are two camps. There are the application developers who love their ORMs and prefer to consider the database to be a simple data store. On the other hand there are the DBAs that consider the database to be the application and everything else to just be an appendix, and force all communication between applications and the database to be through stored procedures and views. I am not entering the argument whether one approach is better than the other, they both have pros and cons, but if you have for some reason decided on the DBA model, you are quite likely going to have a lot of code like this:&lt;/p&gt;
&lt;pre class="brush:c#"&gt;using (SqlCommand cmd = CreateCommand()) {
	cmd.CommandText = "dbo.some_proc";
	cmd.CommandType = CommandType.StoredProcedure;
	cmd.CommandTimeout = CommandTimeout;
	SqlParameter p;
	p = cmd.Parameters.Add("@EmployeeID", SqlDbType.Int, 4);
	p.Value = employeeId;
	p = cmd.Parameters.Add("@Title", SqlDbType.NVarChar, 100);
	p.Value = title ?? DBNull.Value;
	p = cmd.Parameters.Add("@HireDate", SqlDbType.DateTime, 8);
	p.Value = (object)hireDate ?? DBNull.Value;
	using (var rdr = cmd.ExecuteReader()) {
		while (rdr.Read()) {
			DoSomething(MapReaderToEntity(rdr));
		}
	}
}
&lt;/pre&gt;
&lt;p&gt;This is an awful lot of code. Additionally, the&amp;nbsp;MapReaderToEntity method will likely contain a lot of magic strings mapping columns to data members (or worse yet, magic numbers relying on column ordinals.&amp;nbsp;Of course, EF and NHibernate both have support for stored procedures for doing specific things like updating an entity, but if I'm going through the complexity of SPs I don't want to go through the complexity of an ORM as well.&lt;/p&gt;
&lt;p&gt;To solve this problem for myself, I created a small library based on T4 that I call SqlProxy, which can be found &lt;a href="https://gist.github.com/3527712"&gt;here&lt;/a&gt;. The thought is that you add the DatabaseProxy.tt and SqlProxy.cs files to your project. Then whenever you save the DatabaseProxy.tt, it will go to your database and generate typed C# classes based on the information it finds.&lt;/p&gt;
&lt;h2&gt;How to use the generated code&lt;/h2&gt;
&lt;p&gt;Usage of the SqlProxy library is very simple:&lt;/p&gt;
&lt;pre class="brush:csharp"&gt;using (var proxy = new DatabaseProxy("server=.; integrated security=true; initial catalog=AdventureWorks")) {
	int employeeId = proxy.QueryHumanResourcesVEmployee(r =&amp;gt; r.FirstName == "Gigi" &amp;amp;&amp;amp; r.MiddleName == "N" &amp;amp;&amp;amp; r.LastName == "Matthew", null).Single().EmployeeID;
	foreach (var row in proxy.UspGetEmployeeManagers(employeeId)) {
		Console.WriteLine(row.FirstName + " " + row.LastName);
	}
}
&lt;/pre&gt;
&lt;p&gt;This example finds a single matching row in a view, then calls a SP with that result and iterates through the result set returned by the SP. Of course, the result set is not buffered anywhere, it is streamed directly from the underlying datareader directly to the consumer. And, since it is all generated code, IntelliSense is fully functional.&lt;/p&gt;
&lt;p&gt;If a stored procedure returns more than one result set, you cannot foreach over it, but you can foreach over the result sets which are named ResultSet1, ResultSet2 and so on. Due to SQLServer and the streaming nature of SqlProxy, you need to enumerate the result sets in order.&amp;nbsp;If the SP returns a result set and also has output variables, you can access the value of the output variables as properties on the result object, but after you do this, all result sets will be closed (again, due to how SQLServer works). If the SP has output parameters but does not return any result sets, the parameters are simple ref parameters to the method.&lt;/p&gt;
&lt;p&gt;In addition to generating proxies for stored procedures and tables/view, the DatabaseProxy class also has a few utility methods, in particular:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;ExecuteTransacted, which will execute a block of code inside a transaction, and can optionally retry the execution if the process was chosen as a deadlock victim (the famous error 1205),&lt;/li&gt;
&lt;li&gt;BeginTransaction, which will return a transaction wrapper, and until this transaction is disposed, all commands issued from this proxy will automatically be enlisted in the transaction. I don't understand why a plain SqlTransaction does not work like this, but it does not.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Customizing the code generation&lt;/h2&gt;
&lt;p&gt;SqlProxy has a few options you can change to control the generated code. All these options are variable definitions in the DatabaseProxy.cs file. Most of them are self-explanatory, but these ones are worth mentioning:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;generateSqlClr: Does magic stuff that makes the generated proxy class being usable in SQLCLR on SQL Server 2005.&lt;/li&gt;
&lt;li&gt;schemaNames: Only include objects from these schemas.&lt;/li&gt;
&lt;li&gt;customExtractions: This dictionary allows you to specify which arguments to use when executing procedures in order to determine the metadata. The key is the procedure name, the value is a list of parameter values.&lt;/li&gt;
&lt;li&gt;namedQueries: This allows you to create named queries which can be used in the same way as stored procedures. In the example, you can write proxy.EmployeesByBirthDate(minDate, maxDate), exactly as if that query were a stored procedure with one result set. Of course, the database is queried to determine the layout of the result sets.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;How it works&lt;/h2&gt;
&lt;p&gt;SQLServer has a lot of metadata information available, you just need to read it. For getting table, view and custom query information, it uses the GetSchemaTable() method of SqlDataReader. For stored procedures, it calls the procedure inside a transaction which is rolled back. If a custom extraction is not specified for a procedure, it also uses SET FMTONLY ON. Note: You need to specify a custom extraction for a procedure (even if all parameters are null) &amp;nbsp;if you use an IF/ELSE to return different result sets, because given the code below:&lt;/p&gt;
&lt;pre class="brush:sql"&gt;CREATE PROCEDURE p(@v int) AS BEGIN
    IF @v &amp;lt; 0
        SELECT 1
    ELSE
        SELECT 0
END
&lt;/pre&gt;
&lt;p&gt;the metadata extraction based on SET FMTONLY ON will think that the procedure returns two result sets.&lt;/p&gt;
&lt;p&gt;On a side note, in SQLServer 2012, SET FMTONLY ON is replaced with a procedure called sp_describe_first_result_set which doesn't have the above problem but, OTOH, is not usable for SPs that return more than one result set. It is left as an exercise to anyone interested in this code to port it to use the new method instead.&lt;/p&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Is this really useful? No doubt, if you're using SPs for communicating with your database. It seems, though, that ORMs are gaining the upper hand in the fight, and I won't recommend you to do one thing or the other, both sides have pros and cons.&lt;/p&gt;</description><pubDate>Thu, 30 Aug 2012 16:26:39 GMT</pubDate><guid isPermaLink="true">http://www.erik-kallen.se:80/blog/generating-typed-proxies-for-your-data-access-layer</guid></item><item><title>Deploying SQLCLR code as part of your build</title><link>http://www.erik-kallen.se:80/blog/deploying-sqlclr-code-as-part-of-your-build</link><description>&lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms345136(v=sql.90).aspx" target="_blank"&gt;SQLCLR&lt;/a&gt; is a relatively little known and used technology that enables you to run .net code in SQL Server. This gives you the option to do simple things like write a regexp_ismatch function, as well as more complex things such as (like I do) use ANTLR inside SQL Server to transform some DSL into SQL statements to be executed and whose results are returned to the client. I'm not debating (at least not now) whether it is a good or a bad thing; like most technologies it has both pros and cons.&lt;/p&gt;
&lt;p&gt;One thing that is, without any question, terrible is its deployment model. From a high level perspective, is sounds neat: you create a .net assembly, annotate some methods with special attributes, compile your assembly, and finally you &lt;em&gt;deploy the compiled assembly to the database&lt;/em&gt;. The only catch in this is the last part,&amp;nbsp;&lt;em&gt;deploy the compiled assembly&lt;/em&gt;. This involves executing the CREATE ASSEMBLY statement (which, additionally, if you only want to supply your ops team with a single SQL file involves hex-encoding the .dll), then you need to execute CREATE FUNCTION and CREATE PROCEDURE (and other) statements to register the assembly members with the database.&lt;/p&gt;
&lt;p&gt;But wait, didn't we annotate the methods with attributes? Yes, we did.&lt;/p&gt;
&lt;p&gt;Aren't those annotations used? Yes, and no. No, it is not used in the sense that just because you put a [SqlFunction] attribute on a method, it does not mean that the method is available for use as a SQL function. But, some metadata is not available in the CREATE statments but are instead read from the attributes. And some of the metadata is available in both the attribute and the CREATE statements and if different values are specified strange things might happen.&lt;/p&gt;
&lt;h2&gt;Visual Studio to the "rescue"&lt;/h2&gt;
&lt;p&gt;Fortunately, Microsoft realized that this model might be an issue and created the SQLCLR project type, available in Visual Studio Ultra Expensive Edition (actually, Professional can open those projects but doesn't give the choice to create them so you have to manually enter a GUID in a .csproj; I wonder if MS' intention was for this to work). Projects of this type can be deployed to a database using just a right click in the Solution Explorer.&lt;/p&gt;
&lt;p&gt;Unfortunately, the deploy functionality seems to be available only from within Visual Studio, not from msbuild. This means that the way Microsoft intended development of this kind of code to be is something close to:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Go along and write your code,&lt;/li&gt;
&lt;li&gt;Enter the connection string to your production server in the project property page in VS,&lt;/li&gt;
&lt;li&gt;Click the Deploy item in the context menu. Can it be more simple?&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;Alternatively, the slightly more sophisticated way:&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;Write your code,&lt;/li&gt;
&lt;li&gt;Deploy the code to your development server,&lt;/li&gt;
&lt;li&gt;Use SQL Server's Generate Script feature to generate the script for all objects from the assembly.&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;Actually, this second approach does allow some scripting since at least the last step is scripable albeit cumbersome. But if you want your build server to generate the deployment script from sources, you're out of luck because of step 2.&lt;/div&gt;
&lt;p&gt;Additionally, for some godforsaken reason SQLCLR projects can only reference other SQLCLR projects so if you want to share a DTO project between your SQLCLR code and your web front-end, you're completely out of luck with this approach. Also, you can only pick your references from the assemblies currenly installed in SQL Server. You cannot (at least not officially) reference a random assembly on disk and accept that you need to deploy this assembly as well.&lt;/p&gt;
&lt;p&gt;In other words, this integration can only be described as&amp;nbsp;&lt;strong&gt;Halfway finished crap, not suitable for professional software development&lt;/strong&gt;.&lt;/p&gt;
&lt;h2&gt;A working solution&lt;/h2&gt;
&lt;p&gt;However, SQLCLR should not completely be discarded as a useful technology just because the Visual Studio tooling is so bad. I'm not claiming it's a good fit for most projects (perhaps not even for mine; but changing it now would be a lot of work), but it certainly has its uses. The deployment model, though, is something that has to be resolved, so I did just that. It only took a 200ish-line PowerShell script, which can be found here:&amp;nbsp;&lt;a href="https://gist.github.com/3437689"&gt;https://gist.github.com/3437689&lt;/a&gt;. You have to get (the excellent) &lt;a href="https://github.com/jbevain/cecil" target="_blank"&gt;Mono.Cecil&lt;/a&gt;&amp;nbsp;and put the dll into the same file as the powershell script and then you can do&lt;/p&gt;
&lt;pre class="brush:powershell"&gt;.\GenerateSqlClrAssembly -File "MySqlClrAssembly"
&lt;/pre&gt;
&lt;p&gt;and it will print the deployment script to the pipeline (stdout).&lt;/p&gt;
&lt;p&gt;This script contains one unofficial feature: If you specify a name with a dot in an attribute, it will be treated as the multipart identifier schema.object_name instead of the single-part identifier [schema.object_name] (which is what Visual Studio does). If you don't want this behavior, modify the Get-Qualified name function.&lt;/p&gt;</description><pubDate>Thu, 23 Aug 2012 15:31:00 GMT</pubDate><guid isPermaLink="true">http://www.erik-kallen.se:80/blog/deploying-sqlclr-code-as-part-of-your-build</guid></item><item><title>The joys of Subversion</title><link>http://www.erik-kallen.se:80/blog/the-joys-of-subversion</link><description>&lt;p&gt;I have been using Git exclusively for so long now that I didn't remember the amount of fun you're up to when you see this message:&lt;/p&gt;
&lt;p&gt;&lt;img src="/Media/Default/BlogPost/blog/svn.png" alt="" width="857" height="501" /&gt;&lt;/p&gt;</description><pubDate>Thu, 23 Aug 2012 11:04:00 GMT</pubDate><guid isPermaLink="true">http://www.erik-kallen.se:80/blog/the-joys-of-subversion</guid></item><item><title>Running QUnit tests in NUnit</title><link>http://www.erik-kallen.se:80/blog/running-qunit-tests-in-nunit</link><description>&lt;p&gt;&lt;a href="http://qunitjs.com" target="_blank"&gt;QUnit&lt;/a&gt; is a great tool for web development. Admittedly, it is not the most impressive piece of software ever written, but it does its job and does so well. Unfortunately, its job is to run a suite of JavaScript tests and display the outcome to the user which is, albeit helpful, not everything you want when it comes to testing. For it to be really useful, you need to run the tests automatically and how to do this in a .net environment is something that Google provides little help on.&lt;/p&gt;
&lt;p&gt;To solve this problem, maybe you have written some custom tool to parse the results? Maybe you use some feature of your CI server? Or perhaps you don't do it at all? Myself, I use the same tool that I use to run all my other tests: NUnit. This means I can user ReSharper to run my tests, no special-casing in the build script, just smooth sailing all the time. And if I use it with this test file:&lt;/p&gt;
&lt;pre class="brush:javascript"&gt;test('Loose_equality_should_consider_string_and_number_equal', function() {
	ok(1 == '1');
});

test('Strict_equality_should_consider_string_and_number_equal', function() {
	ok(1 === '1');
});

test('Browser_should_not_be_MSIE', function() {
	ok(navigator.userAgent.indexOf('MSIE') == -1, 'User-agent string should not contain MSIE');
});
&lt;/pre&gt;
&lt;p&gt;I get output like this:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;a href="http://qunitjs.com"&gt;&lt;img src="http://www.erik-kallen.se/Media/Default/BlogPost/blog/qunit-nunit-results.png" alt="" width="762" height="182" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;And, finally, I don't have to write more C# than this:&amp;nbsp;&lt;/p&gt;
&lt;pre class="brush:c#"&gt;public class MyTest : TestBase {
	protected override string PageAddress {
		get { return "QUnitTests/MyTests.html"; }
	}
}
&lt;/pre&gt;
&lt;h2&gt;How it is done&lt;/h2&gt;
&lt;p&gt;The key to all this is the use of the &lt;a href="http://seleniumhq.org/" target="_blank"&gt;Selenium&lt;/a&gt; browser automation framework, together with the [TestCaseSource] NUnit attribute. The code can be found at&amp;nbsp;&lt;a href="https://gist.github.com/3428959" target="_blank"&gt;https://gist.github.com/3428959&lt;/a&gt;. To use it, simply create a test that derives from TestBase (just like demonstrated above), and override the PageAddress property to point to the relative URL of any QUnit page you want to run (with this approach you need one class for each page, which I think is the best balance between amount of (strictly speaking unnecessary) C# code to write, and output quality.&lt;/p&gt;
&lt;p&gt;The only missing part in this is what to put in the BaseAddress constant in the TestBase class, which needs to be an address where your website is available. This can be as simple as using the address specified in the Web page in the project settings in Visual Studio (although this leaves some to be desired if you want to run on a CI server). For my own purposes, I use an IIS site that my build script sets up automatically, but more on this in a later post (actually I don't even use a constant).&lt;/p&gt;</description><pubDate>Wed, 22 Aug 2012 20:42:00 GMT</pubDate><guid isPermaLink="true">http://www.erik-kallen.se:80/blog/running-qunit-tests-in-nunit</guid></item><item><title>Hello, World</title><link>http://www.erik-kallen.se:80/blog/hello-world</link><description>&lt;p&gt;
	I am very happy to announce that the world is going to become a slightly better place over the following weeks, because I have decided to start blogging. I have been about to do that many times before, but this time I feel the moon is in the right phase. Wish me luck!&lt;/p&gt;
</description><pubDate>Tue, 21 Aug 2012 16:59:57 GMT</pubDate><guid isPermaLink="true">http://www.erik-kallen.se:80/blog/hello-world</guid></item></channel></rss>