You don't always need an IOC library

Thursday, 08 Oct, 2015


The principle of YAGNI should always apply, and I was recently reminded of that when I had to build a small application to give to a user for a small one off task. We're talking a single screen application with a single button on it. Idiot proof.

So, File > New > Winforms application - doesn't have to be anything fancy. Then I caught myself: My first instinct was to add StructureMap to it.

I thought to myself that it's only going to have a few classes, I mean just because it's a small winforms app doesn't mean I'm going to shit up the code behind with business logic and data access, so why bother with the overhead of adding a IOC library?

It doesn't mean I don't have to use dependency injection. Mark Seemann calls this "Poor Man's DI".

static class Program
    static void Main()

        var service = new Service(new Database(), new Other());
        Application.Run(new Main(service));

It's short and sweet, and there is no IOC container configuration to worry about. Because I don't need to.

What about when....

Requirements change. "Can it just do this as well...?" More dependencies required, perhaps another form, maybe another service or two. I think I'd see how far I could push Poor Man's DI before I brought in a proper IOC container to help manage things.

Configuring SignalR in StructureMap

Thursday, 04 Jun, 2015


Configuring SignalR in ASP.NET MVC, using StructureMap as the IoC container is fairly straightforward, but not without some subtleties that caught me out.

For the purposes of this post, I'm going to assume that you are familiar with both SignalR and StructureMap, and that you know how to configure them in an ASP.NET MVC application. I will also assume that through some google-fu you have seen the Dependency Injection in SignalR guidance, and have worked through it and got to the "Using IoC Containers in SignalR" section.

I would assume, although I've not tested it, that much of this could also be applied to a self-hosted SignalR server.

Library versions used

This post is based on:

Follow the guidance up to the section on using Ninject, at which point we now want to configure StructureMap.

Replace the SignalR Dependency Resolver

The implementation is nearly identical, with some obvious StructureMap specific differences:

public class StructureMapSignalRDependencyResolver : DefaultDependencyResolver
    private readonly IContainer _container;

    public StructureMapSignalRDependencyResolver(IContainer container)
        _container = container;

    public override object GetService(Type serviceType)
        return _container.TryGetInstance(serviceType) ?? base.GetService(serviceType);

    public override IEnumerable<object> GetServices(Type serviceType)
        var objects = _container.GetAllInstances(serviceType).Cast<object>();
        return objects.Concat(base.GetServices(serviceType));

The behaviour is fairly similar. TryGetInstance will attempt to resolve the type, and if it doesn't know about it, will return null, in which case we call the base resolver, which does.

Register this with StructureMap:


In your Startup, where you configure SignalR, we need to use this new resolver implementation:

var resolver = DependencyResolver.Current.GetService<Microsoft.AspNet.SignalR.IDependencyResolver>();

var hubConfiguration = new HubConfiguration
    Resolver = resolver

/* other options as required */

Here, we are using the MVC DependencyResolver, which has already been replaced by StructureMap thanks to StructureMap.MVC5, to resolve an instance of the SignalR dependency resolver we've registered, which we then tell SignalR to use with a hub configuration object.

Now we just need to configure the StructureMap registry, and teach it how to resolve IHubConnectionContext<dynamic>:

    .Is(ctx => ctx.GetInstance<IDependencyResolver>()

As in the guidance, we want the StockTicker instance to be a singleton, and we have specify how to resolve the IHubConnectionContext<dynamic> which the StockTicker requires. In the Is, I'm using the context to resolve the default SignalR connection manager we've registered. This isn't in the guidance, but I couldn't get it work without doing this.

If anyone has comments/improvements on this, I'd love to hear them.

MSBuild Code Analysis (on a build server)

Friday, 17 Apr, 2015


It is possible to setup your build server to run code analysis on your solution/projects, without having to install VS on your build server.

The answer is here:

I'm not going to reproduce the code here, there is no point. This post is just a reminder to myself as to where I found the solution to this.

Gitlab and Active Directory

Wednesday, 15 Apr, 2015


Gitlab is an open source 'clone' of Github, although I don't think it's much of a clone anymore, to be fair. The Enterprise edition has full Active Directory support, and the Community edition does not.

The integration with Active Directory is of great benefit. To get the most out of it, I think that you would need to upgrade to the Enterprise edition, as it adds a number of additonal integrations which look to be really usefull for larger organisations with multiple projects and development teams.

It is possible to configure the Community edition to integrate with Active Directory, to the extent that you can restrict exactly who is allowed to login in. You have to manually configure the users permissions on groups/projects though. In the Enterprise edition, the tighter integration means that you can match up Active Directory groups with groups in Gitlab, and control access both to the server, and groups/projects, all through Active Directory.

Here is the ldap section from my gitlab.yml (please be aware, I'm no Active Directory expert):

enabled: true
      label: 'domain'
      host: ''
      port: 389 
      uid: 'sAMAccountName'
      method: 'plain' 
      bind_dn: 'CN=Git Lab,OU=AB,OU=example,DC=example,DC=com'
      password: 'secret'
      allow_username_or_email_login: true
      active_directory: true
      base: 'OU=example,DC=example,DC=com'
      user_filter: '(memberOf=cn=devs,ou=groups,dc=example,dc=com)'

The bind_dn is for a user called 'Git Lab', and you must suppoly the password. This user was expressly setup just for this, but you can use any user. The base is the level of Active Directory that Gitlab searches for users from. Finally, the user_filter states that users must members of devs in order to be able to login.

ASP.Net Bootstrap 3 form control width

Thursday, 19 Mar, 2015


Having just been stumped on this for longer than I care to admit, and only finding the answer to my problem buried in a comment on StackOverflow, I'm posting it here for my own future reference:

Q: Using ASP.Net MVC with Bootstrap 3, why won't my <input> stretch to the full width of a <div class="form-group">, even though I am putting form-control on the css class?

A: Because Microsoft override input, select and textarea to have max-width: 280px in the default Site.css which is added to new projects. Removing this will allow the 100% width from form-control.