Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Circular dependency between classes

Circular dependency between classes

Scheduled Pinned Locked Moved C#
databasequestioncsharpc++help
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vinayvraman
    wrote on last edited by
    #1

    Consider the following example..

    public class Student
    {
        public string Name { get; set; }
        public IList Courses { get; set; }
    }
    
    public class Course
    {
        public string Name { get; set; }
        public IList Students { get; set; }
    }
    

    In this example a circular dependency exists between Student class and Course class. The scenario arises when we are dealing with entity framework - wherein db tables are created as classes for a Student database management system. In case of C++ we use the concept of forward declaration for telling a class 'A' that another class called 'B' exists even before defining the class 'B'. Can anybody explain how C# compiler solves this problem of circular dependency between classes? PS: Please avoid telling me that the classes sould be redesigned to avoid circular dependency. The question is not related to designing since the below mentioned classes are very much from day-to-day programming scenarios.

    If asking a question is bad; then not knowing the answer is worse!

    P C 2 Replies Last reply
    0
    • V vinayvraman

      Consider the following example..

      public class Student
      {
          public string Name { get; set; }
          public IList Courses { get; set; }
      }
      
      public class Course
      {
          public string Name { get; set; }
          public IList Students { get; set; }
      }
      

      In this example a circular dependency exists between Student class and Course class. The scenario arises when we are dealing with entity framework - wherein db tables are created as classes for a Student database management system. In case of C++ we use the concept of forward declaration for telling a class 'A' that another class called 'B' exists even before defining the class 'B'. Can anybody explain how C# compiler solves this problem of circular dependency between classes? PS: Please avoid telling me that the classes sould be redesigned to avoid circular dependency. The question is not related to designing since the below mentioned classes are very much from day-to-day programming scenarios.

      If asking a question is bad; then not knowing the answer is worse!

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      With Interfaces. :-D

      public interface IStudent { String Name ... } ;
      public class Student : IStudent
      {
          public string Name { get; set; }
          public IList Courses { get; set; }
      }
      
      public interface ICourse { String Name ... } ;
      public class Course : ICourse
      {
          public string Name { get; set; }
          public IList Students { get; set; }
      }
      

      Or something like that.

      1 Reply Last reply
      0
      • V vinayvraman

        Consider the following example..

        public class Student
        {
            public string Name { get; set; }
            public IList Courses { get; set; }
        }
        
        public class Course
        {
            public string Name { get; set; }
            public IList Students { get; set; }
        }
        

        In this example a circular dependency exists between Student class and Course class. The scenario arises when we are dealing with entity framework - wherein db tables are created as classes for a Student database management system. In case of C++ we use the concept of forward declaration for telling a class 'A' that another class called 'B' exists even before defining the class 'B'. Can anybody explain how C# compiler solves this problem of circular dependency between classes? PS: Please avoid telling me that the classes sould be redesigned to avoid circular dependency. The question is not related to designing since the below mentioned classes are very much from day-to-day programming scenarios.

        If asking a question is bad; then not knowing the answer is worse!

        C Offline
        C Offline
        Clive D Pottinger
        wrote on last edited by
        #3

        I am by no means an expert, so don't take this answer as gospel. I am posting more to see if the gurus out there will correct any bad assumptions I have. As I believe: C++ is based on C. When C was created there was a need to keep compilers efficient - CPU time = $$$$. Therefore, the C language was designed with the idea that its compilers should not need to make multiple passes of the code. In a single-pass compiling strategy, you have to know what something is before you can use it. Hence procedures and variables had to be declared before they could be used. That led to the need for header files and forward declarations. I don't know if C++ compilers really need forward declarations, or whether it was just a carry over from C. But either way, this why I believe C++ has header files too. C# simply broke the tradition. If you allow your compiler to go over the code once and categorize the classes/methods/properties/fields and then go over it again to compile it, then there is no need for forward declaration, nor for header files. Am I way off base, Gurus?

        Clive Pottinger Victoria, BC

        L 1 Reply Last reply
        0
        • C Clive D Pottinger

          I am by no means an expert, so don't take this answer as gospel. I am posting more to see if the gurus out there will correct any bad assumptions I have. As I believe: C++ is based on C. When C was created there was a need to keep compilers efficient - CPU time = $$$$. Therefore, the C language was designed with the idea that its compilers should not need to make multiple passes of the code. In a single-pass compiling strategy, you have to know what something is before you can use it. Hence procedures and variables had to be declared before they could be used. That led to the need for header files and forward declarations. I don't know if C++ compilers really need forward declarations, or whether it was just a carry over from C. But either way, this why I believe C++ has header files too. C# simply broke the tradition. If you allow your compiler to go over the code once and categorize the classes/methods/properties/fields and then go over it again to compile it, then there is no need for forward declaration, nor for header files. Am I way off base, Gurus?

          Clive Pottinger Victoria, BC

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          That's right; Eric Lippert explained it in more detail on his blog[^]

          V 1 Reply Last reply
          0
          • L Lost User

            That's right; Eric Lippert explained it in more detail on his blog[^]

            V Offline
            V Offline
            vinayvraman
            wrote on last edited by
            #5

            Ah!! Multiple parsing is the secret!! :-D Thank you very much Clive for answering my question and thank you Harold for sharing that wonderful link. :thumbsup:

            L 1 Reply Last reply
            0
            • V vinayvraman

              Ah!! Multiple parsing is the secret!! :-D Thank you very much Clive for answering my question and thank you Harold for sharing that wonderful link. :thumbsup:

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              In C++ the compiler only needed a function's prototype or a class declaration, both usually found in headers. The compiler left it to the linker to locate the implementations and resolve those dependencies. When working with C++, this is a common error. The compiler compiles all sources without problems, but the linker stops with an error because you forgot to tell it where to locate a library which belongs to one of the headers you have included. So you may very well see the linker as a second pass after compiling.

              And from the clouds a mighty voice spoke:
              "Smile and be happy, for it could come worse!"

              And I smiled and was happy
              And it came worse.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups