The code for this step is located on github.

As noted in the introduction, we are going to take this in steps and go in a test first manner.  So, let’s write our first test.

public class when_resolving_a_type_with_zero_dependencies : ContainerSpecBase
{
    static object _result;

    Because of = () =>
        _result = _container.Resolve(typeof(DummyService));

    It should_not_return_null = () =>
        _result.ShouldNotBeNull();

    It should_return_an_instance_of_the_requested_type = () =>
        _result.ShouldBeOfType();

    private class DummyService { }
}

If you haven’t used Machine.Specifications, this may seem like odd syntax.  But what I like about it is that it is easy to see what is going on when the container is resolving a type with zero dependencies.  First,  the container shouldn’t return null and second, the container should return an instance of the requested type.

Now that we have a test, we are going to start coding the implementation.  We’ll start with the simplest thing that could possibly work here.

public class Container
{
    public object Resolve(Type type)
    {
        return Activator.CreateInstance(type);
    }

    public T Resolve()
    {
        return (T)Resolve(typeof(T));
    }
}

Nothing special, and nothing that you couldn’t have figured out on your own.  In the next post, we’ll see how to handle this when the requested type has dependencies.

As I have moved into a consulting role, I have become more and more surprised with the number of .NET developers who have not heard of Dependency Injection or Inversion of Control.  Talking to different people about the concept is met with a few reactions.  While I can’t do anything about the people who simply don’t care, there are two groups of people that I can help.

I began giving talks entitled “Build Your Own IoC Container” at local events here in Dallas.  This isn’t because I’m a proponent of actually building your own and using it production, but rather as a way to educate the two different groups of people.  (Just to reiterate; please don’t build your own for production.)

The first group are those who simply don’t know about IoC.  The talk (and this series) simply start with a blank slate and begin building in a test-driven(sort of) manner.  I’ll use Machine.Specifications as it makes clearer the intent (plus, I just really like it).  I’ve found that by removing the “black-box”, there is more understanding and adoption.

The second group are those who already know about how containers work, but don’t know how to explain them to others.  I was once in this place and needed to figure out how to talk about it with someone who didn’t know.  So, I did what I always do when I don’t understand something; I build it.  This helped me understand enough of the insides to answer the more difficult questions.  Hopefully, by building one together, you and I will gain a greater understanding of the basics of a container.

At the end of the series, we’ll have a fully functional container that supports pluggable lifetimes, multiple registrations for a type, factories, and cyclic dependencies.  There are 218 lines and 1 file.