David Ross's Blog Random thoughts of a coder

WPF, Silverlight and MVVM presentation slide deck

27. May 2011 11:03 by David in

During the week I presented an introduction to WPF & Silverlight using the Model View ViewModel pattern.  The audience was a group of developers that had been working on a .NET 2.0 based application for the last 5 years and were going to transition to .NET 4.0.  Hence the session material was designed to briefly touch on the main WPF concepts and allow them to drill into the topics in more detail at their own leisure.

Enjoy…

WPF & Silverlight Development using Model View ViewModel.pptx (164.07 kb)

Back in Australia

27. May 2011 10:52 by David in

After living in the UK since mid 2008 my wife and I have moved back to Sydney Australia.  We are also bringing back our baby boy Logan who is the reason I haven’t blogged in almost a year.  I’m looking forward to learning about the Open Source, Alt.NET and Agile community in Sydney. 

BattleLang – A Domain Specific Language in .NET – Part III

22. December 2009 21:45 by David in

In this series of blog posts I am exploring the creation of a DSL for a mythical Role Playing Game called “Dragons of Kin” at the Puzzle Guzzler software house.  The language will allow the game designers to interact with the underlying game engine.  All the business logic and heavy lifting is performed by the engine meanwhile the language will be used a conduit into the engine.  Characters, layout and animations will be configured using the language.  In this post I will show the first implementation of the DSL.

In the last post Douglas (the development lead) and Jasmine (the level designer) had created the following example of the first cut of the language they had developed.

 Creature Crocodile {
    Life := random_select [30..40] LP
    Magic := 15 MP
    LifeHealingSpeed   := 3 LP/sec
    MagicHealingSpeed   := 1 MP/min
    Weapons
    {
      Bite:
        Inflicts:= random_select [5..10] LP
      PoisonBite:
        Inflicts:= random_select [5..10] LP + 3 LP/sec
        Costs:= 5 MP
    }
  }

Douglas takes the language fragment to Jimmy a developer on his team.

JimmySo this is the infamous BattleLang
DouglasYep.  As we discussed in the Stand Up.  We have three weeks to implement a text mode engine that will allow Christoph to move from the town to the spring.  There he will kill the crocodiles and save the town.
JimmyEverything hard coded then.  So we will have three Creature types – Jimmy, Crocodile and Boss Crocodile.  Lets not do spawning either.  The Crocodiles will be created as soon as the engine starts.  There really should be an Area type in the language so that Jasmine can assign the Crocs to a location.
DouglasDefinitely.  Lets put that up onto the Product backlog.  So can you do the first cut of the parser over the next couple of days?
JimmyI think so.  I was looking at F# Lex support, ANTLR, Bison but if I only have 2 days I don’t want to learn a new library or toolset.  I’ll just crank out something quickly and we’ll throw it away bit further down the track. It looks pretty easy to parse.
DouglasLets go to the white board and draw out a transition diagram.

image  

Each line represents a token that the scanner is reading from the input.  We start with “Creature” read in the name of the name Creature then we have an open and close bracket and then we reach the end. In this case we are saying its legal to have a Creature with no attributes defined at all.
JimmyAnd what we assume that during each transition all the whitespace is thrown away?  It doesn’t make sense for that to be legal does it?
DouglasFor now lets assume that around every token there is whitespace.  When the code is rewritten we can throw that assumption away.  So the code should actually look like this.
  Creature Crocodile {
    Life := random_select [ 30 .. 40 ] LP
    Magic := 15 MP
    LifeHealingSpeed   := 3 LP / sec
    MagicHealingSpeed   := 1 MP / min
    Weapons
    {
      Bite :
        Inflicts := random_select [ 5 .. 10 ] LP
      PoisonBite :
        Inflicts := random_select [ 5 .. 10 ] LP + 3 LP / sec
        Costs := 5 MP
    }
  }

What is I mean by it being legal is that the checking if the Creature is valid is a different step in the process. 
image 
By shifting the complex validation out of the Syntax Analyser it makes the code a lot simpler. 
JimmyOk.  Let me see so to do the parsing of the character attributes there is another string literal and then we parse the function itself.
 image  

And the function parser is as follows.
image
DouglasI’ll add in the Weapons
 image

 

Building the Proof of Concept BattleLang DSL

As described above there are a couple of stages to process a language:

  • Lexical Analyser which converts the input stream into into Tokens
  • Syntax Analyser which implements the transition diagram
  • Semantic Analyser validates the business rules

 

Lexical Analyser

By simplifying the language definition so that all Tokens are separated by whitespace the scanner is very simple.

public static TokenAndStringIndex GetNextToken(string text, int currentIndex)
{
string token;
if (HaveReachedEndOfInput(currentIndex, text))
return new TokenAndStringIndex("", text.Length);
currentIndex = RemoveWhitespace(currentIndex, text);
currentIndex = GetToken(currentIndex, text, out token);
return new TokenAndStringIndex(token, currentIndex);
}

The TokenAndStringIndex is a tuple that stores the current Index into the array and the Token that was found.  Retrieving a list of all the tokens is a simple matter of looping through the input.  While we could return an IEnumberable<string> in a more complete solution the TokenAndStringIndex would be renamed and extended to to include information such as the line number within the string the token was extract from.  This information is invaluable when it comes to creating useful error messages when problem are found during the Syntax and Semantic Analyser steps.

 

public static IEnumerable<TokenAndStringIndex> GetAllTokens(string text)
{
int currentIndex = 0;
while (currentIndex < text.Length)
{
var result = GetNextToken(text, currentIndex);
currentIndex = result.IndexIntoInputString;
if (!string.IsNullOrEmpty(result.Token))
yield return result;
}
}

 

Syntax Analyser

The syntax analyser eats through the streams of tokens moving through the transition diagram.  On nodes such as the Open Bracket it needs to grab the next token and determine which of the three subsequent nodes should be selected (Close Bracket, retrieve Creature attribute or Enter Weapons scope).  There are lots of different ways to model the nodes and transitions.  One very clean way is to have a class per node and implementing the state pattern.  All classes implement an interface INode and member INode ProcessToken(Token t)

However this time I decided to use an even simpler (although far less scalable) approach.

First three enums were created:

  • Keywords – Include nodes for Creature,  Weapons, Inflicts, Costs,  MagicPoints, LifePoints, Seconds, Minutes, RandomSelect and Colon
  • Operators – Includes nodes for Assign, Add, Subtract, Multiply, Divide, Range
  • Scope – Includes nodes for OpenCreateScope, CloseCreateScope, OpenWeaponScope, CloseWeaponScope, OpenRangeScope, CloseRangeScope

I then created the TokenAction class which links the Token string, to the enum and to a delegate that will manage the transition logic and the retrieval of the information about the Creature.

internal class TokenAction<T>
{
private readonly T _tokenType;
private readonly string _token;
private readonly Action _action;
public TokenAction(T tokenType, string token, Action action)
{
_tokenType = tokenType;
_action = action;
_token = token;
}
public Action Action
{
get { return _action; }
}
public string Token
{
get { return _token; }
}
public T TokenType
{
get { return _tokenType; }
}
}

