24

Can anyone recommend a good IoC framework for iOS to facilitate dependency injection?

The only one I've found so far is Objection Framework which apparently is similar to Guice (which unfortunately is unfamiliar to me). Is this one a common choice for iOS and are there competing frameworks that are worth checking out?

Clafou
  • 15,250
  • 7
  • 58
  • 89

3 Answers3

21

Typhoon

The Typhoon-website lists the key features. A quick summary:

  • Non-invasive. No macros or XML required. Configuration is done using an Objective-C or Swift API that takes advantage of the Objective-C runtime.

  • Lightweight. Just 2500 lines of code. It has a very low footprint, so is appropriate for CPU and memory constrained devices. Tuned for performance.

  • Makes it easy to have multiple configurations of the same base-class or protocol.

  • No magic strings - supports IDE refactoring, code-completion and compile-time checking.

  • Supports injection of view controllers and storyboard integration.

  • Supports both initializer and property injection, plus life-cycle management.

  • Powerful memory management features. Provides pre-configured objects, without the memory overhead of singletons.

  • Excellent support for circular dependencies.

  • Battle-tested - used in all kinds of Appstore-featured apps.

  • An internationally distributed core team (we even monitor StackOverflow), so support for any of your questions are never far away :)

API Docs and sample app

Jasper Blues
  • 28,258
  • 22
  • 102
  • 185
  • 3
    The only choice with real understanding what DI is. Thank you Jasper! – Alexander Smirnov Apr 07 '13 at 20:03
  • 1
    @Jasper Blues I looked into this and other DI framework. I still don't quite get the benefit of using this versus doing it by hand even after reading through the doc. Using this or any framework seems so heavyweight and it's not without cost. Can you advise? – Boon Jun 02 '14 at 18:47
  • @Boon Sounds like a good question for StackExchange. – Jasper Blues Jun 02 '14 at 23:17
  • I am looking at Typhoon. I am astonished that I can not find something between a fully implemented app and a low level API documentation. Even though this project seems to be the most advanced that a clear flaw to my opinion. – AsTeR Nov 06 '14 at 13:43
  • @AsTeR Its not perfect, but have you tried the user guide here? https://github.com/typhoon-framework/Typhoon/wiki/Types-of-Injections . . . . For API documentation: http://www.typhoonframework.org/docs/latest/api/index.html – Jasper Blues Nov 06 '14 at 14:37
  • Your user guide seems closer to what I was looking for indeed. I didn't found it. Thanks for taking the time to provide me the link (it's under the "Run the example" title on your side, it deserves a link in the header to my personal taste). – AsTeR Nov 06 '14 at 14:59
  • It feels like this is not DI, this is service locator. Instead of getting rid of the dependency it moves the dependency to another layer. DI moves the dependency to a module, this framework keeps it in the classes. Good tool for writing testable code, but not a DI framework. – aryaxt Jan 19 '15 at 22:27
  • 1
    @aryaxt It is a DI container. Internally modeled on Spring (post-processors, etc). When proceeding from one top-level object graph (eg view controller) to another we ask the container for the instance. Don't confuse this with service locator, all DI containers do this. There's a good article by Martin Fowler, who helped coin the term DI in place of IoC, and in this article he compares DI to SL. An oldie but a goodie: http://martinfowler.com/articles/injection.html – Jasper Blues Jan 19 '15 at 23:05
  • 2
    @aryaxt Also note that when proceeding from one object graph to another the factory that provides the instance is itself injected and can be backed by a protocol. So not coupled to the framework at all. Goal of Typhoon was to be non-invasive. . (Worked on the introduction of Spring Framework years ago and used to teach it around the world for SpringSource, so I am fairly familiar with the topic). – Jasper Blues Jan 19 '15 at 23:39
15

...are there competing frameworks that are worth checking out?

Objection is the DI library I could find on google for iOS, so you might be stuck with it if you want a pre-built library.

DI doesn't specifically require a framework to use. If your app is small, you can simply create all your instances at the application root and inject by hand.

If you need more than this, and the existing frameworks aren't cutting it for you, you could roll your own Service Locator, then build a DI container on top of it.

You could also port an existing smaller framework from another platform. There are several "small" ones on .Net, for example - Ninject and SimpleInjector.

Is this one a common choice for iOS...?

It seems that it is a fairly small project as there is only one author/contributor listed. There aren't many issues filed. The iOS market is fairly large though. So I'm thinking that only a very small portion of all iOS developers use this library.

But this isn't necessarily a bad thing. It seems to be created, used, and supported by a small company. It has had fairly steady updates for the past year.

My anecdotal experience with similarly scoped open source projects: I don't always get new features super-often, and I'm often the one who ends up finding bugs. But I tend to get support on the existing feature set very quickly, and a lot of attention is paid to support e-mails I've sent. YMMV.

Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
  • 2
    Your observations about Objection are accurate and fair. I am far and away the largest contributor to the project -- which means I generally add features I need or am inspired to make. I do respond to bugs very quickly. I tend, however, to chew on feature requests for quite sometime as I want to ensure the identity of Objection isn't loss and it doesn't become "bloated". Objection has been used in relatively popular applications. Based on the crash logs associated with it and the performance tests I've performed it has not been the cause of a crash or performance bottleneck. – justice Feb 04 '12 at 01:17
  • 2
    One last thing. Objection is very well tested: https://github.com/atomicobject/objection/tree/master/Specs – justice Feb 04 '12 at 01:27
  • @justice: Do you have a list of applications it has been used in? It might help for advertisement of the library to let people know about those (on the site of course - listing them here wouldn't be kosher). Would probably need to get permission. But testimonials tend to help answer questions like "how popular is this library?" and helps attract new users. – Merlyn Morgan-Graham Feb 04 '12 at 21:34
  • The one application I can talk about is "SideReel". We haven't actively worked on it in a year (Side Reel maintains it). But it has been featured by the appstore several times. – justice Feb 05 '12 at 15:13
11

Well, I hope you forgive me a little plug here, but I just released my own DI framework for Objective-C: Syringe

https://github.com/tomekc/Syringe

It is simplistic and lightweight on purpose, my primary goal was to make it as not obtrusive as possible. I have a strong Java background, so I designed it after Google Guice and Spring.

Tomek Cejner
  • 1,152
  • 1
  • 11
  • 17
  • 2
    +1 for the recommendation, even as a plug. If you want a jump start on a good way to introduce people to DI (since it is a fairly unexplored concept in the Objective-C world), take a look at [NInject's "Getting Started" wiki page](https://github.com/ninject/ninject/wiki/Getting-Started). NInject focuses on constructor injection rather than attribute-based injection, but the way their tutorials build up DI concepts works well. – Merlyn Morgan-Graham Jan 27 '12 at 21:53
  • Love! As stoked as I was about Objection I prefer how you automate fulfillment. Objection still provides a bit more flexibility though. What about properties? Do you intend to grow Syringe? – Texas Nov 25 '12 at 02:19
  • I currently use it in my small private projects, and I am open to hear some feedback from users. – Tomek Cejner Nov 28 '12 at 09:05
  • @Merlyn Morgan-Graham thanks for the Ninject-link! – Nick Weaver May 27 '14 at 10:50