Re: Circular Referencing in C#
From: Nick Malik [Microsoft] (nickmalik_at_hotmail.nospam.com)
Date: 02/16/05
- Next message: Phil Williams: "RE: From C++ to C# and back again...."
- Previous message: Ken Tucker [MVP]: "Re: Creating Word Document Issue"
- In reply to: PromisedOyster: "Circular Referencing in C#"
- Messages sorted by: [ date ] [ thread ]
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??????
> }
> }
> }
>
- Next message: Phil Williams: "RE: From C++ to C# and back again...."
- Previous message: Ken Tucker [MVP]: "Re: Creating Word Document Issue"
- In reply to: PromisedOyster: "Circular Referencing in C#"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|