Support for auto-wired constructor injection?

61 views
Skip to first unread message

Kristopher Schultz

unread,
Jan 21, 2014, 1:42:49 PM1/21/14
to objection...@googlegroups.com
I know that Objection allows you to designate which init method gets called when Objection creates new instances of a class, but does it support any way to have the init arguments automatically fill with objects that the injector already has mappings for? For example...

Say I have a PhotoGallery class with the following init method...

- (id)initWithPhotoService:(id<PhotoService>)service;

And say that I have already mapped the class FlickrService to the PhotoService protocol...

// Inside my JSObjectionModule subclass
[self bindClass:[FlickrService class] toProtocol:@protocol(PhotoService)];

I want an instance of FlickrService to be automatically injected via the init method when I do this...

PhotoGallery *gallery = self.injector[[PhotoGallery class]];

...or (especially) in cases where this approach is used...

objection_requires_sel(@selector(gallery));

Is that supported? I had assumed it would be but have not been able to get it to work.

PR

unread,
Apr 22, 2015, 8:11:41 AM4/22/15
to objection...@googlegroups.com
A little late to answer, but for reference to other people:

The current way to do this is binding a block for your class inside your module.

[self bindBlock:^(JSObjectionInjector *context) {
        return [context getObjectWithArgs:[PhotoGallery class], [context getObject:@protocol(PhotoService)], nil];
    } toClass:[PhotoGallery class]];


And your class PhotoGallery  must be similar to this:

objection_initializer(initWithPhotoService:)

- (instancetype)initWithPhotoService:(id<PhotoService>)service {
    if (self = [super init]) {
        _service = service;
Reply all
Reply to author
Forward
0 new messages