Wednesday, 28 October 2009

SOLID principles

The term SOLID is an acronym for:
  • Single Responsibility Principle
    • A class should have one, and only one, reason to change.
    • If a class assumes more than one responsibility, then there will be more than one reason for it to
    • If a class has more then one responsibility, then the responsibilities become coupled.
  • Open-Closed Principle
    • Software entities should be open for extension but closed for modification.
    • You should be able to extend a classes behaviour, without modifying it.
    • The primary mechanisms behind the Open-Closed principle are abstraction (inheritance) and polymorphism.
  • Liskov Substitution Principle
    • Derived classes must be substitutable for their base classes.
    • Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it.
    • A.K.A. Design By Contract.
    • Derived types are completely substitutable for their base types.
  • Interface Segregation Principle
    • Make fine grained interfaces that are client specific.*
    • Clients should not be forced to depend upon interfaces that they do not use.
  • Dependency Inversion Principle
    • Depend on abstractions, not on concretions.
    • Modules that encapsulate high level policy should not depend upon
      modules that implement details.
    • High level modules should not depend upon low level modules. Both should depend upon abstractions.
    • Abstractions should not depend upon details. Details should depend upon abstractions.

* “This principle deals with the disadvantages of “fat” interfaces. Classes that have “fat” interfaces are classes whose interfaces are not cohesive. In other words, the interfaces of the class can be broken up into groups of member functions. Each group serves a different set of clients. Thus some clients use one group of member functions, and other clients use the other groups.”

Tuesday, 27 October 2009

Naming unit tests

In his book The Art of Unit Testing, Roy Osherove suggests the following format for the name of unit tests:


  • MethodUnderTest - The name of the method being tested.
  • Scenario - This part gives us the “with” part of the name: “When I call method X with a null value, then it should do Y.”
  • Behaviour - This part specifies in plain English what the method should do or return, or how it should behave, based on the current scenario: “When I call method X with a null value, then it should do Y.”

Thursday, 22 October 2009

Expression Trees

In LINQ, expression trees represent structured queries that target sources of data that implement IQueryable<T>. Expression trees are also used in LINQ to represent lambda expressions that are assigned to variables of type Expression<TDelegate>.
Instances of Expression<TDelegate> can be executed. To execute an expression tree call the Compile method to create an executable delegate and then call invoke it. Executing an expression tree may return a value, or it may just perform an action such as calling a method.
We can use the Expression Trees to create data store agnostic dynamic query APIs.
The foundation of the Expression Trees in .Net is the System.Linq.Expression namespace. The MSDN documentation states:
“Expression trees represent language-level code in the form of data. The data is stored in a tree-shaped structure. Each node in the expression tree represents an expression, for example a method call or a binary operation such as x < y.”
Expression nodes are encapsulated by instances of the Expression class and its subclasses. To build expression tree nodes you use static factory methods on the Expression classes.
You can also get the compiler to build an expression tree for you from a lambda expression. When you do this the Expression is always of type Expression<TDelegate>. The Expression<TDelegate> type provides the Compile method, that compiles the code represented by the expression tree into an executable delegate.
Expression trees are immutable so if you want to modify an expression tree, you must create a copy with the necessary modifications. You can use an expression tree visitor to traverse the existing expression tree. See for an example on how to do this.
“Only those expression trees that represent functions, namely Expression<TDelegate> and its parent type LambdaExpression, can be compiled into executable code. To execute other types of expression trees, you must first wrap them in a LambdaExpression node. You can obtain such a LambdaExpression by calling the Lambda method and passing the expression tree as the argument.”

Dynamic Queries

For basic information on creating dynamic queries see
For an example of using AND/OR operations see

Tuesday, 20 October 2009

IQueryable and IQueryable<T>

The type returned from a LINQ query will be of type IQueryable. For example, the var in the following query will be an IQueryable:
var query = from u in data.Users 
            where u.LastName == "Smith" 
            select new { u.FirstName, u.LastName}
IQueryable has very few members and has 3 properties of particular interest:
public interface IQueryable : IEnumerable
    Type ElementType { get; }
    Expression Expression { get; }
    IQueryProvider Provider { get; }
