Circular reference problem

G

Guest

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
 
M

Miha Markic [MVP C#]

Hi Vera,

You should put crossreferenced classes in shared third assembly.

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

Vera said:
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?
 
J

Jay B. Harlow [MVP - Outlook]

Vera,
In addition to Miha's suggestion.

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

http://www.martinfowler.com/eaaCatalog/separatedInterface.html

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 said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top