We're hiring!

We're actively seeking developers for our new Detroit location. Learn more

Objection: Dependency Injection in Objective-C

For those of you that are familiar with the how and why of dependency injection (DI), we’ve created a DI framework for Objective-C called Objection. Those familiar with Google’s Guice will feel right at home with Objection.

We have long wanted DI for Objective-C.
Objection was our answer.

Why use this dependency injection stuff?

DI separates the components of the application in a clean way. It builds and composes the objects of the application. This separation makes code more test friendly. Mock objects can easily be injected into tests to keep the unit tests focused on what needs to be tested. (More info on dependency injection.)

What does it look like?

Basic Usage

A class can be registered with objection using the macros objection_register or objection_register_singleton. The objection_requires macro can be used to declare what dependencies objection should provide to all instances it creates of that class. objection_requires can be used safely with inheritance.

Fetching Objects from Objection

An object can be fetched from objection by creating an injector and then asking for an instance of a particular class. An injector manages its own object context, which means that a singleton is per injector and not necessarily a true singleton.

A global injector can be registered with Objection which can be used throughout your application or library.

Shawn Anderson (33 Posts)

Maker at Atomic Object. Lover of Ruby. Hobbyist game developer.

This entry was posted in Objective C and tagged , , , . Bookmark the permalink. Both comments and trackbacks are currently closed.

4 Comments

  1. Rrrrrr
    Posted December 9, 2010 at 2:41 pm

    Can it be suitable for creation of UIViewController?

  2. Posted December 9, 2010 at 2:41 pm

    It is not currently suitable for instantiating view controllers. Objection does not take constructor hints (ie initWithStyle: with the proper style). There also frameworks that dictate the instantiation of controllers like three20 which would not go through objection to create the controller. Rendering Objection useless.

    You can, however, ask for a controller’s dependencies directly from objection once it is instantiated.

    E.g. self.model = [Objection getObject:[MyModel class]];

  3. Posted December 9, 2010 at 2:41 pm

    Good stuff! I was jus thinking about writing something like this myself. Thanks for saving me the work. You should do a follow-up post on unit testing with Objection.

  4. Posted December 9, 2010 at 2:41 pm

    Thanks Chris,

    Yes we should probably expose a couple of the test helpers we use to ensure an object is properly registered with Objection. We currently have a couple of helpers that prove an object can be constructed and that it is setup with the correct dependencies. We’ll follow up with a blog post soon.

    Let me know how you like Objection.

One Trackback

  1. [...] a previous post we announced Objection, a dependency injection framework created for Objective-C. Since then, we [...]