Re: Circular Referencing in C#

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Nick Malik [Microsoft] (nickmalik_at_hotmail.nospam.com)
Date: 02/16/05


Date: Wed, 16 Feb 2005 02:29:33 -0800

Just for kicks, I used this problem as an interview question about a month
ago, to see if the candidates were familiar with basic techniques in OO
design. One got it right, one got there after a few minutes, and one never
made it. This is a good, common, tough problem that can be solved if
someone has shown you once or twice how to do it.

I would suggest that you take an IoC approach (Google "Martin Fowler
Inversion of Control" for a good paper on IoC).

This means that A2.dll provides some interfaces and, when you create a
particular object in A2, you either pass in a descendent of the interface in
the constructor or in a method call.

My fav example is this: I have a generic logging framework that is useful
for writing audit logs. I configure it using config files. Very general. I
also have a generic configuration handling library that allows me to read
and write config files. Now, I add some features to my config library, and
decide that I want those features to write to the logs. Bingo... circular
reference.

Some folks like seperate assemblies for the interfaces. Other folks like to
put the interface into the base code libraries. Pick one.

The answer is that either the logging library needs to declare that it will
use a config library that meets a particular interface, and have a config
class inherit from that interface, or, in reverse, have the config library
declare that it will use a logging library that meets a particular
interface, and have a logging class inherit from that interface. Once
again, pick one (or, if you like Aspect Oriented Programming... pick both
:-).

Then your calling app will create the config object, and either pass in no
logging library, or pass in a "dummy" logging library that meets the
interface requirements but doesn't log anything.

Next, your calling app will create the logging object, passing in the config
object in the constructor.

Lastly, your calling app will invoke a method on the config object to pass
in the logging library.

Of course, this may not be the easiest thing in the world to understand. I
hope it helps.

-- 
--- Nick Malik [Microsoft]
    MCSD, CFPS, Certified Scrummaster
    http://blogs.msdn.com/nickmalik
Disclaimer: Opinions expressed in this forum are my own, and not 
representative of my employer.
   I do not answer questions on behalf of my employer.  I'm just a 
programmer helping programmers.
--
"PromisedOyster" <PromisedOyster@hotmail.com> wrote in message 
news:1108446976.518847.208900@o13g2000cwo.googlegroups.com...
> Hi
>
> I have a situation where I want to use circular referencing. I have cut
> down the example for demonstration purposes.
>
> Say we have one executable (main.exe) and two DLLS (A1.dll and A2.dll).
> Main can access classes in A1 and A2; A1 can access classes in A2.
>
> However, I now want to access classes in A1 from A2. I cannot simply
> create a third DLL as the A1/A2 assemblies use other classes within
> their respective assemblies.
>
> I believe that I can use interface classes to solve this situation.
> However, to be honest, I'm not sure how?
>
> An example would be very useful....
>
>
> eg in main.exe
> --------------
> using A1;
> using A2;
>
> A1C1 a1c1 = new A1C1();
> A2C1 a2c1 = new A2C1();
>
>
> A1.DLL
> ------
> using A2;
> namespace A1
> {
> /// <summary>
> /// Summary description for Class1.
> /// </summary>
> public class A1C1
> {
> public A1C1()
> {
> A2C1 c = new A2C1();
> }
> }
> public class A1C2
> {
> public A1C2()
> {
>                       // I want access thi class from A2
> }
> }
> }
>
> A2.DLL
> ------
>
> namespace A2
> {
> /// <summary>
> /// Summary description for Class1.
> /// </summary>
> public class A2C1
> {
> public A2C1()
> {
>                    // Can access this class from A1
> }
> }
> public class A2C2
> {
> public A2C2()
> {
>                    // I now want to access  A1C2??????
> }
> }
> }
> 


Relevant Pages

  • Re: Casting Generic Classes - Possible Solution
    ... It leaves me with the open option to use generics or not when I ... This works as saying "accept a store of anything that are Record ... public class Record { ... So I tried adding an interface into the structure: ...
    (microsoft.public.dotnet.languages.csharp)
  • PPPoE/DSL -- no connectivity
    ... If anyone can give me some config pointers, ... fine and all interfaces are UP, ... Virtual-Access1 unbinds from Interface Dialer1 giving me this debug ... service timestamps debug datetime msec ...
    (comp.dcom.sys.cisco)
  • Re: Cisco T1 Internet Config
    ... If the line is functioning properly with this config, ... I personally would not have used a sub interface in this case as you only ... ip unnumbered command would have moved to the serial interface. ... encapsulation frame-relay IETF -- Do I need this line? ...
    (comp.dcom.sys.cisco)
  • Re: Trying to connet to Telia(Skanova) ADSL provider in Sweden with 837
    ... >>These Cisco 837 Config Wizards may or may not help you: ... > interface ATM0 ... > ip nat outside ... > bridge 1 protocol ieee ...
    (comp.dcom.sys.cisco)
  • Re: List<> classes and inheritance (C#)
    ... I think the answer to your question is the use of an interface that all ... public class MyClass1: IMyInterface ... There may be no need at all to override the Add method at this point because ...
    (microsoft.public.dotnet.csharp.general)