jbsf / blindside

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

Delegates - Cyclic dependencies #51

Open ehrenmurdick opened 8 years ago

ehrenmurdick commented 8 years ago

We're trying to set up a dependency graph which includes a delegate with a reference back up the graph. When we try to inject all the deps with Blindside we get an exception "Cyclic dependency found on key…".

How can we use BS to inject an object's delegate? The delegate has a weak reference to a protocol going back up the chain, so we'd expect it to be ok.

briancroom commented 8 years ago

Hey @ehrenmurdick, I'm not so sure that Blindside is going to be able to help you very much with wiring up a delegate relationship like you're describing, although I'm not entirely sure without knowing more about what you're trying to achieve though.

Assuming you've working with the conventional Cocoa delegation pattern though, the reason you're running into trouble is that Blindside will try to instantiate a new object (and its transitive dependencies) whenever a dependency is requested, as opposed to populating the delegate field with s previously-instantiated object which is probably what you're hoping for. In these kinds of situations, the standard pattern is to hook up the delegate explicitly in code once the object graph has been set up.

If you have your heart set on having a framework help you out in this scenario, you may want to look into Typhoon which is powerful enough to handle this configuration.