Injection IoC

Injection Overview

 After much time spent manually injecting code, a conversation I had led to an increased interest in Dependency Injection Inversion of Control Containers. I thought there was no better way to fully understand IoC containers than to build my own and try and make it match the performance of others.

And it’s not too shabby either. Some benchmarking shows that Injection is faster than a lot of those out there, but still only around halfway in the speed table (Based on this).

Injection is a lightweight dependency injection container. Injection allows for the configuration of Type Mappings, Lifetime management of mappings and resolution of instantiated objects.

Injection: How To

So let’s get started. First off, Install Injection through NuGet.

Congratulations

Injection is now ready to use.

Register a Type Mapping

Injection can be told to register a type mapping using the following;

Injection will default to a Transient lifecycle, unless told otherwise.

Anonymous

IInjectionContainer c = new Container();
c.Register<IVehicle, Boat>();

Named

IInjectionContainer c = new Container();
c.Register<IVehicle, Boat>("Boat");

Resolve Anonymous

To resolve a type mapping;

IInjectionContainer c = new Container();
IVehicle vehicle = c.Resolve<IVehicle>();

Resolve Named

To resolve a named type mapping;

IInjectionContainer c = new Container();
IVehicle vehicle = c.Resolve<IVehicle>("Boat");

Register Singleton

To register a Type mapping as a singleton lifecycle, use one of the following for anonymous and named respectively;

IVehicle vehicle = c.Register<IVehicle, Boat>().Singleton();
IVehicle vehicle = c.Register<IVehicle, Boat>("Boat").Singleton();

Assign Constructor Arguments

When registering a Type Mapping, you can declare constructor parameters on the object using the following approach;

IInjectionContainer c = new Container();

c.Register<IBuilding, Pub>("NamedPub")
    .HasConstructArgs(() => new Pub("Pub Name"));

Any resolution of the named registration “NamedPub” will be called with these arguments.

A registration can be made Singleton with Constructor Arguments.

Remove Named Registration

Type Mappings can be removed using the following method;

container.Remove<IBuilding>("NamedPub");

Remove Anonymous Registration

Anonymous Registrations can be removed in the following way;

Note -> If there are multiple named registrations of the same type, but no anonymous registration, the first named registration will be removed instead

container.Remove<IBuilding>();

Automatic Resolution

Injection will automatically resolve dependencies on types that no constructor is provided for, but will not automatically assign values to other required parameters.

For example, the following will allow for Automatic Resolution;

container.Register<IAmmo, Bullet>();
container.Register<IWeapon, Gun>();

public class Bullet : IAmmo
{
    public Bullet(){}
}

public class Gun : IWeapon
{
    public IAmmo ammunition {get; set;}

    public Gun(IAmmo ammo)
    {
        ammunition = ammo;
    }
}

However, should Gun require an argument that is not of a registered type, construction will fail;

//Resolution of IWeapon will fail as 'dmg' is not supplied
container.Register<IAmmo, Bullet>();
container.Register<IWeapon, Gun>();

public class Bullet : IAmmo
{
    public Bullet(){}
}

public class Gun : IWeapon
{
    public IAmmo ammunition {get; set;}
    public int damage {get; set;}

    public Gun(IAmmo ammo, int dmg)
    {
        ammunition = ammo;
        damage = dmg;
    }
}

Inversion of Control in MVC

Injection plays very nice with MVC for automated Inversion of Control.

To find out how to setup Injection within MVC, read this article.

Exceptions

Should Injection come across an issue, it will throw a InjectionException.

The error message returned will explain what went wrong.

More To Come.

In the future I aim to improve the performance of Injection as a whole, more as a learning exercise than a necessity.

I may also look to work towards a MVC specific version of Injection, ready to plug and play, automatically completing the required setup for MVC.

I may also look into introducing Interception into Injection.

As Always; If you have any ideas, you can get in touch with me here.

Version History

Version 1.0.0 -> Initial Release