An IQueryable has an Expression property. In other words we can get hold of the Expression Tree for the query (note that the Expression type is the base class for the open generic implementations such as Expression<T>). If we can get hold of the Expression Tree we can manipulate it!
Because IQueryable has a GetEnumerator() method you can enumerate the results of the query. 
IQueryable<T> implements IQueryable (which itself implements IEnumerable) and “provides functionality to evaluate queries against a specific data source wherein the type of the data is known”. The MSDN documentation states:
“This interface inherits the IEnumerable<T> interface so that if it represents a query, the results of that query can be enumerated. Enumeration forces the expression tree associated with an IQueryable<T> object to be executed. Queries that do not return enumerable results are executed when the Execute<TResult>(Expression) method is called.
The definition of "executing an expression tree" is specific to a query provider. For example, it may involve translating the expression tree to a query language appropriate for an underlying data source.
The IQueryable<T> interface enables queries to be polymorphic. That is, because a query against an IQueryable data source is represented as an expression tree, it can be executed against different types of data sources.” (my italics)
IQueryable<T> has a stack of members including all the query type operations you would expect in the form of extension methods (e.g.  Distinct, Max, Min, etc). It has the same properties as IQueryable, including the Expression property.

Monday, 19 October 2009

Expression<TDelegate> Class

Documentation is here:
The documentation states:
Represents a strongly typed lambda expression as a data structure in the form of an expression tree. This class cannot be inherited…
“…When a lambda expression is assigned to a variable, field, or parameter whose type is Expression<TDelegate>, the compiler emits instructions to build an expression tree…
…The expression tree is an in-memory data representation of the lambda expression. The expression tree makes the structure of the lambda expression transparent and explicit. You can interact with the data in the expression tree just as you can with any other data structure.
The ability to treat expressions as data structures enables APIs to receive user code in a format that can be inspected, transformed, and processed in a custom manner. For example, the LINQ to SQL data access implementation uses this facility to translate expression trees to Transact-SQL statements that can be evaluated by the database.”


Expression<TDelegate> has 4 properties:
  • Body – gets the body of the lambda expression.
  • NodeType – gets the node type of the expression (an enumeration of 45 different values representing all the different types of expression such as constants, greater than (>), less than (<) etc).
  • Parameters – gets the parameters of the lambda expression.
  • Type - gets the static type of the expression that this Expression represents (i.e. one of the Func types such as Func<T1, T2, TResult>).


There is really only one interesting method on Expression<TDelegate>:
  • Compile - compiles the lambda expression described by the expression tree into executable code.


Expression<TDelegate> is part of the System.Linq.Expressions namespace which contains a number of interesting classes:
Class Description
BinaryExpression Represents an expression that has a binary operator.
ConditionalExpression Represents an expression that has a conditional operator.
ConstantExpression Represents an expression that has a constant value.
ElementInit Represents an initializer for a single element of an IEnumerable collection.
Expression Provides the base class from which the classes that represent expression tree nodes are derived. It also contains static (Shared in Visual Basic) factory methods to create the various node types. This is an abstract class.
Expression<TDelegate> Represents a strongly typed lambda expression as a data structure in the form of an expression tree. This class cannot be inherited.
InvocationExpression Represents an expression that applies a delegate or lambda expression to a list of argument expressions.
LambdaExpression Describes a lambda expression.
ListInitExpression Represents a constructor call that has a collection initializer.
MemberAssignment Represents initializing a field or property of a newly created object.
MemberBinding Provides the base class from which the classes that represent bindings that are used to initialize members of a newly created object derive.
MemberExpression Represents accessing a field or property.
MemberInitExpression Represents calling a constructor and initializing one or more members of the new object.
MemberListBinding Represents initializing the elements of a collection member of a newly created object.
MemberMemberBinding Represents initializing members of a member of a newly created object.
MethodCallExpression Represents calling a method.
NewArrayExpression Represents creating a new array and possibly initializing the elements of the new array.
NewExpression Represents a constructor call.
ParameterExpression Represents a named parameter expression.
TypeBinaryExpression Represents an operation between an expression and a type.
UnaryExpression Represents an expression that has a unary operator.
More to follow on some of these separate types.

ExpressionTreeVisulaizer in VS 2008

To install the ExpressionTreeVisualizer:
  1. Get the C# language samples that include example LINQ projects from
  2. Open and build the ExpressionTreeVisualizer solution.
  3. Copy the generated ExpressionTreeVisualizer.dll file into ..\Program Files\Microsoft Visual Studio 9.0\Common7\Packages\Debugger\Visualizers directory.
If and when you set a breakpoint and hover over an Expression you can click on the magnifying glass icon in the pop-up tooltip to access the Expression Visualizer.

Friday, 16 October 2009

Func and Action

