A Really Super Light and Simple IoC Container for Windows Phone 7
I finally managed to get the Windows Phone 7 tools installed. I’m not going to vent on that anymore because I feel like I’ve done that enough already – and once they did install correctly they’ve been very pleasurable to use. I started working on an application, and old habits die hard. I like Inversion of Control, it’s a major need for me simply because I’ve forced my mind to work like that. I’ve previously worked with Unity and Windsor. Unity has grown on me a lot, and I like it. However, none of them seem to work in Windows Phone 7, or at least they weren’t designed to. So I wrote my own really super simple IoC container for Windows Phone 7. I wanted the following features, and nothing else (for now):
- Able to register types such that arguments of it’s constructor are resolved
- Able to register an interface and type such that if the interface is resolved, the component is resolved and it’s constructor arguments are resolved
- Assume that there is either a default constructor or a single constructor that takes parameters
- Everything will have a singleton lifetime.
Super simple requirements. This will be running on phone hardware, so it needs to be lightweight, too. It fits in a single class which is about 65 lines. I’ve split it into two different files using a partial class to keep the registration separate. I can imagine some additional features that other people might want, such as a transient lifestyle, use WeakReferences if you are registering a lot of things, etc.
For the source, see the Gist on GitHub.