Monday, 9 December 2013

Avoid installutil when creating .net services

There is a perfectly good solution for installing services:
sc create
No need to inherit from the install util specific classes in your .net code. The problem with installutil is that it's not appropriate when automating install with a local service account. You also get a lot of extra code very tied to installutil.

Sunday, 27 October 2013

Writing rubyish c# or porting ruby code to c#

Since I liked the ruby version for parsing phone numbers, I ported it to c#. In order to avoid having to rewrite to much I did the following:

  1. To begin with, the entire project was using ruby name convention. This simplifies things.
  2. Whenever there is a missing method where the behavior of the method is not the same as the c# one, I added an extension method. For instance I implemented a variant of gsub. The behavior of this gsub uses $1 for matching groups (since it's more natural to remove a bit of code than make a full port).
  3. I tried to translate different ruby constructs into c#. For instance the lazy pattern found in the library @variable ||= init_variable I had to replace with _variable ?? (_variable=InitVariable())
  4. After all the tests became green and I had checked in the initial version, it was quite simple to change name convention using resharper.
The finished version is available on github.

Sunday, 20 October 2013

Using values in expression

I've done some small tests while writing the library to handle readonly objects in c#. Turns out that getting the value of a sub expression can be costly (since you have to compile the expression and execute). When just setting one property there can be a performance benefit of using the following syntax:
new MyClass(1, "2").With(m => m.MyProperty, 3)
There might be some way of optimizing the code in order to get better performance for the form:
new MyClass(1, "2").With(m => m.MyProperty == 3)
If there are only a few usages then the performance hit is probably negligible.

Friday, 18 October 2013

Using readonly objects in c#

One thing I like about f# is the ability to use readonly objects in a way that does not require a lot of code. I've done a small lib inspired by this ability of f#: with

The syntax is a bit special since I assume that the properties have a private set.

myobject.With(m => m.Property1 == 3 && m.Property2 == "3")

Monday, 14 October 2013

Rake:ish make clone in c#

If you prefer to write your build tasks in c# then there is a package for you:
Shake

Sunday, 13 October 2013

Writing build scripts in c#

I went to leetspeak yesterday. Justin Rusbatch had a talk about scriptcs. In that talk he mentioned that several people wanted to write build scripts in c# and thought that it would be nice to use scriptcs to do that. I started trying out to write build scripts in c#. Turns out that many of the classes that are available in .net are not supposed to be used by users of .net. It's cleaner and simpler to execute the command line tools from c#. I started writing some code to try it out. I called it shake.