circular references

  • Thread starter Thread starter news.austin.rr.com
  • Start date Start date
N

news.austin.rr.com

Hi,

I have 2 classes the need to reference eachother but i get build error for
circular references. How can I avoid this? Should i merge the classes? I
dont want to merge them since one is a config class...

Thanks
 
Do you mean 2 class libraries (dlls)? If not please explain further. If
yes...

.... even if it is possible to compile such a setup I would advise against it
(maybe some crazy reflection-based approach could workaround it).

Consider merging the two dlls since they have such a close relationship (or
move classes from one to the other until the dependency is unidirectional).
Alternatively, introduce a 3rd dll with the common dependencies and have the
two original ones reference that. Note that the extraction of common stuff
can be just an interface and not the actual implementation. Without more
info I cannot offer other advice.

Cheers
Daniel
 
Yes 2 dlls

So would the new class have and interface that the other classes use?
I havent done much with interfaces...
 
Tell me more about the dependency and I'll give you a clue. E.g. is it a
class A from dll_a depending on class B from dll_b and class B from dll_b
depending on class C of dll_a? Or is it actually two classes from each dll
depending on each other? The best thing to do is to create a small
reproducible sample and we can look at that (what ode is responsible for
creating each class is important).

The best approach is to merge them though...

Cheers
Daniel
 
It is 2 classes from each DLL.

One class is a configuration class and the other is a test measurments
class. So they dont really have much in common to justify merging the 2
classes but that is exactly what a co - worker did without my knowledge. So
I want to fix it and I like the idea of using a proxy type class/dll and
interfaces to keep the implementation in the original classes. Just not sure
how it should be structured.

Thanks
 
Before you set out to do something just because it is possible... I guess
the "test measurements" class uses the configuration class. How does the
config class use the test measurements? If both of these are infrastructure
classes, it makes sense keeping them in the same dll.

If you still want to continue keeping them in separate dlls please address
my other questions.

How does each class know about the other? Does one create the other? Passed
as an argument? Held at a class level field? Used in a local variable? Is
there a factory somewhere that creates both of them?

You really have to provide more on the scenario which is why I am asking for
repro code. That will give us a concrete example to talk about.

The general principle is that you create an interface and place it in its
own dll. The interface has all the public methods of the class. Wherever you
use the class, instead of using it directly, you use it via the interface
(this includes any signatures it appears in). Repeat for the other class
(new interface) and also place it in the new dll. The new dll is referenced
by the other two. So before you had dll_a referencing dll_b and vice versa.
Now you have dll_a and dll_b referencing dll_c.

Typically the creation code for the classes remains in one dll, let's say
dll_b (which means that dll_a also references dll_b but not the other way
round). When you provide more info on how *exactly* the two classes know
each other, we can look at this in more detail.

If there is a next question, I'll be looking for the attached repro and
answers to every question mark above ;-)

Cheers
Daniel
 
Both classes are static. Only has static fields, methods, properties ...

But the measurments class could be made a singleton
 
Note that the interface approach won't work as static methods are not
inheritable and not applicable to interfaces (wish you had divulged that
information earlier).

I paste here for your convenience some of my previously unanswered
questions.

How does the config class use the test measurements? How does each class
know about the other? Does one create the other? Passed as an argument? Held
at a class level field? Used in a local variable? The best thing to do is to
create a small reproducible sample and we can look at that (what ode is
responsible for creating each class is important).

Cheers
Daniel
 
Back
Top