Blog Moved

My blog has moved!

Monday, July 21, 2014

The Littlest IoC that Could

Hello All, I'm back again.  I know that updates have been more than a little sparse, but I'm doing my best to try and keep up with everything.  I'm definitely still trying to get a handle on this blogging thing.

So I did want to get a post out, and am going to be working on getting more posts out soon but in the mean time I thought I'd post about something that I've been working with a lot, and that's TinyIoC.

As many developers already know the idea of Dependency Injection and Inversion of Control has really taken the development world by storm over the past few years.  And to anyone who has worked with it at all, it really shouldn't be that surprising.  The basic idea of Dependency Injection is a relatively simple one.  We decouple our code by using interfaces and a construct called a "Container" to swap out those interfaces for a concrete class.

I'm not going to go into a lot of detail about Dependency Injection, that's a whole big blog post for another time.  But the real strength is that we can have our classes exists 100% detached from one another, and then use these containers to swap out interfaces for the appropriate classes.

There are lots of reasons that this idea can be beneficial.  For one, it makes unit testing a WHOLE lot easier.  Because now we can swap in test versions of the classes that are being used within a class and focus on testing only one unit at a time.  Another benefit is that it makes our code a whole lot more maintainable, cause if we need to build a new class we can, and just point the container at it instead of the old class and the container takes care of the rest.

But what I wanted to talk about in this blog post was Dependency Injection with regard to PCLs.  Right now I've been doing a lot of work with Xamarin, mainly to build applications in a professional, and a personal setting.  And it really is a powerful tool, and it shouldn't be all that surprising that if you want to build an app that is any bigger than a calculator...that you would want some form of dependency injection involved.

But there's the problem, see for you to be able to implement a DI container, in a Xamarin application (for iOS, Android, or Windows Phone), then it has to be PCL Compliant.

For those who aren't aware, PCLs or Portable Class Libraries, utilize a subset of the .net framework to allow us as developers to build reusable class libraries to utilize in our applications.  But because its only a subset of the .net framework.  Sometimes the dependencies that are required just plain aren't available to us in a mobile setting.

In the case of most of the larger containers out there.  For example Castle Windsor, Unity, Ninject, etc.  They are not PCL compliant.  But after a lot of researching and discussion with other devs, I found a great open source product that fills this need.  It's called TinyIoC.

You can find it on GitHub here.

While it may not be as powerful as some of its more robust cousins, it is great for using Constructor Injection, or even manually resolving interfaces within code.

So for those interested, I recommend checking it out.  Watch this space...later this week I'll be discussing how to use TinyIoC in an iOS application as well as how to leverage a bootstrap class to handle injection of dependencies at the PCL and then native application level.