473,385 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Implications of circular references.

I have a circular reference between 2 classes in the same project (i.e.
each class refers to the other). The app runs fine and I am seeing no
issues, which kind of surprised me.

Are there any issues that I am not seeing (performance wise or garbage
collection wise) with circular references?

Thanks.
Nov 17 '05 #1
12 7035
The only circular reference issues I know of in .NET are between
assemblies. .NET doesn't allow classes in different assemblies to
mutually reference each other.

Other than that, no problem.

Nov 17 '05 #2
In message <ub*************@TK2MSFTNGP15.phx.gbl>, Frank Rizzo
<no**@none.com> writes
I have a circular reference between 2 classes in the same project (i.e.
each class refers to the other). The app runs fine and I am seeing no
issues, which kind of surprised me.

Are there any issues that I am not seeing (performance wise or garbage
collection wise) with circular references?


Getting round the circular references problem is one of the benefits of
the garbage collection algorithm .NET uses over a reference counting
scheme. If neither object is reachable, they're both available to be
GC'd.

--
Steve Walker
Nov 17 '05 #3
Bruce Wood wrote:
The only circular reference issues I know of in .NET are between
assemblies. .NET doesn't allow classes in different assemblies to
mutually reference each other.

Other than that, no problem.


I know about the fact that you can't do circular refs between
assemblies. What I am wondering is whether I'll have (possibly
difficult to troubleshoot) issues if there are circular refs between
objects in a single assembly.
Nov 17 '05 #4
They also come crashing in in COM interop. If I have a reference to a COM object via an RCW and I hand a refernce to myself to it (so it gets a CCW) we have a reference counted/live rooted deadly embrace

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

The only circular reference issues I know of in .NET are between
assemblies. .NET doesn't allow classes in different assemblies to
mutually reference each other.

Other than that, no problem.

Nov 17 '05 #5
None that I know of, other than it points out a possible place in your
design where you could use interfaces to do some abstraction.

I have circular references between my business layer and my data layer.
(My business layer is the "outer face" of the two layers, and so has to
know how to call the data layer. The data layer returns and consumes
business objects.)

It hasn't caused me any runtime / practical problems. My only regret is
that reading posts by Joanna Carter here I'm beginning to realize that
I should create an abstract data layer interface and have the business
layer call that instead, so that it doesn't have to know about the data
layer directly.

As I said, though, that's an OO design thing. As for practical
gotchas... none so far.

Nov 17 '05 #6
Bruce Wood wrote:
The only circular reference issues I know of in .NET are between
assemblies. .NET doesn't allow classes in different assemblies to
mutually reference each other.
Actually, it does.
c# handles it fine so long as you dont lose the binaries.
vb.net spits it.
(not that I'm advocating it in any way)
JB
:)
Other than that, no problem.

Nov 17 '05 #7
Actually, it doesn't, at least not within the same project. It's a
problem of which to compile first. Since the compiler gets its class /
interface / method signature information from the compiled assembly, it
can't compile assembly A, which references assembly B, without first
compiling B so that it can check A's references to things in B to
ensure that they're valid references. However, it can't compile B until
it first compiles A so that it can check B's references to things in A
to ensure that they're valid references.

Unfortunately, the compiler doesn't say anything like "circular
assembly reference". Instead it just throws up its (virtual) hands and
says that (if it chose to compile A first) it "can't find" any of the
things in assembly B.

I've never tried putting the circular-referencing assemblies in
different projects, and compiling one (without the circular reference),
then the other, then adding the circular reference and compiling the
first project again. In theory the compiler wouldn't notice, but as I
said, I've never tried it.

Nov 17 '05 #8
Visual Studio doesn't like circular project references but the C# compiler is quite happy with circular assembly references

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Actually, it doesn't, at least not within the same project. It's a
problem of which to compile first. Since the compiler gets its class /
interface / method signature information from the compiled assembly, it
can't compile assembly A, which references assembly B, without first
compiling B so that it can check A's references to things in B to
ensure that they're valid references. However, it can't compile B until
it first compiles A so that it can check B's references to things in A
to ensure that they're valid references.

Unfortunately, the compiler doesn't say anything like "circular
assembly reference". Instead it just throws up its (virtual) hands and
says that (if it chose to compile A first) it "can't find" any of the
things in assembly B.

I've never tried putting the circular-referencing assemblies in
different projects, and compiling one (without the circular reference),
then the other, then adding the circular reference and compiling the
first project again. In theory the compiler wouldn't notice, but as I
said, I've never tried it.

Nov 17 '05 #9
"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
None that I know of, other than it points out a possible place in your
design where you could use interfaces to do some abstraction.

I have circular references between my business layer and my data layer.
(My business layer is the "outer face" of the two layers, and so has to
know how to call the data layer. The data layer returns and consumes
business objects.)

It hasn't caused me any runtime / practical problems. My only regret is
that reading posts by Joanna Carter here I'm beginning to realize that
I should create an abstract data layer interface and have the business
layer call that instead, so that it doesn't have to know about the data
layer directly.

As I said, though, that's an OO design thing. As for practical
gotchas... none so far.


If you want more Bruce, our dear Joanna Carter, can easily be found on
borlands newsgroups, especially the oo-design group.

- Michael S
Nov 17 '05 #10
"Steve Walker" <st***@otolith.demon.co.uk> wrote in message
news:Qi**************@otolith.demon.co.uk...
In message <ub*************@TK2MSFTNGP15.phx.gbl>, Frank Rizzo
<no**@none.com> writes
Getting round the circular references problem is one of the benefits of
the garbage collection algorithm .NET uses over a reference counting
scheme. If neither object is reachable, they're both available to be GC'd.


Frank, with other words:

The GC will start by assuming that all objects are dead.
Then it starts traversing all object references, starting by the application
root and all object references on the stack.

Hence, if your circular objects cannot be reached, they have allready been
concidered dead and will be collected.

Happy Coding
- Michael S
Nov 17 '05 #11
"Bruce Wood" <br*******@canada.com> a écrit dans le message de news:
11**********************@z14g2000cwz.googlegroups. com...
None that I know of, other than it points out a possible place in your
design where you could use interfaces to do some abstraction.
Certainly, circular references are a good indicator that it is time to
refactor :-)
It hasn't caused me any runtime / practical problems. My only regret is
that reading posts by Joanna Carter here I'm beginning to realize that
I should create an abstract data layer interface and have the business
layer call that instead, so that it doesn't have to know about the data
layer directly.


As long as you have sufficient metadata available in your business objects,
there should be absolutely no need for the data layer to know about specific
business classes.

In my OPF, I use a generic SQL generation mechanism for storing/retrieving
instances of most classes, but if a class should prove difficult/slow
(reporting classes that contain many joins), I construct a custom
persistence handler for that class that has intimate knowledge of the class
and of the type of persistence connection behind the OPF. Use of a Mediator
class like this resolves the circular problem.

Joanna

--
Joanna Carter
Consultant Software Engineer
Nov 17 '05 #12
Bruce Wood wrote:
Actually, it doesn't, Yes, it does.
at least not within the same project. Within the same project it is definitely possible.
Within different projects, within the same solution, VS wont let you add
the reference, so it will not allow it.
It's a
problem of which to compile first. Since the compiler gets its class /
interface / method signature information from the compiled assembly, it
can't compile assembly A, which references assembly B, without first
compiling B so that it can check A's references to things in B to
ensure that they're valid references. However, it can't compile B until
it first compiles A so that it can check B's references to things in A
to ensure that they're valid references.

Unfortunately, the compiler doesn't say anything like "circular
assembly reference". Instead it just throws up its (virtual) hands and
says that (if it chose to compile A first) it "can't find" any of the
things in assembly B.

I've never tried putting the circular-referencing assemblies in
different projects, and compiling one (without the circular reference),
then the other, then adding the circular reference and compiling the
first project again. In theory the compiler wouldn't notice, but as I
said, I've never tried it.

Yes, this works.
I should have specified MS VS .NET though.
:)

JB
Nov 17 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Kiuhnm | last post by:
Is there an elegant way to deal with semi-circular definitions? Semi-circular definition: A { B }; B { *A }; Circular reference: A { *B }; B { *A }; The problems arise when there are more...
2
by: Earth Worm Jim | last post by:
I have been able to get simple circular references to be serialized in xml by using the ImportTypeMapping method on the SoapReflectionImporter class. But I am unable to serialise circular...
8
by: Eric Eggermann | last post by:
I'm having a problem with really large file sizes when serializing the classes that describe my little document. There are some circular references which result in the same object getting written...
3
by: Keith F. | last post by:
Visual Studio doesn't allow circular references between projects. I have a situation where I need to allow 2 projects to reference each other. Is there any way to make Visual Studio allow this? ...
5
by: Gos | last post by:
Hi, It is known that .NET does not allow us to add circular references. Is there a way to workaround this problem by late-binding the objects at run time? Will this create any other problems? ...
6
by: Stephen Robertson | last post by:
We are currently in a dead end with a circular reference issue using vb.net, and are hoping someone might help us resolve it. Idea... We have frmmain calling frmperson (dim f as new frmperson)...
2
by: Lapu-Lapu | last post by:
I have authored a web service using ASP 2.0. The web services return objects that use generics and that also contain circular references. Programmatically, everything works well, as long as you...
5
by: Madhur | last post by:
Hello If I define two classes in the same cs file. And in each class, I define the object of other class as a member. Can anyone explain me how .NET or its compiler will resolve this kind of...
2
by: Dansk | last post by:
Hi all, I am currently writing some code that explores assemblies dependencies. I start loading the first assembly with Assmebly.LoadFrom which gives me an Assembly instance. Then, I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.