Click here to Skip to main content
15,884,472 members
Articles / All Topics

The Littlest IoC that Could

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
22 Jul 2014CPOL3 min read 5.9K  
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 it's 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
My name is Kevin Mack, I'm a software developer in the Harrisburg Area. I have been a software developer since 2005, and in that time have worked on a large variety of projects. Everything from small applications, to mobile and Enterprise solutions. I love technology and enjoy my work and am always looking to learn something new. In my spare time I love spending time with my family, and learning new ways to leverage technology to make people's lives better. If you ask me what I do, I'll probably tell you I can paid to solve problems all-day-every-day.

Check out my blog at https://kmack.azurewebsites.net/ and https://totalalm.azurewebsites.net/

Comments and Discussions

 
-- There are no messages in this forum --