Iris Classon
Iris Classon - In Love with Code

A simple C# and F# example with IoC and unit tests

Last week I had the pleasure of attending the Öredev conference as a speaker again and I had a great time. Not only did I get to see old friends, but also make new ones, and learn new things. I did end up doing some things I didn’t expect, such as box, dance tango, go slack lining (balance on a line), acrobatic yoga and some other things, but I’ll save that for a separate blog post. One thing that I did learn that is a bit more tech related, was F# and very gentle introduction to machine learning- which definitive is something I want to spend more time learning. A new friend I made at Öredev was Phil Trelford, who is an F# MVP. I think he spent a good 11 hour plus in the lobby teaching me F# and with some great guidance and some of his F# frameworks I under his guidance put together a very simple C# and F# example using F# for tests and for IoC container, and C# to wire it all up. I thought why not share it, so here it is :)

phil_teachin_fsharp

I quite like the compactness of F# but struggle to read it as I’m so used to C#, it takes a few extra seconds to ‘get it’, but that goes for any new languages and thankfully F# shares quite a few similarities with C#.

So this is a very simple example, but I’ll find the time later this or next week to explain some cool stuff I learned such as providers and active patterns + +.

I recommend taking a look at Phil’s blog, I just saw that he had written up a nice blog post about his time at Öredev and our little ‘hackaton’ in the lobby (and the slack lining; I tried very hard to make him try but never succeeded).

Download the F# and C# sample here – please note that the two frameworks used are made by Phil Trelford so share the love when using them :)

C# - Main

[sourcecode language=“csharp”]
namespace MyHouse
{
using MiniIoc;
using System;
public interface IAnimal
{
string MakeSound();
}

public class Cat : IAnimal  
{  
    public string MakeSound()  
    {  
        \_service.Poop();  
        return "Mjau!";  
    }  

    private readonly ILitterTrayService \_service;  
    public Cat(ILitterTrayService tray)  
    {  
        \_service = tray;  
    }  
}  

public interface ILitterTrayService  
{  
    void Poop();  
}  

public class LitterTrayService : ILitterTrayService  
{  
    private int \_poopCount;  
    public void Poop()  
    {  
        if (\_poopCount >= 1)  
        {  
            throw new OverflowException();  
        }  
        ++\_poopCount;  
    }  
}  

class Program  
{  
    static void Main(string[] args)  
    {  
        var container = new Container();  
        container.Register<IAnimal>(typeof(Cat),Lifetime.Transient);  

        var foo = container.Resolve<IAnimal>();  

        foo.MakeSound();  
    }  
}  

}

[/sourcecode]

Tests in F#

[sourcecode language=“csharp”]
module Main.Test

open NUnit.Framework
open Foq
open MyHouse
open System

[]
let When cat makes sound it says mjau()=
let cat = Cat(mock());
let sound = cat.MakeSound();
Assert.AreEqual(“Mjau!",sound)

[]
let When cat makes sound it poops()=
let service = mock()
let cat = Cat(service)
let sound = cat.MakeSound()
verify <@ service.Poop() @> once

[]
let When cat poops twice littertray overflows()=
let cat = Cat(LitterTrayService())
let _ = cat.MakeSound()
Assert.Throws(fun () ->
cat.MakeSound() |> ignore
) |> ignore

[/sourcecode]

Comments

Leave a comment below, or by email.


Last modified on 2013-11-17

comments powered by Disqus