Archive for the ‘c#’ Category

Approval testing – value for the money

Monday, January 16th, 2012

I am a believer in the value of test­ing. How­ever not all tests are equal, and actu­ally not all tests pro­vide value at all. Raise your hand if you’ve ever seen (unit) tests that tested every cor­ner case of triv­ial piece of code that’s used once in a blue moon in an obscure part of the sys­tem. Raise your other hand if that test code was not writ­ten by human but generated.

 

As with any type of code, test code is a lia­bil­ity. It takes time to write it, and then it takes even more time to read it and main­tain it. Con­sid­er­ing time is money, rather then blindly unit test­ing every­thing we need to con­stantly ask our­selves how do we get the best value for the money – what’s the best way to spend time writ­ing code, to write the least amount of it, to best cover the widest range of pos­si­ble fail­ures in the most main­tain­able fashion.

Notice we’re opti­mis­ing quite a few vari­ables here. We don’t want to blindly write plenty of code, we don’t want to write sloppy code, and we want the test code to prop­erly ful­fil its role as our safety net, alarm­ing us early when things are about to go belly up.

Test­ing conventions

What many peo­ple seem to find chal­leng­ing to test is con­ven­tions in their code. When all you have is a ham­mer (unit test­ing) it’s hard to hit a nail, that not only isn’t really a nail, but isn’t really explic­itly there to being with. To make mat­ters worse the com­piler is not going to help you really either. How would it know that Login­Con­troller not imple­ment­ing ICon­troller is a prob­lem? How would it know that the new depen­dency you intro­duced onto the con­troller is not reg­is­tered in your IoC con­tainer? How would it know that the pub­lic method on your NHiber­nate entity needs to be virtual?

 

In some cases the tool you’re using will pro­vide some level of val­i­da­tion itself. NHiber­nate knows the meth­ods ought to be vir­tual and will give you quite good excep­tion mes­sage when you set it up. You can ver­ify that quite eas­ily in a sim­ple test. Not every­thing is so black and white how­ever. One of diag­nos­tics pro­vided by Cas­tle Wind­sor is called “Poten­tially mis­con­fig­ured com­po­nents”. Notice the vague­ness of the first word. They might be mis­con­fig­ured, but not nec­es­sar­ily are – it all depends on how you’re using them and the tool itself can­not know that. How do you test that efficiently?

Enter approval testing

One pos­si­ble solu­tion to that, which we’ve been quite suc­cess­fully using on my cur­rent project is approval test­ing. The con­cept is very sim­ple. You write a test that runs pro­duc­ing an out­put. Then the out­put is reviewed by some­one, and assum­ing it’s cor­rect, it’s marked as approved and com­mit­ted to the VCS repos­i­tory. On sub­se­quent runs the out­put is gen­er­ated again, and com­pared against approved ver­sion. If they are dif­fer­ent the test fails, at which point some­one needs to review the change and either mark the new ver­sion as approved (when the change is legit­i­mate) or fix the code, if the change is a bug.

 

If the expla­na­tion above seems dry and abstract let’s go through an exam­ple. Wind­sor 3 intro­duced way to pro­gram­mat­i­cally access its diag­nos­tics. We can there­fore write a test look­ing through the poten­tially mis­con­fig­ured com­po­nents, so that we get noti­fied if some­thing on the list changes. I’ll be using Approval­Tests library for that.

[Test]
pub­lic void Approved_potentially_misconfigured_components()
{
    var con­tainer = new Wind­sor­Con­tainer();
    container.Install(FromAssembly.Containing<HomeController>());

    var han­dlers = GetPotentiallyMisconfiguredComponents(container);
    var mes­sage = new String­Builder();
    var inspec­tor = new DependencyInspector(message);
    fore­ach (IEx­poseDe­pen­den­cy­Info han­dler in han­dlers)
    {
        handler.ObtainDependencyDetails(inspector);
    }
    Approvals.Approve(message.ToString());
}

pri­vate sta­tic IHan­dler[] GetPotentiallyMisconfiguredComponents(WindsorContainer con­tainer)
{
    var host = container.Kernel.GetSubSystem(SubSystemConstants.DiagnosticsKey) as IDi­ag­nos­tic­sHost;
    var diag­nos­tic = host.GetDiagnostic<IPotentiallyMisconfiguredComponentsDiagnostic>();
    var han­dlers = diagnostic.Inspect();
    return han­dlers;
}

