Circular reference problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vera

    #1

    Circular reference problem

    I have two assemblies that each consist of several classes.
    Each object instantiated from those classes can have one or more child- and/or parentobjects that are also instantiated from those classes. Most relationships exist within one assembly, but relationships between assemblies may sometimes occur. For each relationship, I have to make sure that an object from Class1 knows that it can have an object from Class2 as one of its children. Likewise, I have to make sure that an object from Class2 knows that it can have an object from Class1 as its parent.
    With a relationship existing between two assemblies, this obviously results in a circular reference. However, in this case a circular reference is exactly what is required.

    How can I solve this problem? Or, in other words, how can I work around the circular reference restriction?

    Any help will be very much appreciated!

    Vera

  • Miha Markic [MVP C#]

    #2
    Re: Circular reference problem

    Hi Vera,

    You should put crossreferenced classes in shared third assembly.

    --
    Miha Markic [MVP C#] - RightHand .NET consulting & software development
    miha at rthand com


    "Vera" <anonymous@disc ussions.microso ft.com> wrote in message
    news:EEF9E650-A3CF-4619-95A9-B71FCFE2E81B@mi crosoft.com...[color=blue]
    > I have two assemblies that each consist of several classes.
    > Each object instantiated from those classes can have one or more child-[/color]
    and/or parentobjects that are also instantiated from those classes. Most
    relationships exist within one assembly, but relationships between
    assemblies may sometimes occur. For each relationship, I have to make sure
    that an object from Class1 knows that it can have an object from Class2 as
    one of its children. Likewise, I have to make sure that an object from
    Class2 knows that it can have an object from Class1 as its parent.[color=blue]
    > With a relationship existing between two assemblies, this obviously[/color]
    results in a circular reference. However, in this case a circular reference
    is exactly what is required.[color=blue]
    >
    > How can I solve this problem? Or, in other words, how can I work around[/color]
    the circular reference restriction?[color=blue]
    >
    > Any help will be very much appreciated!
    >
    > Vera
    >[/color]


    Comment

    • Jay B. Harlow [MVP - Outlook]

      #3
      Re: Circular reference problem

      Vera,
      In addition to Miha's suggestion.

      The Separated Interface Pattern is very useful to avoid a circular reference
      between assemblies.

      Defines an interface in a separate package from its implementation.


      Define an Interface that Class1 uses that Class2 implements. Put this
      interface in the same assembly as Class1 (or a third assembly). The Class1
      assembly needs to reference the assembly where the interface is defined if
      its not in the same assembly. The Class2 assembly needs to reference the
      Class1 assembly & the assembly where the interface is defined. Class2 can
      reference Class1 directly, while Class1 can only reference Class2 via the
      interface that it implements.

      Something like:
      ' Assembly 1
      Public Interface IClass2
      Public Property Value1() As Integer
      Public Sub Method1()
      End Interface

      Public Class Class1

      Public Sub Test(ByVal object2 As IClass2)
      If object2.Value1 = 100 Then
      object2.Method1 ()
      End If
      End Sub

      End Class

      ' Assembly 2
      ' References Assembly 1

      Public Class Class2
      Implements IClass2

      Public Property Value1() As Integer Implements IClass2.Value1
      ...

      Public Sub Method1() Implements IClass2.Value1
      ...

      End Class

      Note instead of an Interface, you could use an Abstract Base Class
      (MustInherit).

      Hope this helps
      Jay

      "Vera" <anonymous@disc ussions.microso ft.com> wrote in message
      news:EEF9E650-A3CF-4619-95A9-B71FCFE2E81B@mi crosoft.com...[color=blue]
      > I have two assemblies that each consist of several classes.
      > Each object instantiated from those classes can have one or more child-[/color]
      and/or parentobjects that are also instantiated from those classes. Most
      relationships exist within one assembly, but relationships between
      assemblies may sometimes occur. For each relationship, I have to make sure
      that an object from Class1 knows that it can have an object from Class2 as
      one of its children. Likewise, I have to make sure that an object from
      Class2 knows that it can have an object from Class1 as its parent.[color=blue]
      > With a relationship existing between two assemblies, this obviously[/color]
      results in a circular reference. However, in this case a circular reference
      is exactly what is required.[color=blue]
      >
      > How can I solve this problem? Or, in other words, how can I work around[/color]
      the circular reference restriction?[color=blue]
      >
      > Any help will be very much appreciated!
      >
      > Vera
      >[/color]


      Comment

      Working...