If we are at Node “Creature” the next token will be the Creature’s name then the following node should be a “{“.  The “{“ node includes its own transition logic that we can access by calling execute on its Action.  Hence:

new TokenAction<KeyWords> (KeyWords.Creature, "Creature", () =>

  {
    _creature.Name = GetLiteral(); 
    TransitionToScopeToken(new[] { Scope.OpenCreateScope });
  }

)

The following code searches for either “MP” or “LP” and sets the _dimension to the required value:

TransitionToKeywordToken(new[] { KeyWords.MagicPoints, KeyWords.LifePoints });

new TokenAction<KeyWords>(KeyWords.MagicPoints, "MP", () => _dimension = DimensionType.Magic)

new TokenAction<KeyWords>(KeyWords.LifePoints, "LP",  () => _dimension = DimensionType.Life)

As you can see the code “collects” values such as the DimesionType as it parsers the tree.  All of the Tokens and their relevant Actions were placed into three symbol tables using the TokenActions class which manages the moving executing of the Actions.

internal class TokenActions<T> 
{
private readonly Dictionary<T, TokenAction<T>> _symbolTable = new Dictionary<T, TokenAction<T>>();
public void Add(TokenAction<T> tokenAction)
{
_symbolTable.Add(tokenAction.TokenType,tokenAction);
}
private TokenAction<T> GetTokenAction(string token)
{
return _symbolTable.Values.First(x => x.Token == token);
}
private TokenAction<T> GetTokenAction(T tokenType)
{
return _symbolTable[tokenType];
}
public void InvokeAction(string token)
{
GetTokenAction(token).Action();
}
public void InvokeAction(T tokenType)
{
GetTokenAction(tokenType).Action();
}
public bool IsTokenFor(string s, T tokenType)
{
return GetTokenAction(tokenType).Token == s;
}
public bool TokenSymbolExists(string token)
{
return _symbolTable.Values.Count(x => x.Token == token) > 0;
}
}

The parser populates the following objects:

  • Unit which models the following “10 MP / sec”
  • UnitsType a collection of Unit objects and automatically reduces “10 MP / sec + 11 MP / sec” to “21 MP / sec”
  • Weapon which includes the damage that is inflicted and the cost to the user
  • Creature which will be passed to the engine – Currently implemented as a fake shell to demonstrate the parser
using System.Collections.Generic;
using System.Linq;
namespace BattleLangParser
{
public enum DimensionType
{
Magic,
Life
}
public enum TimespanType
{
Now,
Second,
Minute
}
public class Unit
{
public float Amount { get; set; }
public DimensionType Dimension { get; set; }
public TimespanType Timespan { get; set; }
}
public class UnitsType
{
private readonly List<Unit> _units = new List<Unit>();
public IList<Unit> Units { get { return _units; } }
public void Add(Unit u, Operator @operator)
{
if(_units.Where(x => x.Dimension == u.Dimension && x.Timespan == u.Timespan).Count()>0)
{
var result = _units.First(x => x.Dimension == u.Dimension && x.Timespan == u.Timespan);
switch(@operator)
{
case Operator.Add:
result.Amount += u.Amount;
break;
case Operator.Subtract:
result.Amount -= u.Amount;
break;
}                
}
else
{
_units.Add(u);
}
}
}
public class Weapon
{
public Weapon()
{
Inflicts = new UnitsType();
Costs = new UnitsType();
}
public UnitsType Inflicts { get; private set; }
public UnitsType Costs { get; private set; }
}
public class Creature
{
public Creature()
{
Stats = new Dictionary<string, UnitsType>();
Weapons = new Dictionary<string, Weapon>();
}
public string Name { get; set; }
public Dictionary<string, UnitsType> Stats { get; private set; }
public Dictionary<string, Weapon> Weapons { get; private set; }
}
}

Semantic Analyser

In this version of the software it does not include a semantic Analyser.

 

 

Unit Tests

An example of one of the Unit tests:

[Test]
public void ParseCreature_MultipleDimensions_EachDifferent()
{
const string emptyCreature = @"
Creature TestMonster
{
RingOfhealing := 3 MP / min + 8 LP / min
}";
var b = new Parser(emptyCreature);
Creature result = b.Parse();
Assert.AreEqual("TestMonster", result.Name);
Assert.IsNotNull(result.Stats["RingOfhealing"]);
Assert.AreEqual(3.0, result.Stats["RingOfhealing"].Units[0].Amount);
Assert.AreEqual(DimensionType.Magic, result.Stats["RingOfhealing"].Units[0].Dimension);
Assert.AreEqual(TimespanType.Minute, result.Stats["RingOfhealing"].Units[0].Timespan);
Assert.AreEqual(8.0, result.Stats["RingOfhealing"].Units[1].Amount);
Assert.AreEqual(DimensionType.Life, result.Stats["RingOfhealing"].Units[1].Dimension);
Assert.AreEqual(TimespanType.Minute, result.Stats["RingOfhealing"].Units[1].Timespan);
}

Verifying the Creature has been created correctly involves:

  • Checking that expected attributes have been loaded
  • Checking that attributes have the correct Units of Measure
  • Providing the user with appropriate error messages

 

Summary

This post has shown how to very easily write a simple DSL (less than 500 lines of code in total) that can be used to populate a relatively complex object. 

There are problems with the approach I’ve taken:

  • The Lexical Analyser was written by hand – There are some great libraries and frameworks that can do the heavy lifting for us on this one
  • Logic for populating the Creature was intermingled with logic for verifying the grammar – An Abstract Syntax Tree and the Visitor pattern is the answer for decoupling those two domains
  • Currently very poor error messages for badly formed user code

 

Source code

BattleLang.SimpleImplementation.7z (91.06 kb)

BattleLang – A Domain Specific Language in .NET – Part II

19. December 2009 15:24 by David in

At Puzzle Guzzler, a small games software company, the development team are starting design work on their new game “Dragons of Kin”.  They are sitting around a table covered with toy dragons, dwarfs and concept drawings. 

The group consists of:

  • Brett the writer – He will be creating the overriding theme for the game, the quests, dialog and boss character personalities
  • Jasmine the environment and level lead – She owns the look of the game and the placement of monsters and items
  • Jen the Animation lead – She owns all CG assets in the game including modelling, lighting and animation
  • Douglas the Engine lead – The programmer geek

 

Brett Just to recap Kin is telling the story of a society falling apart.  Kin was vibrant farming country that stretched from fishing villages in the east, to mines to the north.  There was no King instead each small village was run by a council of elders.  Basically very idyllic with elements of Greek culture but unable to handle a big crisis or invasion. 
Jasmine So no massive cities…  Lots of little towns surrounded by farms fairly clear roads between each area.  The forests will be where all the creatures are then.
Brett Yah that’s right…
Thirty years ago the “Scrolls of Elioch” were discovered and taken to the Mage Guild.  They contained new spells for weather control, a minor fireball and the ability to calm animals.  Its the last one that has caused the trouble.  Relatively quickly the master mages discovered that its a small leap from calming to control.
Now many of the towns folk are slaves to the strongest mage in the area. They use them for their own amusement and live like Gods.  We will get to delve into some pretty dark human vices.  Some will have harems, others will feed off the population to “live forever”.  We will also have a few towns where the local Mage is good but is in constant fear that another stronger Mage will “steal his town”.  So they need to either constantly make potions to give their community immunity or put a minor control spell on everyone.
Jasmine And our hero is?
Brett

Christoph is a member of “Dragons” a resistance movement.  French resistance with fire balls instead of bombs. They go into each town, assassinating the weaker mages, putting potions into the water supply to disrupt the magical control of the towns etc.  The Dragons maybe heroes but since all the militia and armies are controlled by the mages they are public enemy number one.  I’m still working on the back story but his family were refugees fleeing their village. He became separated from the rest of his family and is now trying to find them. 

He has natural immunity to all magic and can’t be controlled but makes his own magical abilities pretty poor.  I think its important that by the end of the game he isn’t walking around with his own slave army.  Kinda defeats the purpose of him being a hero…

Douglas I’m really excited about this game its really going to kick ass.  But we really have to make sure that the coders don’t become a bottleneck like last time.
I want to start working on the AI for fighting.  Do we have an idea yet on what character stats we are going to use?  At least an outline so that I can start creating some of the core classes?
Jen Yah I would like to know as well so that my team can look at mocking up the character setup screens.
Jasmine Steady on.  We aren’t even close to that yet.  Isn’t there a way you can start on the engine without knowing all the details.  It sounds like you want to lock in the design but I think we should delay it as late as possible at least until we have created a completed dungeon and have play tested it for a couple of weeks.
Douglas Umm I’m not sure.  I suppose but I still need a starting point.
Brett How about…  Let me see…
Ok Christoph is with three other members of the resistance.  The town mage is very weak and only just hangs onto power.  But his brother who is now running the town militia has his palace completely locked down.  So the normal dash and stab is out.  The Dragon cleric sends Christoph out of town to find some ingredients for the immunity potion.  Battle, blah, blah hunt/gather blah, blah.   
When he returns to the town there are gallows setup and the Dragon’s have been found guilty of treason.  They will be killed in 2 hours.  Camera zooms in and Christoph’s hands start glowing a large tattoo of a Dragon is seen pulsing on his forearm.  The cleric mouth is moving in the distance but he can hear every word.  “There is a spring above the town go quickly pour the elixir into it.  It runs below the square it might just be enough to save us.”
Later our hero stands looking down on the spring.  There is a large crocodiles chained to an enormous statue. Magical energy vibrates across it.  Of course! This is how the mage controls the town.  Destroy the statue.  Use the elixir and the town will be free.
Douglas Brett you really are full of (*^(*&^
Brett Thanks…  Lets make this interesting. 
There are 10 baby crocs each dealing health damage.  The big one has a poisonous bite.  It drains health away each second after you have been bitten.  By being close to the statue  Christoph is being confounded so he cant move as fast.  He also has a ring on that is slowly healing him ever few seconds.
Douglas Ok so we have a number of effects that are time dependent.  We have effects that are dependent on the distance between objects and of course we have the run of the mill hit points and magic points.
Jen Funny it sounds a lot like a Mel script in Maya.  Some stuff can be done on the timeline and some can be done through triggers.
Douglas Of course, of course that’s all easy to do in Mel as the language is designed around animating.  It knows a lot about polygons, timelines etc.  Maybe we should create a language for “Dragons of Kin”.  At the start it will be very simple and generic just to get us going.  But it will know about spells and damage and weapons. 
Wow that could be great.  If Brett and Jasmine write the story using the language my team can create an Engine that will “play the game”.  Awesome…  And the best bit, my engine and the language and decoupled.  When we start optimising or refactoring it wont break the language as they are completely separate. 
Bring it on…
Jasmine Douglas I don’t want to rain on your parade but I’m not a programmer and sometimes I wonder if Brett can even type.
Douglas Don’t worry about that.  It will be designed for you guys from scratch.  It should be almost identical to how you create the levels in Word now.  But instead of my team having to translate the design into code the document will be the code.

 

BattleLang first draft…

The next day Douglas and Jasmine start on designing the language

Douglas Lets start with the crocodiles setup

  class Crocodile {
    int magicPoints;
    int healthPoints;
    float healthHealingSpeed;
    float magicHealingSpeed;

    Crocodile(…)
  }

Ok so we have this class and stats…
Jasmine Sorry, I thought you said the language would be text like.  Kinda looks like normal code to me.  Why are there int and floats in there.  How do I set the amounts?  What is a class how do I do different types for crocodiles for instance like the boss crocodile?
Douglas Dam your right.  Ok lets start again.  What you need to be able to do is setup the values for the different stats directly in here…  Maybe something closer to Pascal and I’ll kill the semicolon.

  Creature Crocodile {
    Life := 40
    Magic := 50
    LifeHealingSpeed   := 5
    MagicHealingSpeed   := 5
  }
Jasmine That makes more sense.  How fast is the healing speed.  Is it per minute per second.
Douglas Right, maybe we should be using Ada’s and F# Units of Measure. 
Sorry, in a normal programming language everything is a number.  The problem is that it means you can add values that aren’t properly related.  So if you add 3 oranges and 2 apples, instead of getting 5 fruit you get either 5 oranges or 5 apples.  As far as the computer is concerned its just two numbers.  The knowledge about what the number represents in a problem for the programmer.  Units of Measure allows you to stamp each numeric value with the actual item type it represents.  Most examples use gravity, speed, velocity, money that kinda thing.
So…
  Creature Crocodile {
    Life := 40 LP
    Magic := 50 MP
    LifeHealingSpeed   := 1 LP/sec
    MagicHealingSpeed   := 10 MP/min
  }
Jasmine Cool so does that mean we can represent damage this way.
  LizardPoison  := 15 LP + 3 LP/sec
Douglas Wow yes you could.  That is really compact.  Lets extend this to spells
  FireBall :
    Inflicts:= 20 LP
    Costs:= 5 MP
Jasmine I see so the firewall has an amount of damage that inflicts but to use the spell it has a cost of 5.  I suppose all that is really missing is that every crocodile shouldn’t have just 50 LP but it should random from a range of values.
Douglas Ok lets do the Crocodile properly then
  Creature Crocodile {
    Life := random_select [30..40] LP
    Magic := 15 MP
    LifeHealingSpeed   := 3 LP/sec
    MagicHealingSpeed   := 1 MP/min
    Weapons
    {
      Bite:
        Inflicts:= random_select [5..10] LP
      PoisonBite:
        Inflicts:= random_select [5..10] LP + 3 LP/sec
        Costs:= 5 MP
    }
  }

This a good start.  Eventually we will need to extend it to contain all the animation and sounds effects that Jen needs and currently there is no concept of the distance weapons.  Before we go any further I would like to spend a couple of days writing a parser that can load the language into a form an engine can use.

BattleLang – A Domain Specific Language in .NET

18. December 2009 21:56 by David in

Over the next couple of blog posts I will be creating a simple DSL for a Role Playing Game (RPG).   DSLs are often used in the games industry to combat the huge amounts of complex data within AAA titles.  A RPG will often have thousands of different characters spread throughout the environment each having different:

  • Animation cycles and sound effects
  • Characteristics such as hit points and strength
  • Armour and weapons

All of this information eventually gets fed into a C++ engine that actually runs the game.  This means either all of the members of the team need to be professional programmers or the game’s data needs to be managed and manipulated elsewhere.  While a CSV or XML file can provide some assistance eventually the designer will need to be input state based behaviour:

If HiPoints > 20

  Attack closest player

Else

  Run away

This type of logic is extremely difficult to embed into a CSV file.  However it is relatively simple to create a DSL allows the Game Designer to define each creature’s characteristics and the effects of items and spells.

DSL Background - Ubiquitous Language

A core tenet of Domain Driven Design is the Ubiquitous Language where business users and developers, use a shared language and terminology to define each concept, within the domain.  Instead of leaving this information in a document they are embedded into the domain objects within the system that is being built.  By both groups using the same terminology the opportunities for miscommunication are greatly reduced.  Further as the business and developers come to understand and respect the other group’s expertise the language becomes richer and the definition’s gain greater clarity resulting in the the domain objects being updated to replicate the new richer model.

DDD and Acceptance testing are a perfect fit.  By having tests written using terminology from the Ubiquitous Language allows the business to create test cases early in the development process.  These tests can form the basis of the project’s detailed requirements and allows the development team to build the meaty parts of the application extremely quickly and to a very high quality.

Business writing the software

The natural question to ask is “If the business can write the tests can they also write the software”?  The answer is a resounding NO.  Software development is very complex.  Modern generic programming languages are designed to execute an infinite number of different programs.  They do this by having a type system that maps to the architecture of the underlying computer.  We use ints, floats and strings in our programs to represent concepts such as age, weight and names.  Programmers are easily able to do the mental jump between built in types and the abstract concepts that are being simulated by the program and are described in the Ubiquitous Language.  Further developers are able to look at control structures such as for loops and instantly recognise that some action is occurring on each element within an array.  These skills are not universal and without them it is very difficult to understand any non-trivial piece of software.

The problem space that the DSL solves is making a programming language that allows a user to explain a business activity or business data using concepts from the Ubiquitous Language directly.  So instead of having to understand an entire application with five hundred files, tens of thousands of lines of code and concepts such as component layering and patterns.  They can either write or approve a small code fragment that looks surprising close to the Acceptance tests that they may are already actively creating.

Examples of useful DSLs:

DSL Requirements

If we treat our DSL as a tool to allow a business analyst or fairly technical business user to interact with our application we can quickly come up with some high level requirements:

  • The language should use business or problem space terminology – Its pointless creating a DSL that isn’t intermit with the domain
  • The language should include the smallest number of control structures as possible – The more complex the language the more difficult it is to learn and maintain.  If statements and assignment should be adequate for the majority of problems.
  • The DSL user code should be treated as any other development artefact
    • Should be in source control
    • Build server should validate any user generated code on check in – Very important since DSL syntax will change over time hence its vital to know when a script is no longer valid
  • DSL should be very focussed – There is no reason not to have a couple of complementary DSLs on a single project each solving a single problem is a clear and simple manner

Introduction to Immutability in C#

6. November 2009 22:43 by David in

Value Objects from Domain Driven Design, Functional programming and “Many Cores” have made immutability sexy.  However being immutable only means that the code is question is side effect free.  What is more important is how the immutable object will be used.

 

Immutability – I don’t change

In Domain Driven Design Money often given as an example of a Value Object.

public class Money
{
    public decimal Amount { get; set; }
    public string CurrencyCode { get; set; }
}

 

Here the Money class in clearly mutable.  The class includes public methods that can be used by external consumers to modify the Amount and the CurrencyCode. 

var tenDollars = new Money {Amount = 10, CurrencyCode = “AUD”};

tenDollars.Amount = 20; 

 

We can ensure that type is not modifiable by making the the property setters private. 

 

public class Money
{
    public Money(decimal amount, string currencyCode)
    {
        Amount = amount;
        CurrencyCode = currencyCode;
    }

    public decimal Amount { get; private set; }
    public string CurrencyCode { get; private set; }
}

While private setters make the class immutable from external change both Amount and CurrencyCode can both be modified within the class or from a derived class.  Using a read only backing field allows the compiler to enforce the immutability of the type.

public class Money
{
    private readonly decimal _amount;
    private readonly string _currencyCode;

    public Money(decimal amount, string currecyCode)
    {
        _amount = amount;
        _currecyCode = currencyCode;
    }

    public decimal Amount
    {
        get { return _amount; }
    }
    public string CurrencyCode
    {
        get { return _currencyCode; }
    }
}

 

While Value Objects like Money are immutable Domain Entities are not.  Hence the Bank Account or Person’s Wallet Domain Entity will have a public setter on a property of type Money.  Changing the value involves creating a new instance of the Money class.

 

Immutability – I don’t change and am interchangeable

Once an immutable type has been created we need to decide whether instances on the type are interchangeable.  Consider an Audit log entry that is created each time a web page is accessed on a server.  Logically each entry is globally unique since the entry is created for a particular web page request, from a particular IP address at an instant in time.  The Log entry is immutable however  Log Entries are NOT interchangeable. 

Meanwhile Money by its very nature is interchangeable, a ten dollar note has the identical worth to ten one dollar coins.  Further an interchangeable type should be able to be used in the Flyweight Pattern.  Instead of creating a new instance of the Money class each time it is used a cache whose key is based on Amount and CurrencyCode can be used instead.

In C# if a class in Interchangeable it must override the Equals and GetHashCode methods and the ‘==’ an ‘!=’ operators.  Thankfully Resharper will generate this boiler plate code automatically.

public class Money
{
    private readonly decimal _amount;
    private readonly string _currecyCode;

    public Money(decimal amount, string currecyCode)
    {
        _amount = amount;
        _currecyCode = currecyCode;
    }

    public decimal Amount
    {
        get { return _amount; }
    }
    public string CurrencyCode
    {
        get { return _currecyCode; }
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        if (ReferenceEquals(this, obj)) return true;
        if (obj.GetType() != typeof (Money)) return false;
        return Equals((Money) obj);
    }

    public bool Equals(Money other)
    {
        if (ReferenceEquals(null, other)) return false;
        if (ReferenceEquals(this, other)) return true;
        return other._amount == _amount && Equals(other._currecyCode, _currecyCode);
    }

    public override int GetHashCode()
    {
        unchecked
        {
            return (_amount.GetHashCode()*397) ^ (_currecyCode != null ? _currecyCode.GetHashCode() : 0);
        }
    }

    public static bool operator ==(Money left, Money right)
    {
        return Equals(left, right);
    }

    public static bool operator !=(Money left, Money right)
    {
        return !Equals(left, right);
    }
}

 

 

Immutability – I don’t change, am interchangeable and emulate mutable operations

A Money type needs to be able to support simple arithmetic operations such as add and subtract.  As the type is immutable instead of modifying the private data of the class a new new instance is created each time the type is modified.  C# enforces the pattern of creating a new instance of the type during the arthimetic operation by making the operators statically defined.

 

public static Money operator + (Money left, Money right)
{
    if(left._currecyCode==right._currecyCode)
        throw new ArgumentException("Only Money of the same currency can be added");
    return new Money(left._amount + right._amount, left.CurrencyCode);
}

public static Money operator -(Money left, Money right)
{
    if (left._currecyCode == right._currecyCode)
        throw new ArgumentException("Only Money of the same currency can be added");
    return new Money(left._amount - right._amount, left.CurrencyCode);
}

public Money ConvertToCurrency(string newCurrencyCode)
{
  return Money(LookupConversionRate(_amount, _currencyCode, newCurrencyCode), newCurrencyCode);

}

In summary

When creating an immutable type you should consider how that type will be used.  Equality functions and operators should be added if it is interchangeable with other instances and mutable operations should be emulated by creating new instances of the type.

Deployment Verification Test Assertions

8. October 2009 08:22 by David in

Its almost a year since the TDD talk I did last year at Skillsmatter with Chris Roff.  Of the topics we discussed Deployment Verification Tests seemed to be the least understood and 12 months later the majority of projects still don’t have a suite of automated tests to determine if a deployment has been successful.  The following is a summary of a introduction to DVTs that I recently wrote for one of my clients.  Unfortunately there is no code attached.

 

Introduction

Deployment Verification Tests (DVTs) determine if an installation of a component has been successful.  Typically they are executed as a manual set of checklists, by the environment support team, or as a smoke tests by the QA team.  A better approach is to automate the tests and have the development team verify each of the assumptions about the environment that the component is deployed to.

In particular:

  • That all dependent systems (including databases, Web Services, Message Queues, remote file shares etc) can be accessed
  • That all dependent systems have been deployed correctly
    • Has the database upgrade script run correctly/Verify that new the tables, columns or stored procedures exist?
  • That all local resources can be accessed
    • Is the account running the new Windows Service able to write to its log file?

Best Practices

Deployment Verification tests should follow the same practices as unit and integration tests hence they should be:

  • Rerunnable
  • Focussed – Each test should verify a single assumption which quickly helps the investigation process when a test fails
  • Part of the build – By writing the tests in NUnit enables the DVTs to be executed as part of the build process

Unlike the Unit and Integration tests, Deployment Verification Tests are executed in production. 

They must be:

  • Idempotent for Users - Never change customer visible data stores within an environment (i.e. any database records)
  • Clean for administrators - Minimise the impact on production staff visible data stores (i.e. log files)
  • Independent of other tests – They should exist within their own assembly to ensure that Integration or Acceptance tests aren’t accidently executed in production (these tests will often change the contents of the database)

 

Don’t Repeat Yourself – Sharing Configuration

Since DVTs are used to verify that a deployment is successful it is imperative that the test uses the same configuration information as the System Under Test (SUT).  A simple way of achieving this is to have the test extract any environment specific configuration item, such as a connection string, out of the SUTs configuration file.

In the following example the DVT assembly has been deployed into the same folder as the website that is being tested.  The OracleDatabaseAssert class uses an XPath to drill into the web.config to retrieve the connection string. 

   1: [TestFixture]
   2: public class verify_can_connect_to_different_services
   3: {
   4:     [Test]
   5:public void can_connect_to_oracle()
   6:     {
   7:         OracleDatabaseAssert.CanConnectUsingConfiguration
   8:             ("web.config", "/configuration/appSetting[@name='db']/@value", 
   9: "Can not connect to the Oracle database");
  10:     } 
  11: }

 

The test will fail fail when:

  • The web.config is missing the appropriate appSetting tag
  • There is a firewall blocking access to the Oracle database server
  • The user account accessing the database hasn’t been configured

 

Its very simple to use this example to create a set of libraries to cover the most common testing scenarios including:

  • Database Asserts
  • Networking/Firewall Asserts
  • File System Permissions Asserts
  • Web Services Asserts

 

Example Assertion Library - OracleDatabaseAssert

Used to perform a DVT against an Oracle database.
CanConnectUsingConfiguration Used to determine if the connection details can be used to connect to the database
TableExistsUsingConfiguration Used to determine that table exists within a database
ColumnExistsUsingConfiguration Used to determine if a column within a database table exists
StoredProcExistsUsingConfiguration Used to determine if a stored procedure exists in the database

     

    Summary

    With a small amount of effort it is very simple to convert manual smoke tests into automated Deployment Verification Tests.  By implementing the tests using NUnit enables the tests to be easily executed by the environment support team.

    My testing Life Cycle

    30. September 2009 08:16 by David in

    Each time we start a new project I usually have to fill in the Test plan for the development team.  That task involves creating a set of high level goals around quality and having that plan approved by the Test lead.  The following summarises the plan for my current project.

    Testing Stage Description Designed byAudienceTools Environments
    Behaviour Tests

    · Tests used to grow the system’s API

    · Validates developers understanding of the Story being built

    · Scenarios are added to increase understanding of the problem domain

    · Developers

    · BA validating the design

    · NUnit

    · Rhino.Mocks

    · NBehave

    · Build

    Unit Tests

    · Used to verify a component is built correctly

    · Used to test isolated component’s independently

    · Layers are tested independently

    · Dependent layers are mocked out

    · Tests are added to increase code coverage and to validate error paths

    · Developers

    · Other developers

    · NUnit

    · Rhino.Mocks

    · Build

    Integration Tests

    · Used to test the interaction between components and services

    · Interact with external message databases, Web Services and Message queues

    · Tests are added when assumptions about the dependent systems are incorrect

    · Developers

    · Other developers

    · NUnit

    · Build

    Acceptance Tests

    · Used to validate business rules and scenarios

    · End to End testing scenario

    · BAs/Testers

    · Business users

    · Selenium

    · White

    · ALL except production

    Deployment Verification Tests

    · Tests that are run in production

    · Developers and the support team

    · Support team

    · NUnit

    · All

     

    I try to differentiate the look and structure of the code between writing Behaviour, Unit and Integration tests (I find pure BDD syntax throughout the test overly complex and ugly).  As the table shows both the audience and focus of the tests are very different and so the code should look different.  I can be confident to walk through a NBehave test with a BA but explaining the use of Rhino.Mocks within in Unit test is not worth the hassle.

    MPI.Net Slides

    17. July 2009 09:23 by David in

    Here are the slides for the MPI.Net talk that I gave last night at the OpenSource.Net eXchange III.

    Mpi.NetTalk.pptx (510.92 kb)

    Gojko has a summary of the evening here

    Tail End Recursion and F# – Part 2

    9. June 2009 23:03 by David in

    In first post on Tail End Recursion it was shown that recursive functions can be automatically replaced with a fast while loop by the compiler.  The post implies that the head::tail syntax should be used to write F# code instead of using a while loop.  In reality this is often unnecessary due to the pipeline operator |> .  The pipeline operator is used to channel the output of one function into the input parameters of another.  This is similar to a pipe operator in Unix.

    ls | grep “my program” | wc –l

    In F# the operator allows streams to be filtered and enriched.  I have converted the previous code to use the pipeline operator.  The code function coverts a (x,y) tuple into a (x, y, x*x, y*y, x*y) tuple this holds the temporary working data for the program.  The work data is then summed together and finally the linear regression is calculated.

       1: let public regressionUsingPipelineOperator l : linearRegressionCoeffs =
       2:let length = (float) (List.length l)
       3:  
       4:let working s = 
       5:let (x,y) = s
       6:     (x, y , x*x, y*y, x*y)
       7:  
       8:let calc =
       9:     l |> Seq.map working
      10:       |> Seq.fold (fun s acc ->
      11: let (a,b,c,d,e) = s
      12: let (a1,b1,c1,d1,e1) = acc
      13:                     (a + a1, b + b1, c + c1, d + d1, e + e1)
      14:                  ) (0.0, 0.0, 0.0, 0.0, 0.0) 
      15:       |> fun (sumX, sumY, sumXX, sumYY, sumXY)   ->
      16: let meanX = sumX/length
      17: let meanY = sumY/length
      18: let m = (sumXY - length*meanX*meanY) / (sumXX - length* meanX * meanX)
      19: let c = meanY - m*meanX
      20:            {m = m; c=c;}
      21:
      22:   calc

    In the example above the pipeline method is slightly slower than the hand crafted code using the head::tail syntax (since in a single step the code does the equivalent of the map and fold operation).  However the programmer’s intent is far clearer and hence should be used as the default solution.

    Update:

    The code above can be simplified to by reversing the parameters to the Seq.Fold method as follows.

       1: let public regressionUsingPipelineOperator l : linearRegressionCoeffs =
       2:let length = (float) (List.length l)
       3:  
       4:let calc =
       5:     l|> Seq.fold (fun acc (x,y) ->
       6: let (a,b,c,d,e) = acc
       7:                     (a + x, b + y, c + x*x, d + y*y, e + x*y)
       8:                  ) (0.0, 0.0, 0.0, 0.0, 0.0) 
       9:       |> fun (sumX, sumY, sumXX, sumYY, sumXY)   ->
      10: let meanX = sumX/length
      11: let meanY = sumY/length
      12: let m = (sumXY - length*meanX*meanY) / (sumXX - length* meanX * meanX)
      13: let c = meanY - m*meanX
      14:            {m = m; c=c;}
      15:
      16:   calc

    Whoops…  F# code seems to continually shrink.