Func(TResult) is a delegate type that encapsulates a method that has no parameters and returns a value of the type specified by the TResult parameter.
MSDN documentation states:
“You can use this delegate to represent a method that can be passed as a parameter without explicitly declaring a custom delegate. The method must correspond to the method signature that is defined by this delegate. This means that the encapsulated method must have no parameters and must return a value.”
The documentation goes on to point out that if you need a delegate type that does not have a return type use Action instead.
There are 4 further delegate types related to Func(TResult):
  • Func(T, TResult)
  • Func(T1, T2, TResult)
  • Func(T1, T2, T3, TResult)
  • Func(T1, T2, T3, T4, TResult)
Each of these encapsulates a method that takes one or more parameters of various types and which returns TResult.
There are equivalent Action types for encapsulating delegates that do not have a return type.
This is all very interesting when you consider you can assign a lambda expression to a Func, e.g.
Func<int, int, int> function = (a,b) => a + b;
…and that you can also create an Expression from a lambda represented as a Func.
Expression<Func<int, int, int>> expression = (a,b) => a + b;
More to follow on Expression.
Friday, 16 October 2009,

A reminder about delegates

A delegate is a reference type used to encapsulate a method with a specific signature and return type. You can encapsulate any matching method in that delegate.

From MSDN documentation:

“A delegate is a type that references a method. Once a delegate is assigned a method, it behaves exactly like that method. The delegate method can be used like any other method, with parameters and a return value…

…Any method that matches the delegate's signature, which consists of the return type and parameters, can be assigned to the delegate. This makes is possible to programmatically change method calls, and also plug new code into existing classes. As long as you know the delegate's signature, you can assign your own delegated method.

This ability to refer to a method as a parameter makes delegates ideal for defining callback methods.”

The documentation goes on to state that delegates have the following properties (and others):

  • Delegates are similar to C++ function pointers, but are type safe.
  • Delegates allow methods to be passed as parameters.
  • Delegates can be used to define callback methods.
  • Delegates can be chained together; for example, multiple methods can be called on a single event.

Delegate creation syntax:

// define the delegate
public delegate void MyDelegate(object sender, EventArgs e);

// The public member variable myDelegate is an instance of a MyDelegate delegate
public MyDelegate myDelegate;

// assign method SomeMethod to myDelegate
myDelegate = SomeMethod(object myObject, new EventArgs());

Note that this removes any previously assigned methods from the delegate and assigns the new one. You can use an alternative syntax to add more than one method to a delegate. You can then execute all methods in one delegate call:

public delegate  void MyDelegate(object sender, EventArgs e);
public MyDelegate myDelegate;
myDelegate += SomeMethod(object myObject, new EventArgs()); 
myDelegate += SomeOtherMethod(object myObject, new EventArgs());
//Call both methods
myDelegate(obj, args);


The event keyword restricts how delegates are used. Events are delegates that:

  • Can only be registered with += and not with the assignment operator (=)
  • Can only be called by methods of the class that defines the event
public delegate  void MyDelegate(object sender, EventArgs e);
public event MyDelegate myDelegate;
myDelegate += SomeMethod(object myObject, new EventArgs()); 
myDelegate += SomeOtherMethod(object myObject, new EventArgs());

// Compilation error here! Can't asign to an event.
myDelegate = YetAnotherMethod(object myObject, new EventArgs());
//Call both methods
myDelegate(obj, args);

Tuesday, 13 October 2009

SQL Server versioning

SQL Server versions are:

Version Number
SQL Server version
SQL Server 2008 RTM
SQL Server 2008 Service Pack 1
SQL Server 2005 RTM
SQL Server 2005 Service Pack 1
SQL Server 2005 Service Pack 2
SQL Server 2005 Service Pack 3
SQL Server 2000 RTM
SQL Server 2000 SP1
SQL Server 2000 SP2
SQL Server 2000 SP3
SQL Server 2000 SP3a
SQL Server 2000 SP4


To find the version of SQL Server try running the following SQL query:

SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY ('productlevel'), SERVERPROPERTY ('edition')

Sunday, 11 October 2009

Why LINQ to SQL sucks

Given the following repository interface how can you create a LINQ to SQL implementation that uses data transfer objects (POCO classes that are agnostic as to the underlying data access technology) when instantiating the repository?
public interface IRepository<T> where T : class 
    void Save(T instance); 

    void Delete(T instance); 

    TResult FindOne<TResult>(ISpecification<T, TResult> specification); 

    IQueryable<TResult> FindAll<TResult>(ISpecification<T, TResult> specification); 

    IQueryable<T> FindAll(); 