What’s impor­tant here is we’re set­ting up the con­tainer, get­ting the mis­con­fig­ured com­po­nents out of it, pro­duce read­able out­put from the list and pass­ing it down to the approval frame­work to do the rest of the job.

Now if you’ve set up the frame­work to pup-up a diff tool when the approval fails you will be greeted with some­thing like this:

approval_diff

You have all the power of your diff tool to inspect the change. In this case we have one new mis­con­fig­ured com­po­nent (Home­Con­troller) which has a new para­me­ter, appro­pri­ately named miss­ing­Pa­ra­me­ter that the con­tainer doesn’t know how to pro­vide to it. Now you either slap your­self in the fore­head and fix the issue, if that really is an issue, or approve that depen­dency, by copy­ing the diff chunk from the left pane to the right, approved pane. By doing the lat­ter you’re noti­fy­ing the test­ing frame­work and your team­mates that you do know what’s going on and you know it’s not an issue the way things are going to work. Cou­pled with a sen­si­ble com­mit mes­sage explain­ing why you chose to approve this dif­fer­ence you get a pretty good trail of excep­tion to the rule and rea­sons behind them.

 

That’s quite an ele­gant approach to a quite hard prob­lem. We’re using it for quite a few things, and it’s been giv­ing us really good value for lit­tle effort it took to write those tests, and main­tain them as we keep devel­op­ing the app, and the approved files change.

 

So there you have it, a new, use­ful tool in your toolbelt.

Lock-free thread safe initialisation in C#

Wednesday, January 5th, 2011

I used to do quite a lot of con­cur­rent pro­gram­ming early in my career. Recently I've been work­ing again on opti­mis­ing a large piece of code to behave bet­ter when ran in con­cur­rent envi­ron­ment and I must share a secret with you — I had a great fun. It all started with a semi-serious chal­lenge from a friend, and ended up con­sum­ing big part of my Christmas-new year break.

One of the prob­lems I found, was that a group of objects that was being used on mul­ti­ple threads had to be ini­tialised in a thread safe man­ner — that is the ini­tial­i­sa­tion code had to be ran just once, atom­i­cally, on a sin­gle thread. How­ever after­wards it could be safely exe­cuted con­cur­rently on mul­ti­ple threads. This was being accom­plished by using code sim­i­lar to following:

public void Foo GetInitializedFoo()
{
   if(this.fooIsInitialized) return foo;
   lock(lockObject)
   {
      if(this.fooIsInitialized) return foo;

      InitializeFoo();
      return foo;
   }
}

As you can see the code usu­ally works lock-free, and only the first time when foo is not ini­tialised it has to lock to per­form the ini­tial­i­sa­tion algo­rithm. The rea­son that was sub­op­ti­mal is that in heav­ily multi-threaded sce­nar­ios it yielded sub­op­ti­mal per­for­mance when mul­ti­ple threads were try­ing to ini­tialise foo.

Imag­ine there are four threads. First of them suc­ceeds in obtain­ing the lock, and goes ahead to per­form the ini­tial­i­sa­tion, while the other two go into the wait­ing queue and sleep there wait­ing for the lock to be freed. The first thread fin­ishes the ini­tial­i­sa­tion pretty soon, but by the time the first thread wakes up already quite a lot of time is wasted. Then the sec­ond thread obtains the lock, but the remain­ing two, even though the vari­able is already ini­tialised and there­fore safe to be read con­cur­rently, still wait in the lock queue. The three remain­ing threads could exe­cute all at once now, yet they will be woken up and exe­cuted each in turn.

Mad sci­en­tist solution

The prob­lem was that using lock proved to be sub­op­ti­mal for the prob­lem at hand. To try to address this (and to make my inner geek happy) I decided to look at mak­ing this code per­form better.

While .NET frame­work library has lots of syn­chro­ni­sa­tion prim­i­tives none of them really seemed to be any bet­ter suited for the job then good old Mon­i­tor. I didn't want to put wait­ing threads to sleep imme­di­ately, as this is quite an expen­sive oper­a­tion (and so is wak­ing them up back again), and the ini­tial­i­sa­tion would usu­ally be per­formed quite quickly. I also wanted all the wait­ing threads to be able to exe­cute con­cur­rently with no syn­chro­ni­sa­tion after the first one of them fin­ished ini­tial­i­sa­tion. To accom­plish this I came up with the fol­low­ing solution:

public sealed class ThreadSafeInit
{
	// We are free to use negative values here, as ManagedTreadId is guaranteed to be always > 0
	// http://www.netframeworkdev.com/net-base-class-library/threadmanagedthreadid-18626.shtml
	// the ids can be recycled as mentioned, but that does not affect us since at any given point in time
	// there can be no two threads with the same managed id, and that's all we care about
	private const int Initialized = int.MinValue + 1;
	private const int NotInitialized = int.MinValue;
	private int state = NotInitialized;

	public void EndThreadSafeOnceSection()
	{
		if (state == Initialized)
		{
			return;
		}
		if (state == Thread.CurrentThread.ManagedThreadId)
		{
			state = Initialized;
		}
	}

	public bool ExecuteThreadSafeOnce()
	{
		if (state == Initialized)
		{
			return false;
		}
		var inProgressByThisThread = Thread.CurrentThread.ManagedThreadId;
		var preexistingState = Interlocked.CompareExchange(ref state, inProgressByThisThread, NotInitialized);
		if (preexistingState == NotInitialized)
		{
			return true;
		}
		if (preexistingState == Initialized || preexistingState == inProgressByThisThread)
		{
			return false;
		}

		var spinWait = new SpinWait();
		while (state != Initialized)
		{
			spinWait.SpinOnce();
		}
		return false;
	}
}

The Thread­SafeInit class man­ages lock-free access to part of code­base. It has a state int flag which starts off in a non-initialised state. Then as Exe­cuteThread­SafeOnce method, which marks the begin­ning of the ini­tial­i­sa­tion sec­tion is being called, it uses Interlocked.CompareExchange to set the flag in a lock-free thread safe manned to the id of cur­rent thread. The thread that suc­ceeds returns true and is free to go ahead and do the ini­tial­i­sa­tion. Any other thread waits for the first one to fin­ish, but instead of going imme­di­ately to sleep it spins for a while, effec­tively wast­ing cycles, and then once the first thread fin­ishes, and calls End­Thread­SafeOn­ce­Sec­tion method, all the other threads can pro­ceed in parallel.

Usage pat­tern looks some­thing like this:

public void Foo GetInitializedFoo()
{
   if(this.fooIsInitialized) return foo;
   var initialising = false;
   try
   {
      initializing = init.ExecuteThreadSafeOnce();
      if(this.fooIsInitialized) return foo;

      InitializeFoo();
      return foo;
   }
   finally
   {
      if(initialising)
      {
         init.EndThreadSafeOnceSection();
      }
   }
}

Using this code I was able to mea­sure over two-fold improve­ment over the old solu­tion in this part of code. Sure, it's a micro-optimisation and in 99.9% of cases lock would be just fine, but as I said, it was a chal­lenge, and I had a blast work­ing on this, and that's all I care about. Well, not all per­haps. If you see any flaws in this code, ways to improve it, or I just repli­cated some­thing that some­one else did much bet­ter long time ago, let me know.

C# dynamic and generics dispatch

Wednesday, December 15th, 2010

I remem­ber when C# 3 came out, I imme­di­ately started using pretty much all of the new fea­tures in it and I loved it. Imag­ine devel­op­ing with­out var, auto­matic prop­er­ties, lamb­das, exten­sion meth­ods includ­ing those in .Linq namespace.

With C# 4 it's been dif­fer­ent. Even though it's been sev­eral months since I started using Visual Stu­dio 2010 and .NET 4, except for generic vari­ance and occa­sional use of named argu­ments I hardly use any of the new fea­tures. Espe­cially, I couldn't find a good use case for the most hyped new fea­ture — the dynamic keyword.

Until today, that is.

Short intro­duc­tion to the problem

The appli­ca­tion I'm work­ing on right now, has forms (you wouldn't expect that, would you?). Each of them col­lects some data in steps and based on answers from pre­vi­ous step, its own state etc, it decides whether or not it needs to col­lect more info or "does stuff".

So the C# 3 version…

Each form (ques­tion­naire) imple­ments a com­mon inter­face and var­i­ous (mul­ti­ple!) closed ver­sion of IHandleData<TData> inter­face for each type of data it col­lects from the user. The solu­tion is very loosely generic, so the code that wires those things together does not know any details about the types it's work­ing with. All it knows is that it gets two objects — one of them is the data type, and the other imple­ments generic IHan­dle­Data closed over the for­mer type. It then has to some­how invoke the right one (of many) Han­dle method on the first object, pass­ing the sec­ond one as the argument.

