views:

600

answers:

4

Hello guys, Thanks for reading.

I'm using Unity framework to implement dependency injection in my app (ASP.Net MVC). Sometimes there are some cyclic dependencies among services that I want to avoid.

So I'm looking for solutions : )


My case

well lets imagine 3 services ServiceSally, ServiceJoe, ServiceJudy

ServiceSally depends on ServiceJoe

ServiceJoe depends on ServiceJudy

ServiceJudy depends on ServiceSally (<< That is kind of weird isn't it?)

So if you instance ServiceSally, she will need ServiceJoe to be injected, and ServiceJoe will need ServiceJudy and.... BANG!... ServiceJudy will need ServiceSally starting an endless cycle -and very sad love triangle-.


How could I solve this cyclic-loveTriangle case? : /

UPDATE:

My first solution: The LazyJoe

What about to use a wrapper around the services references to delay the injection until they are used?

What do you think?

+6  A: 

This depends on what (if any) DI framework you're using. Spring for example will handle this kind of cyclic dependency as long as not every involved bean (object) is initialized by a constructor. Basically it injects an empty object into (at least) one of the other beans and initializes it later. So the sequence is something like:

  1. Create a ServiceSally
  2. Create a ServiceJoe
  3. Create a ServiceJudy
  4. Initialize ServiceJudy
  5. Inject ServiceJudy into ServiceJoe
  6. Initialize ServiceJoe
  7. Inject ServiceJoe into ServiceSally
  8. Initialize ServiceSally
  9. Inject ServiceSally into ServiceJudy
  10. Tell ServiceJoe, ServiceJudy and ServiceSally that they're ready

This is why initialization-on-construction won't work with this method (because initialization is deferred). It's really the only way to handle it. Well maybe you could use some kind of proxy (temporary or permanent) too.

Generally speaking, at least in my experience, cyclic dependencies are symptomatic of a design that is either flawed in some way or in need of simplification.

cletus
Hi Cletus, thanks for answer : ) +1I'm using Unity.You're right I think a simplification could be needed.. but at this point of the project life it's going to be delayed a while ; )respecting Spring-way to do it... then it's a good idea to use lazy DI? : O
SDReyes
at step 9. ServiceSally instance isn't created for ServiceJudy'cause step3 finish all the object creation phase right? : O
SDReyes
SD": it's not that lazy DI is a good idea in these circumstances, it's simply about the only option.
cletus
thanks Cletus : )
SDReyes
+1 for "generally speaking...cyclic dependencies are symptomatic of a design that is either flawed...or in need of simplification." There are specific, rare cases where cyclic dependencies may be the best solution to a problem, but they are rare.
Greg D
+2  A: 

Don't make a Service* dependent on another concrete Service*. Make them dependent on a superclass or interface. Then inject a concrete Service* into another Service* after creation.

Ozan
Thanks for your solution Ozan! +1 very creative : D
SDReyes
A: 

I agree with Cletus to some extend, whenever you find yourself having services depend on one another it's time to sit down and rethink your design.

If you do "lazy DI", why are you doing DI at all? One of the benefits of using DI is not having to worry when your dependencies are initialized, you just have them there when you need them.

JoseMarmolejos
I think you totally missed the point
Adriaan Koster
A: 

Hi There,

I really like Ozan's creative solution. Can anyone send me sample spring configuration file for Ozan's solution. I am a newbie and I appreciate any help. Thank you, Hari

Hari