Monday, July 6, 2009

IoC and Structure Map

I started playing with StructureMap a couple of days ago. It is a pretty decent framework for IoC and dependency injection. It works well with ASP.NET MVC and makes it possible to write some good, testable code. That's not to say that there isn't anything better out there, just that this gets the job done well and so I didn't need to look for anything better. The features that I like most are the registry configuration system, and the dependency injection approach.

One way to configure StructureMap is to subclass the Registry class. Methods in the class allow you to create mappings between interfaces or abstract classes and the concrete types that will be used during run time. The simplest approach is to create one to one mappings for each interface / concrete type pair. Nothing you couldn't do with a simple text or configuration file of some sort. However, there are more powerful methods available that will automatically map all types against the interfaces they implement within a given assembly or namespace. You can even map multiple types against the same interface and use a key to get the correct implementation for a given situation at run time. Score one for the IoC framework.

As for creating the mapped instances, there are a couple different ways to get that done. The simpler approach is just a factory method that accepts the interface name and optionally a key and returns the configured implementation. Again, this is nothing that you can't do very quickly and easily with the Activator object in .NET. Once again, a more powerful technique exists and that is to request an instance of an object that accepts mapped types in its constructor. E.g. Suppose I have a class SiteController with a constructor defined as void SiteController(ISiteMapper mapper, ISiteRepository repository) and that both the interfaces have mapped concrete classes configured. I could request an instance of SiteController from the StructureMap ObjectFactory and it would create the SiteController container with the appropriate dependencies passed to the constructor. This is another nice convenience that good IoC frameworks provide you with.

Martin Fowler said something I like about IoC:
"When these containers talk about how they are so useful because they implement 'Inversion of Control' I end up very puzzled. Inversion of control is a common characteristic of frameworks, so saying that these lightweight containers are special because they use inversion of control is like saying my car is special because it has wheels."
My take on this quote is that IoC is nothing more than good object oriented design that takes advantage of polymorphism and factory patterns. It's a technique that has been around since the first OO languages and isn't some new advance in software design. That being said, StructureMap and other frameworks like it do offer some great utilities that simplify the task of writing the code.

No comments:

Post a Comment