Your LINQ to SQL repository implementation might look something like this (I’ve snipped a bunch of code and left one method implemented):
public class Repository<T> : IRepository<T> where T : class 
    private readonly DataContext _ctx; 

    public Repository(DataContext ctx) 
        _ctx = ctx; 

    public void Save(T instance) ... 
    public void Delete(T instance) ...  
    public TResult FindOne<TResult>(ISpecification<T, TResult> specification) ... 
    public IQueryable<TResult> FindAll<TResult>(ISpecification<T, TResult> specification) ...

    public IQueryable<T> FindAll() 
        return _ctx.GetTable<T>().AsQueryable(); 
The problem is that the call to _ctx.GetTable<T>() expects T to be a type defined in the LINQ to SQL layer – not a data transfer type. 
Let’s say you have a User data transfer class. You would expect to instantiate the repository as follows:
var repository = new Repository<User>();
This won’t work because the User type would have to be the User type defined in the LINQ to SQL layer. Your client code will no longer be agnostic as to the data access layer.
How rubbish is that!
There are some very hacky workarounds that all involve writing lots of unnecessary code.
Sunday, 11 October 2009

Friday, 9 October 2009

Queuing theory

Little’s law

In a stable system the average amount of time it takes to get something through a process is equal to the number of things in the process divided by the average completion rate.

Cycle Time = (Things in Process)/(Average Completion Rate)

To reduce cycle time either:

  1. Get things done faster (usually involves spending money)
  2. Reduce the number of things in process

Reducing Cycle Time

Queuing theory offers some more suggestions on reducing cycle time:

  1. Even out the arrival of work
  2. Minimise the number of things in process
  3. Minimise the size of things in process
  4. Establish a regular cadence
  5. Limit work capacity
  6. Use pull scheduling

On evening out the arrival of work

“Queues at the beginning of the development process may seem like a good place to hold work so that it can be released to the development organisation at an even pace. But those queues should be no bigger than necessary to even out the arrival of work…”

On minimising the number of things in process

“…Queues of work waiting for approval absorb energy every time they are estimated, reprioritised, and discussed at meetings. To-do queues often serve as buffers that insulate developers from customers; they can be abused to obscure reality and they often generate unrealistic expectations.”

On limiting work to capacity

“Time sometimes seems to be elastic in a development organisation. People can and do work overtime, and when this happens in short bursts they can even accomplish more this way. However, sustained overtime is not sustainable. People get tired and careless at the end of a long day, and more often than not, working long hours will slow things down rather than speed things up.”

Avoid the thrashing that occurs when there is too much work for the number of people available.

Some final thoughts

Some general rules when using queues:

  1. Queues must be kept short – perhaps two cycles of work.
  2. Managers can reorganise or change items at any time that they are in a queue. But once teams start to work on an item, they should not interfere.
  3. Teams pull work from a queue at a regular cadence until that work is done. It is the pull system that keeps the team busy at all times while limiting work to capacity.
  4. Queues should not be used to mislead people into thinking that their requests are going to be dealt with if the team does not have the capacity to respond.

See Implementing Lean Software Development: From Concept to Cash.

Thursday, 8 October 2009

Data file sizes in SQL Server 2005

If you want to list all the data and log files plus their sizes try the following SQL:

USE master ; 

SELECT  db.dbid AS 'DB ID', AS 'Database Name', AS 'Logical Name', 
        af.[size] as 'File Size (in 8-kilobyte (KB) pages)', 
        (((CAST(af.[size] AS DECIMAL(18, 4)) * 8192) / 1024) / 1024) AS 'File Size (MB)', 
        ((((CAST(af.[size] AS DECIMAL(18, 4)) * 8192) / 1024) / 1024) 
          / 1024 ) AS 'File Size (GB)', 
        af.filename AS 'Physical Name' 
FROM    sys.sysdatabases db 
        INNER JOIN sys.sysaltfiles af ON db.dbid = af.dbid 
WHERE   [fileid] IN (1, 2);

Wednesday, 7 October 2009

The IQueryable<T> interface

The MSDN documentation states that it:

“Provides functionality to evaluate queries against a specific data source wherein the type of the data is known…

The IQueryable<(Of <(T>)>) interface is intended for implementation by query providers.

