Getting Started with using StructureMap IoC container

Its been a while since I have had the opportunity to work on the MotorDB project.

But before continue with anything, its time to put in an IoC container that should have done at the beginning.

Because I want to, I am going to use StructureMap.

First we need to clean up the PolicyRepository class to remove the poor man’s dependency injection that was setup using the Lazy<> initializer.

  • Remove the empty constructor
  • Remove the Lazy references

Once completed, the PolicyRepository is now

    public class PolicyController : BaseApiController
    {
        private readonly IPolicyRepository _policyRepository;

        public PolicyController(IPolicyRepository policyRepository)
        {
            _policyRepository = policyRepository;    
        }

        public HttpResponseMessage Get()
        {
            var policyDataToReturn = _policyRepository.Get();
            var response = Request.CreateResponse(HttpStatusCode.OK, policyDataToReturn);
            return response;
        }

        public HttpResponseMessage Get(int id)
        {
            var policyToReturn = _policyRepository.GetPolicyFor(id);
            if ( policyToReturn == null )
                return Request.CreateResponse(HttpStatusCode.NotFound);
            var response = Request.CreateResponse(HttpStatusCode.OK, policyToReturn);
            return response;

        }
    }

StructureMap is really simple to get setup and configured within MVC.  I had StructureMap set up and configured in minutes. This is all that is required

Run NuGet and search for StructureMap. Make sure that you select StructureMap.MVC4

StructureMap-NuGet

Once complete, several new files and folders have been added.

Within the App_Start folder, there is a new file StructuremapMvc.cs

There is a new folder DependencyResolution which contains three files:

  • IoC.cs
  • StructureMapDependencyResolver.cs
  • StructureMapDependencyScope

Two small code changes are required.

Open global.asax.cs, and add StructuremapMvc.Start() to the Application_Start method;

Open IoC.cs, and after Scan lamda, add the following:

x..For<IPolicyRepository>().Use<PolicyRepository>();

Thats it, everything is now ready to go.

However, what we have done is not the best solution. It means that everytime we create a new interface, it is going to have to be added manually. So delete x..For<IPolicyRepository>().Use<PolicyRepository>();, and scan.AssembliesFromApplicationBaseDirectory();

The class is now:

        public static IContainer Initialize() {
            ObjectFactory.Initialize(x =>
                        {
                            x.Scan(scan =>
                                    {
                                        scan.AssembliesFromApplicationBaseDirectory();
                                        scan.TheCallingAssembly();
                                        scan.WithDefaultConventions();
                                    });
                        });
            return ObjectFactory.Container;
        }

Everything runs.

There are no unit tests in this post as we do not need to test StructureMap, as we know that it will return the correct class.

Advertisements

My musing about anything and everything

Tagged with: , ,
Posted in MotorDB, StructureMap

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 12 other followers

%d bloggers like this: