Posts

Decorating Unity extension

First I want to give credit where it is due, the code in this post is based largely on the code I found in this post by Jim Chrisopher. The original code required me to register my decorators before registering the type I wanted to decorate.  I didn't like this because I prefer to register all the low-level services from within their own assemblies and then have the app (website etc) decorate those types at a higher level.  So I changed the code in the following ways It now uses the UnityContainer.Configure<> method to decorate types. The decorators may be registered before or after the registered type, or both, it doesn't matter. It is possible to register both generic and non-generic types, and both will be used if applicable (e.g. ICommand<> and ICommand<string> would apply to Command<string> but only ICommand<> would apply to Command<int>.) It works with child containers. It uses the context.NewBuild method instead of requiring an

SpinLock SynchronizationLockException - The calling thread does not hold the lock

Here is the code from a console app. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace ConsoleApplication48 { class Program { static readonly SpinLock SpinLock = new SpinLock(); static void Main( string [] args) { bool lockTaken = false ; try { SpinLock.Enter( ref lockTaken); if (lockTaken) Console.WriteLine( " Lock taken " ); } finally { if (lockTaken) SpinLock.Exit(); } Console.WriteLine( " Done " ); } } } So why would this single-threaded app tell me that the thread trying to call SpinLock.Exit() doesn’t hold the lock? SpinLock is a value type.  When you mark a field referencing a reference type as readonly you are not only making the field unassigna

C# scripting in .NET

Based on a couple of days of experimenting it seems that I now have C# scripting working in my application to a level that I am satisfied.  Because I couldn’t have achieved this without looking at other people’s examples I thought it was only fair that I share what I now have.  Firstly I want to say that my purpose was to give the user the ability to write procedural code rather than object code.  Although it is still possible for the user to write OOP source code my requirement was to let the user define a function which returned a specific return type. public interface ICompiledFunction < T > { T Execute(Dictionary < string , object > variables); } A typical script might look something like this public decimal string Main() { return " Bob Monkhouse " ; } To create an instance of the ICompiledFunction<T> I use ICompilerService, which is defined like so public interface ICompilerService { bool Compile < T > ( string [] scripts,

Why I dislike DLR

It took me hours last night to work out why I was getting a null result from the Value property in the following code when accessed via IronPython public interface ISomeInterface { decimal Value { get ; } } How could a non nullable type possibly return a null (or “None”)?  It turns out that when I set my variable using the .NET scripting API ScriptScope.SetVariable(variableName, (ISomeInterface)value); The scripting engine still works on the implementing object rather than the interface.  This means that if my object has a method “DoSomethingThatScriptingShouldNotHaveAccessTo()” then scripting has access to it! In my case this bit me because I had a Nullable<decimal> property called “Value”, and the Value property implemented explicitly for the interface looked like this decimal ISomeInterface.Value { get { if ( this .Value == null ) CalculateValue(); return this .Value; } } In this case I expected the script to access the variable’s “Value” propert

How many times does a day occur within a date range?

Given two dates I need to know how many times each day of the week occurs within that range, so that I can calculate a value where the factor varies by day.  So I wrote the following code which returns an array of integers, position 0 will tell you how many Sundays there are, position 6 will tell you how many Saturdays, and so on. int [] CountDays(DateTime firstDate, DateTime lastDate) { var totalDays = lastDate.Date.Subtract(firstDate.Date).TotalDays + 1 ; var weeks = ( int )Math.Floor(totalDays / 7 ); var result = Enumerable.Repeat < int > (weeks, 7 ).ToArray(); if (totalDays % 7 != 0 ) { int firstDayOfWeek = ( int )firstDate.DayOfWeek; int lastDayOfWeek = ( int )lastDate.DayOfWeek; if (lastDayOfWeek < firstDayOfWeek) lastDayOfWeek += 7 ; for ( int dayOfWeek = firstDayOfWeek; dayOfWeek <= lastDayOfWeek; dayOfWeek ++ ) result[dayOfWeek % 7 ] ++ ; } return result; }