atomicobject / objection

A lightweight dependency injection framework for Objective-C
http://www.objection-framework.org
MIT License
1.85k stars 223 forks source link

Getting a reference to the JSObjectionInjector which created an object? #97

Closed mflint closed 9 years ago

mflint commented 9 years ago

Hello team,

I sometimes need to get the JSObjectionInjector which was used to create an object, after the call to -awakeFromObjection has been performed, and would like to contribute a pull request to make this possible.

I can see two options:

  1. add support for a -awakeFromObjection:(JSObjectionInjector *)injector function, so the injector can pass itself to the created object
  2. add support for injecting the active JSObjectionInjector to a property, using Objection itself.

Would the maintainers have any preference? (Or a third, better way?)

Thanks for any advice you may have!

Matthew

Option 1

@property (nonatomic) JSObjectionInjector *injector;

-(void)awakeFromObjection:(JSObjectionInjector *)injector {
  self.injector = injector;
  ...
}

Option 2

@property (nonatomic) JSObjectionInjector *injector;

objection_requires(@"injector")
jdewind commented 9 years ago

The JSObjectFactory can be used in-lieu of the JSObjectionInjector. It is a proxy to the JSObjectionInjector that you can use to build objects from the injector context.

mflint commented 9 years ago

Awesome, thank-you @dewind. That could be the magic I was searching for!