This interface inherits the IEnumerable<(Of <(T>)>) interface so that if it represents a query, the results of that query can be enumerated. Enumeration forces the expression tree associated with an IQueryable<(Of <(T>)>) object to be executed. Queries that do not return enumerable results are executed when the Execute<(Of <(TResult>)>)(Expression) method is called.

The definition of "executing an expression tree" is specific to a query provider. For example, it may involve translating the expression tree to a query language appropriate for an underlying data source.

The IQueryable<(Of <(T>)>) interface enables queries to be polymorphic. That is, because a query against an IQueryable data source is represented as an expression tree, it can be executed against different types of data sources.”

IQueryable<T> appears to be an excellent choice as a return type for repository methods. Because it is the enumeration that forces the execution of the expression tree the actual database query is deferred until that point. By passing IQueryable<T> around we can add to the query at any point and only execute it when the result is enumerated.

However, there is a down side. If we return an IQueryable<T> we are not really returning the data but rather a query that can be used to get the data. This is not what repositories are intended to do – the repository should execute the query and return the data, not return queries which can be used to get the data at some indeterminate point.

I think the jury is still out on this one.

Tuesday, 6 October 2009

Adding time to dates in SQL Server

To add time to a date use the DATEADD Transact-SQL statement.

DATEADD (datepart , number, date )

The date parts can be:

datepart Abbreviations


yy, yyyy


qq, q


mm, m


dy, y


dd, d


wk, ww


dw, w




mi, n


ss, s







For example:

SELECT DATEADD(dd, 12, "29 March 1964")

To get the date at midnight you can use the following:

SELECT DATEADD(dd,0, DATEDIFF(dd,0, GETDATE())) as date_at_midnight

See for details.

Gallio test automation platform

Gallio is a neutral test automation platform designed to help run tests from a variety of testing frameworks in a unified environment. It is a project that seems to be a spin off from the MbUnit team but will run tests from a variety of other frameworks including NUnit.

The Gallio Icarus test runner is somewhat similar to the test runner you get with Resharper. There is also a command line tool called Echo that has support for generating test reports etc.

Gallio also integrates with CruiseControl.Net. You need to setup your build to export a report in Gallio XML format (e.g. run Gallio.Echo.exe as a build task) and update the <publishers> node in ccnet.config. There is then a new report plug-in to be added to the CCNet dashboard to display the Gallio reports.

Sunday, 4 October 2009

NHibernate and quoted identifiers

If you have a column or table name that is a reserved word or which contains spaces etc. you can force NHibernate to use quoted identifiers by using a back tick in the table/column name:

<class name="LineItem" table="`Line Item`">
    <id name="Id" column="`Item Id`"/><generator class="assigned"/></id>
    <property name="ItemNumber" column="`Item #`"/>

Friday, 2 October 2009

Querying the results of a stored procedure

If you want to run a query against the results of a stored procedure a simple solution is to use a temporary table and the “INSERT INTO” SQL syntax. For example:

     some_field varchar(10),
     some_other_field varchar(40)

     EXEC my_stored_procedure

SELECT * FROM #MyTempTable WHERE some_field LIKE '%some value%'

Lean development – eliminate waste

One of the principles of lean software development is to eliminate waste. Anything that not adding value to the customer is a candidate to be considered as waste.

In terms of software development wasteful activities can include:

  • Writing unnecessary code and features
  • Delays in the development process
  • Unclear or ambiguous requirements
  • Unnecessary bureaucracy (this could include estimating)

Other candidates for waste are:

  • Implementing features not actually used by the customer
  • Waiting (e.g. for another team to complete something)
  • Bugs and low quality software

There are 3 terms that are often applied to waste in a system:

  • Muda – an activity that is wasteful or unproductive
  • Mura – unevenness or inconsistency
  • Muri - overburden

Mary Poppendiek states the following:

“The first step in lean thinking is to understand what value is and what activities and resources are absolutely necessary to create that value. Once this is understood, everything else is waste.”

And this:

“All lean thinking starts with a re-examination of what waste is and an aggressive campaign to eliminate it. Quite simply, anything you do that does not add value from the customer perspective is waste. The seven wastes of software development are:

  • Partially Done Work (the “inventory” of a development process
  • Extra Processes (easy to find in documentation-centric development)
  • Extra Features (develop only what customers want right now)
  • Task Switching (everyone should do one thing at a time)
  • Waiting (for instructions, for information)
  • Handoffs (tons of tacit knowledge gets lost)
  • Defects (at least defects that are not quickly caught by a test)”