jbsf / blindside

Blindside provides dependency injection capabilities for Objective-C on iOS and OS X
MIT License
71 stars 43 forks source link

The world implodes when I bind to a previously bound class, then attempt retrieve an instance #23

Closed akitchen closed 9 years ago

akitchen commented 9 years ago

We found ourselves in a situation where we wanted to override a binding; the key was a class which had been previously bound to an instance. The world imploded.

        context(@"when the key is a class which has an existing binding", ^{
            it(@"should build the class without infinite recursion", ^{
                [injector bind:[Address class] toInstance:[NSNull null]];
                [injector bind:[Address class] toClass:[Address class]];
                id address = [injector getInstance:[Address class]];
                expect(address).to(be_instance_of([Address class]));
            });
        });

Alternatively, is there a recommended way to remove a binding to restore the default behavior? This came up in a testing scenario.

thunderweasel commented 9 years ago

I think this has also come up once or twice in the Toronto office. It would be handy to be able to remove bindings.

joemasilotti commented 9 years ago

I'm now running into this situation as well. Are there any alternatives available?

briancroom commented 9 years ago

I'll add that the root problem isn't overriding bindings. The problem is that binding a class key to the class itself is not currently supported:

context(@"when the key is a class which is bound to the same class", ^{
    it(@"should build the class without infinite recursion", ^{
        [injector bind:[Address class] toClass:[Address class]];
        id address = [injector getInstance:[Address class]];
        expect(address).to(be_instance_of([Address class]));
    });
});
jbsf commented 9 years ago

Thanks for tracking this down. Do we know whether this is easily fixed?

On Tue, Sep 8, 2015 at 8:02 PM, Brian Croom notifications@github.com wrote:

I'll add that the root problem isn't overriding bindings. The problem is that binding a class key to the class itself is not currently supported:

context(@"when the key is a class which is bound to the same class", ^{ it(@"should build the class without infinite recursion", ^{ [injector bind:[Address class] toClass:[Address class]]; id address = [injector getInstance:[Address class]]; expect(address).to(be_instance_of([Address class])); }); });

— Reply to this email directly or view it on GitHub https://github.com/jbsf/blindside/issues/23#issuecomment-138671265.