To han­dle this, we came up with code resem­bling the fol­low­ing (this code is sim­pli­fied — the actual solu­tion is a tad more elaborate):

var viewType = data.GetType();
var closedType = typeof(IHandleData<>).MakeGenericType(viewType);
var handle = closedType.GetMethod("Handle");
handle.Invoke(questionnaire, new[] { data });

There's quite a lot of cer­e­mony here, to work around the fact that at com­pile time we don't have enough type infor­ma­tion (and that type infor­ma­tion are dif­fer­ent from invo­ca­tion to invo­ca­tion) to sat­isfy the compiler.

Then again in C# 4…

In sit­u­a­tions like this, involv­ing gener­ics and dynamic dis­patch, you can lever­age the new C# 4 capa­bil­i­ties, to make the code much neater and much more concise:

dynamic data = GetData();
dynamic questionaire = GetQuestionaire();
questionaire.Handle(data);

The addi­tional ben­e­fit to vastly improved read­abil­ity of the code is bet­ter per­for­mance. A use­ful thing to keep on the back of your head.

ConcurrentDictionary in .NET 4, not what you would expect

Friday, August 6th, 2010

.NET 4 brings some awe­some sup­port for mul­ti­threaded pro­gram­ming as part of Task Par­al­lel Library, Con­cur­rent col­lec­tions and few other types that live now in the framework.

Not all behav­iour they expose is… not unex­pected thought. What would be the result of the fol­low­ing code:

private static void ConcurrentDictionary()
{
	var dict = new ConcurrentDictionary<int, string>();
	ThreadPool.QueueUserWorkItem(LongGetOrAdd(dict, 1));
	ThreadPool.QueueUserWorkItem(LongGetOrAdd(dict, 1));
}

private static WaitCallback LongGetOrAdd(ConcurrentDictionary<int, string> dict, int index)
{
	return o => dict.GetOrAdd(index,i =>
										{
											Console.WriteLine("Adding!");
											Thread.SpinWait(1000);

											return i.ToString();
										}
					);
}

We call GetOrAdd method from Con­cur­rent­Dic­tionary<, > on two threads try­ing to con­cur­rently obtain an item from the dic­tio­nary, cre­at­ing it using pro­vided del­e­gate in case it’s not yet present in the col­lec­tion. What would be the result of run­ning the code?

Sur­pris­ingly both del­e­gates will be invoked, so that we’ll see the mes­sage “Adding!” being printed twice, not once. This unfor­tu­nately makes the class unus­able for a whole range of sce­nar­ios where you must ensure that cre­ation logic is only ran once. One sce­nario where I wished I could use this class was Cas­tle Dynamic Proxy and its proxy type caching. Cur­rently it uses coarse grained dou­ble checked lock­ing. The code could ben­e­fit from more ele­gant API like the one exposed by Con­cur­rent­Dic­tionary and fine grained locking.

How­ever looks like Con­cur­rent­Dic­tionary is not an option, not unless cou­pled with Lazy<>, which while I sup­pose would work, is far less ele­gant and would not per­form so good, since both classes would use their own, sep­a­rate lock­ing, so we’d end up using two locks instead of one, and hav­ing to go through addi­tional, unnec­es­sary layer of indirection.

So the mes­sage of the day is – new con­cur­rent classes in .NET 4 are awe­some but watch out for issues anyway.

On C# 4, co/contra-variance and generic constraints

Saturday, June 12th, 2010

While try­ing to clean up some code today, I stum­bled upon inter­est­ing result of cer­tain refactoring.

Given the fol­low­ing covari­ant inter­face and its implementation:

 

public interface IReference<out T> { }

public class ComponentReference<T> : IReference<T> { }

and the fol­low­ing usage:

public static IReference<IInterceptor> ForType<T>() where T : IInterceptor

{

    return new ComponentReference<T>();

}

the code won’t com­pile. How­ever, not all is lost – if you cast explic­itly, every­thing will work just fine:

public static IReference<IInterceptor> ForType<T>() where T : IInterceptor

{

    return (IReference<IInterceptor>)new ComponentReference<T>();

}

Thoughts on C# 4.0’ optional parameters

Wednesday, September 2nd, 2009

