locked
Circular dependency RRS feed

  • Question

  • User-1478786865 posted
    Is there an elegant way to get around circular dependency. I have a base class and two child classes. The child classes need to refer to each other, but I cannot add a reference both ways because it gives me the "circular dependency" error. I do not want the child classes to be in the same dll although I know that will solve this particular problem, because except for one property which has to go two-way the two child classes are quite different from one another and better off in their own dlls. I tried creating an Interface class, but I have the same problem because I added a reference to both the child classes to the Interface, but the two child classes need to reference each other to implement the interface. The situation with a general example is this: Base class: Toyota Child class1: LandCruiser Child class2: Forerunner Land cruiser needs a property called "ForeRunner" and Forerunner needs a property called "LandCruiser" and the problem is, I cannot add reference to each other. Has anybody had the same situation and if so, can you please let me know how you got it to work. Thanks in advance.
    Monday, August 2, 2004 11:57 AM

All replies

  • User-967169866 posted
    ooo BAD! You should slap yourself twice! It's 4Runner! :) just kidding. I think you have a flawed architecture. Rather than calling LandCruiser Class, I'd make a base class that will encompass both LandCruiser and 4Runner. That way you can store the base class in the base reference, and overload specific things in the child assemblies. They'll still be able to refer to other classes since they are the same thing. OR you can start utilizing interfaces. Either way, I think your design's a big no-no. To have a class depend on another class that depends on each other is poorly designed oop. I don't mean to hate or be mean, but it's just not good practice. I'd break from the fear of having the parent assembly being so compact. If you need things there, then might as well put them there. Know what I mean?
    Monday, August 2, 2004 1:30 PM
  • User19793893 posted

    Hello Suparna,

    I understand your pain... and let me say it "You are not alone"!  I have found myself so many times in the same situation, especially when I am working on fixing legacy code. 

    One solution that I really like is the following:

    http://theadventuresofacodeaholic.com/Temp/image/CircularDependencySolution1.jpg

    As you can see, the assemblies A, B, and C implement respectively (and individually) the classes A, B, and C. These classes are using and referencing each other through its interfaces. The interfaces are all kept together in a separate assembly (which can be used as a "global" assembly).  A controller assembly (which in this case holds the entry point method for your application), references the assemblies that contains each specific class it needs to instantiate as well as the assembly containing the "global" interfaces.

    As they say, a picture is worth a thousand words and I really hope the drawing is clear enough. However, if you still have any questions about this solution, please let me know.  I have its source code implementation and I will be happy to share it with you.

    Regards,

    The Codeaholic.

    Friday, September 25, 2009 2:05 PM
  • User-952121411 posted

    I do not want the child classes to be in the same dll although I know that will solve this particular problem, because except for one property which has to go two-way the two child classes are quite different from one another and better off in their own dlls.

    I don't think this is such a big deal and it would solve your issue.  They both are Toyotas after all right?  It is not like we are talking about apples and rockets or something drastically different.  You can certainly have separate files, classes, namespaces, etc but all get bundled up into one assembly that has a more generic or abstract name like MycompanyName.Toyotas.Models.dll or something like that.

     

    Monday, October 12, 2009 5:11 PM