As my Mono for Android app continues, I’ve been introducing more and more libraries to try to sort out its structure.
One of the largest changes to my development many moons ago was the use of a dependency injection container. There is only so far that I can go in any project before needing one – even if it’s just a test project.
Greg Shackles wrote about using either TinyIoC or Func with Mono for Android back in Feb 2011.
So I’ve added TinyIoC to my app and have been registering the classes, adding interfaces to separate component dependencies etc.
Now in WPF / Silverlight, we’ve been spoilt by the powerful AutoFac. It seems a little heavyweight for a phone app though, but that doesn’t stop me wanting some of its power.
I chose to try TinyIoC because:
- it’s just a single cs file that you include in your project – simples!
- it had support for automatic factories – so you register T and can then inject Func<T>
What I’ve found that is that many of my components have a mixture of dependencies and parameters in the constructor. AutoFac just copes with this automagically, but I’ve been having to register custom factories for everything in TinyIoC. It’s ok for now but will get tedious quickly. It also uses a slightly cumbersome string object dictionary for custom parameters.
If you’re doing IoC correctly, then none of the components should be affected by which container you’re using – so I might try a different container shortly.