C# 4.0 is just round the cor­ner and along with it set of nice new addi­tions to the lan­guage, includ­ing optional para­me­ters. There’s been some his­tor­i­cal resis­tance to add this fea­ture to the lan­guage, but here' it is, and I’m glad it’s com­ing, or at least I was.

In few words, optional para­me­ters, have their default value spec­i­fied in the sig­na­ture of the method. You can then skip them when call­ing method, and the method will be called with their default values.

So, what’s the deal?

To sim­plify the cur­rent dis­cus­sion I will refer to the method con­tain­ing default para­me­ters (Foo in this exam­ple) as called method, and to method pro­vid­ing the default value (DateTime.Now get­ter in the exam­ple few para­graphs below) as value provider.

Take a look at the code below. Method Foo has two para­me­ters, but we can call them as if it had none.

defaultParameters_0

Look­ing good so far, right? Let’s take a look at how Main method looks like in Reflector.

defaultArguments_1

As we can see there’s no magic here – sim­ple com­piler trick. Com­piler binds the invo­ca­tion to the method, and them puts the argu­ments for you. The code looks as if you had writ­ten it your­self in ear­lier ver­sion of C#.

Still good, right? So how does exactly the com­piler knows what to put on the call­ing side? Let’s take a look at the com­piled sig­na­ture of the method Foo.

defaultParameters_2

Ha! The val­ues are encoded in Default­Pa­ra­me­ter­ValueAttribute. Do you see the prob­lem here? No? Than let’s try some­thing else. Let’s change the sig­na­ture of the method to take Date­Time instead of int.

defaultParameters_3

Notice we ini­tial­ize the time to default value of DateTime.Now. All good? So let’s com­pile the code, shall we?

defaultParameters_4

Oops.

