Circular References! Big Problem!

P

pnp

I'm developing an app (in C #) that uses 2 usercontrols that must be in
different dll's.
The problem is that each one needs to use the other, so as a result I get a
circular reference error when I try to add the references to the dll's.

Is there a way round this problem? In C++ one could use header files... What
can I do here?
 
J

Jay B. Harlow [MVP - Outlook]

pnp,
Use a Separated Interface Pattern.

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

Project1 would define UserControl1 along with Interface2. UserControl1 would
maintain a reference to Interface2. Interface2 would have any Properties,
Subs, Functions, or Events that UserControl1 needed of UserControl2.

Project2 would define UserControl2 which implements Interfac2.

Project2 would reference Project1, however Project1 would not reference
Project2.

Your form's project could reference both Project1 & Project2 & connect the
two controls on the forms.


However! I have to ask the question: Aren't you introducing too much
coupling if UserControl1 requires UserControl2 & visa versa? I would
seriously consider a Mediator Pattern instead of a Separated Interface
Pattern, as the Mediator would have a reference to the two controls
mediating any communication between the two controls. The form itself could
be the Mediator, or the Mediator could be its own UserControl or Component.

Hope this helps
Jay
 
P

pnp

Thanks Jay, on your replying so soon.

By using interfaces the problem still remains because I need to create
objects of
classA in Proj1 in classB in Proj2 and vice versa... These are graphical
usercontrols and I don't want to put them in the same dll because they need
to be seperate modules... :(

Besides the mediator function, is there something else that could be done?


Peter N. P.
 
J

Jay B. Harlow [MVP - Outlook]

pnp,
No the problem does not still exist!

Are you saying you want to: create Control1 on top of Control2 on top of
Control1 on top of Control2 on infinity? that obviously wont work, or at
least it does not really make sense (to me) to visually nest one control on
another that is nested on itself.

What I gave will allow ClassA to have an instance of ClassB that also has a
reference to the first ClassA! Where ClassA & ClassB are in separate
projects.

Something like:

public interface Interface2
{
void Method2()
}

public class Class1
{

Interface2 i2;

public Class1(Interface2 i2)
{
this.i2 = i2;
}
}

public class Class2 : Interface2
{

Class1 c2;

public Class2()
{
c2 = new Class1(this);
}

void Method2()
{
}
}

Instead of passing is to expose a Interface2 property on UserControl1.
Depending on the requirements of the project I will actually use
Activator.CreateInstance to create an instance of the class that implements
the interface...

Because Project2 (class2) has a reference to Project1, Class2 can use
methods of Class1 directly. Class1 however uses methods of Class2 indirectly
via the interface. Note in this case the interface can be either an actual
Interface of a base Class.

Hope this helps
Jay
 

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