Turns out we can’t. What seems like a really rea­son­able code is not allowed. Since the default val­ues for argu­ments are being kept in attrib­utes they must be a com­pile time con­stants, which includes prim­i­tive types (numeric types and strings), tokens ( typeof, methodof, which is not exposed in C# though ) and nulls. Pretty dis­ap­point­ing right?  This means no new Foo(), no Foo.Bar is allowed. This dra­mat­i­cally lim­its the range of sce­nar­ios where this fea­ture can be used, as most of the time, you’ll want not null, but some­thing else as your default value, in which case you’ll end up cre­at­ing over­loads anyway.

There are some workarounds, like the one described in this book, but they have their own down­sides, and it’s not always pos­si­ble to use them anyway.

This all makes me think – why did the authors of the lan­guage decided to pro­vide such crip­pled imple­men­ta­tion of the fea­ture? I’m not an expert in this mat­ter, but I found a sim­ple way in which the fea­ture could be imple­mented allow­ing for far greater range of scenarios.

What if…

Let’s re-examine how this fea­ture works.

  • The com­piler puts the default val­ues of the argu­ments in a spe­cial attribute type on the called method signature.
  • On the call­ing side, the com­piler reads the val­ues, and puts these of them that were not pro­vided explicitly.

All the work hap­pens at com­pile time (let’s ignore the dynamic for a moment, we’ll get to that as well) so no addi­tional work at run­time is required. Since the com­piler is very pow­er­ful, why not go one step further.

I think by sim­ply extend­ing this approach the fol­low­ing sce­nar­ios could be enabled.

  • using value returned by sta­tic para­me­ter­less method (this includes sta­tic prop­erty get­ters) as default value.
  • using value returned by instance para­me­ter­less method defined on the same type (or base type of the type) as called method is defined on (this would only be applic­a­ble for instance methods).
  • using default, para­me­ter­less con­struc­tors as default value.
  • or if we wanted to extend it fur­ther: using value return­ing by any vari­ant of the above that does take para­me­ters that are allowed to be put in the attribute (includ­ing both con­stants, and val­ues of other parameters).

Let us exam­ine how this could be (I think) achieved.

The spoon does not exist

What we would need is a way to store infor­ma­tion which method, or con­struc­tor of which type we want to invoke in case no default value is pro­vided. Since tokens are legal in attrib­utes, the exist­ing approach could be extended with some­thing like this:

defaultParameters_5 

We have a way of stor­ing the method. Now, the com­piler could eas­ily retrieve the token and invoke the called method.

  • If value provider is sta­tic there’s no prob­lem – just call it, regard­less of whether called method is an instance or sta­tic method.
  • If value provider is instance method, and called method is instance method as well, invoke the value provider on the instance on which called method is being invoked (hence the require­ment that value provider must be declared on the same type as called method or its base type).
  • If value provider is instance method but called method is sta­tic do not allow (at com­pile time!) this code to com­pile, since there’s no instance to call the value provider on.

When it comes to con­struc­tors it is even sim­pler – since we allow only default con­struc­tor, at com­pile time we would check if the type does indeed have default, acces­si­ble con­struc­tor, and dis­al­low the code to com­pile oth­er­wise. Then when the called method is being invoked, retrieve the type token and call the default con­struc­tor on that type.

What about dynamic code?

I don’t have very inti­mate knowl­edge of dynamic code yet, but I think this could work with dynamic code as well. The com­piler would put all the infor­ma­tion on the call site (new con­cept intro­duced in .NET 4.0) and this would be all we need. In C# 1.0 hav­ing Method­Info of a method you could invoke it. Same hav­ing System.Type it is easy to find and invoke it’s default con­struc­tor using lit­tle bit of reflec­tion. I really see no rea­son why this would not work.

 

What do you think – am I miss­ing part of the pic­ture – is it some­thing ter­ri­bly wrong with my idea that would restrain it from work­ing? Or maybe C# team didn’t really want (as they used to) imple­ment this fea­ture so they pro­vided only the min­i­mal imple­men­ta­tion they needed for other major fea­tures, COM interop specifically.

Tech­no­rati Tags:

Solving a programming puzzle

Monday, August 10th, 2009

My fel­low devlicio.us blog­ger Tim Barcz posted an inter­est­ing prob­lem to solve. I think it's fun, so I decided to give it a try. Now, I don't know enough con­text to solve it for just about any case (and I don't think that's even pos­si­ble), so I made a few assumptions.

  • The strings we're gonna be han­dling are not too long. Oth­er­wise I'd prob­a­bly use a StringReader/Writer.
  • I'm look­ing at min­i­mal solu­tion. YAGNI — I could do some opti­miza­tions prob­a­bly, but I want to keep it sim­ple, because the sam­ple prob­lem (just a hand­ful of char­ac­ters) is simple.
  • Solu­tion is not con­text spe­cific. In other words, if you want to swap char­ac­ters dif­fer­ently based on con­text, it won't do it. Again, I'm keep­ing it simple.
  • I'm using a fac­tory here, to cre­ate the cleaner and feed it with map­ping but based on the needs and tools avail­able, I'd prob­a­bly use an IoC con­tainer to get the instance, and if needed get the map­ping from an exter­nal file, where it's more read­able, and can be eas­ily changed.

Now, here's the code:

public string CleanInput( string input )
{
    StringCleaner cleaner = CleanerFactory.BuildCleaner( input );
    return cleaner.Clean( input );
}

The Clean­er­Fac­tory is not impor­tant. All it does is con­struct an instance of a String­Cleaner and feed it with the map­ping. How­ever, I'll post it for completeness:

public static class CleanerFactory
{
    public static StringCleaner BuildCleaner( string input )
    {
 
        var cleaner = new StringCleaner();
        BuildMapping( cleaner );
        return cleaner;
    }
 
    private static void BuildMapping( StringCleaner cleaner )
    {
        /*
         *  Character         Correct ASCII Value     Bad Values
            Hyphen             45                         8208, 8211, 8212, 8722, 173, 8209, 8259
            Single Quote     39                         96, 8216, 8217, 8242, 769, 768
            Double Quote     34                         8220, 8221, 8243, 12291
            Space             32                         160, 8195, 8194
         */
        cleaner.AddMapping( (char) 45, (char) 8208, (char) 8211, (char) 8212, (char) 8722, (char) 173, (char) 8209, (char) 8259 );
        cleaner.AddMapping( (char) 39, (char) 96, (char) 8216, (char) 8217, (char) 8242, (char) 769, (char) 768 );
        cleaner.AddMapping( (char) 34, (char) 8220, (char) 8221, (char) 8243, (char) 12291 );
        cleaner.AddMapping( (char) 32, (char) 160, (char) 8195, (char) 8194 );
    }
}

The cleaner itself looks like this:

public class StringCleaner
{
        private IDictionary<char,char> mappings = new Dictionary<char, char>();
 
        public string Clean( string input )
        {
            if( string.IsNullOrEmpty( input ) )
            {
                return string.Empty;
            }
 
            var buffer = new StringBuilder( input );
            for( int i = 0; i < buffer.Length; i++ )
            {
                var character = buffer[i];
                char validCharacter;
                if( this.mappings.TryGetValue( character, out validCharacter ) )
                {
                    buffer[ i ] = validCharacter;
                }
            }
 
            return buffer.ToString();
        }
 
        public void AddMapping( char validValue, params char[] invalidValues)
        {
            if( invalidValues == null )
            {
                throw new ArgumentNullException( "invalidValues" );
            }
 
            foreach( var invalidValue in invalidValues )
            {
                this.mappings[ invalidValue ] = validValue;
            }
        }
}

Since strings are immutable, I'm using a String­Builder to iter­ate over all the char­ac­ters in the string, and replace these I want. I'm using Dic­tio­nary for the map­ping, because it has a read­able API, and O(1) search time.

WCF service host builder

Thursday, July 23rd, 2009

When you have a WCF ser­vice that is not a sin­gle­ton, and you want to use non-default con­struc­tor, you enter a world of pain. There's a lot of hoops that you have to go through to plug an IIn­stan­ce­Provider into the ser­vice. Even worse if you have many ser­vices. To alle­vi­ate the pain, I cre­ated a small helper method.

private ServiceHost GetService<TService>( string address, Func<TService> createServiceInstance )
{
    var service = new ServiceHost( typeof( TService ) ); 
 
    var serviceInstanceFactoryBehavior = new ServiceInstanceFactoryBehavior( () => createServiceInstance() );
    var endpoint = service.AddServiceEndpoint( typeof( TService ).GetInterfaces().Single(), this.GetBinding(), address );
    endpoint.Contract.Behaviors.Add( serviceInstanceFactoryBehavior );
    return service;
}
 

It is based on few assumptions:

  • All ser­vice types imple­ment just one con­tract (which I think you should strive for anyway).
  • All ser­vices use the same binding.

Ser­vi­ce­In­stance­Fac­to­ry­Be­hav­ior plugs a cus­tom IIn­stan­ce­Provider into the host, that uses the pro­vided del­e­gate to fab­ri­cate new instances.

As a side­note, Cas­tle WCF inte­gra­tion facil­ity can do this (in a much more flex­i­ble man­ner), but unfor­tu­nately we can't use it.

Tech­no­rati Tags:

Framework Tips XII: Advanced .NET delegates

Saturday, July 18th, 2009

All .NET del­e­gates inherit (indi­rectly) from System.Delegate class (and directly from System.MulticastDelegate) which has a sta­tic Cre­at­eDel­e­gate method. The method has two pow­er­ful char­ac­ter­is­tics that are not widely known.

All del­e­gate types are imple­mented by the run­time. What do I mean by that? Let’s have a look at how any del­e­gate type looks at the IL level:

delegateInIL

All meth­ods are run­time man­aged, mean­ing that, in a sim­i­lar fash­ion you pro­vide imple­men­ta­tion for inter­face meth­ods, run­time pro­vides imple­men­ta­tion for del­e­gate meth­ods. Take a look at the con­struc­tor. Regard­less of del­e­gate type it always has two argu­ments: object (on which a method will be invoked via the del­e­gate) and a han­dle to that method.

While try­ing (with great help of Ken­neth Xu) to find a way of pro­vid­ing prox­y­ing of explic­itly imple­mented inter­face mem­bers in Dynamic Proxy, I saw this as a pos­si­ble way of being able to imple­ment the fea­ture. The prob­lem in this case is, that proxy is an inher­ited class, that has to call a pri­vate method on the base class. So given that Dynamic Proxy works in IL, I tried to call that con­struc­tor in IL pass­ing required para­me­ters, but unsur­pris­ingly, run­time does access checks so what I got instead of work­ing del­e­gate was this:

MethodAccessException

 

 

Ken­neth sug­gested another approach – using Delegate.CreateDelegate, because as it turns out, you can suc­cess­fully use that method to bind del­e­gates to pri­vate meth­ods. That is the first impor­tant char­ac­ter­is­tic that may be a life saver in cer­tain cases.

So, instead of this (which won’t compile):

public override int BarMethod(int num1)
{
    Func<int, int> d = new Func<int, int>(this.DynamicProxy.Program.IFoo.BarMethod);
    return d(num1);
}

You can use this, which will both com­pile and work:

public override int BarMethod(int num1)
{
    Func<int, int> d = Delegate.CreateDelegate(typeof(Func<int, int>), this, barMethodInfo) as Func<int, int>;
    return d(num1);
}

It has a draw­back though – it’s order of mag­ni­tude slower than cre­at­ing the del­e­gate via its con­struc­tor. Solu­tion brings us to the sec­ond char­ac­ter­is­tic I wanted to talk about – open instance methods.

Each instance method at IL level has an addi­tional argu­ment, implic­itly passed this pointer. Armed with this knowl­edge, let’s read what MSDN says about Delegate.CreateDelegate and open instance methods:

If firstAr­gu­ment is a null ref­er­ence and method is an instance method, the result depends on the sig­na­tures of the del­e­gate type type and of method:

  • If the sig­na­ture of type explic­itly includes the hid­den first para­me­ter of method, the del­e­gate is said to rep­re­sent an open instance method. When the del­e­gate is invoked, the first argu­ment in the argu­ment list is passed to the hid­den instance para­me­ter of method.

  • If the sig­na­tures of method and type match (that is, all para­me­ter types are com­pat­i­ble), then the del­e­gate is said to be closed over a null ref­er­ence. Invok­ing the del­e­gate is like call­ing an instance method on a null instance, which is not a par­tic­u­larly use­ful thing to do.

This basi­cally says that we can bind method to del­e­gate that passes the ‘this’ argu­ment explic­itly, so that we can reuse the del­e­gate for mul­ti­ple instances. Here’s how that works:

private static Func<Foo, int, int> d;
 
static Foo()
{
    d = Delegate.CreateDelegate(typeof(Func<Foo, int, int>), barMethodInfo) as Func<Foo, int, int>;
}
 
public override int BarMethod(int num1)
{
    return d(this, num1);
}

With this approach we pay the price of del­e­gate cre­ation only once per life­time of the type.

I was curi­ous how all these approaches would per­form, so I cre­ated a quick poor-man’s benchmark.

I invoked an instance method 100 times using four approaches:

  1. directly
  2. via del­e­gate cre­ated using its constructor
  3. via del­e­gate cre­ated using Cre­at­eDel­e­gate pass­ing first argu­ment explic­itly (not null).
  4. via del­e­gate cre­ated once using Cre­at­eDel­e­gate and then reusing it for sub­se­quent calls.

Here are the results (times are in ticks):

poormansDelegateBenchmark

As you can see, when reusing the instance after 100 calls we can achieve the same per­for­mance as when using the delegate’s con­struc­tor, which, given we can use in broader spec­trum of cases, is pretty amazing.

If you want to run the tests for your­self, the code is here.

Tech­no­rati Tags: ,,,

On self-documenting code

Saturday, March 28th, 2009

Take this piece of code:

public int Count(string text)
{
    Console.WriteLine("Counting...");
    // simulate some lengthy operation...
    Thread.Sleep(5000);
    return text.Length;
}

What’s wrong with it?

It’s not very obvi­ous what the magic num­ber 5000 means? Does it mean 5000 years? 5000 min­utes? 5000 the time it takes to run around Main Mar­ket Square in Kraków?

Sure, you can hover over the invo­ca­tion (if you’re in Visual Stu­dio) and see the tooltip

selfdocumentingcode

and see that the num­ber denotes mil­lisec­onds, but then you (as well as the junior pro­gram­mer who will main­tain the code­base) have to remem­ber what a mil­lisec­ond is, and what it means to wait 5000 milliseconds.

The prob­lem is, that mil­lisec­ond is not the most nat­ural unit of time for humans, so I can assure you that the first thing the afore­men­tioned junior pro­gram­mer will do when he inher­its the code will be “refac­tor­ing” it, to this:

public int Count(string text)
{
    Console.WriteLine("Counting...");
    // simulate some lengthy operation...
    Thread.Sleep(5000);// waits 5 seconds
    return text.Length;
}

What this means, is not that junior devel­oper is not very bright – no. Quite the con­trary – he would be doing the right thing, only the wrong way. He would be increas­ing the read­abil­ity of the code by doc­u­ment­ing it. How­ever, he would not have to do that, if the code was self doc­u­ment­ing.

So how do we fix that? For exam­ple, like this:

public int Count(string text)
{
    Console.WriteLine("Counting...");
    // simulate some lengthy operation...
    Thread.Sleep(TimeSpan.FromSeconds(5));
    return text.Length;
}