How can i solve if i have the circular dependence?

  • Thread starter Thread starter micmic
  • Start date Start date
M

micmic

Dear all experts,
I have the Coding as following:
In File abc.cs
namespace A
{
public class abc
{

public abc()
{
B.bcd xxx = new B.bcd(this);

}
}

}

in file bcd.cs
namespace B
{
public class bcd
{
...................
public bcd (abc xxx)
{
.............................
}
}

}

where abc and bcd is in different project files. In order to make it work, i
add the dll in the reference. However, once i add for one of the namespace,
then i cannot add to another. Thwn what should i do to sovle this case of
problem ??

Best wishes,
Micmic
 
micmic said:
Dear all experts,
I have the Coding as following:

where abc and bcd is in different project files. In order to make it work, i
add the dll in the reference. However, once i add for one of the namespace,
then i cannot add to another. Thwn what should i do to sovle this case of
problem ??

You can't have two projects which depend upon each other. What you
*can* have, is an interface that one of the types implements, have that
in a third project which both projects depend on, and get out that way.
 
bigmouth1979 said:
Dear all experts,
I have the Coding as following:
In File abc.cs
namespace A
{
public class abc
{

public abc()
{
B.bcd xxx = new B.bcd(this);

}
}

}

in file bcd.cs
namespace B
{
public class bcd
{
...................
public bcd (abc xxx)
{
.............................
}
}

}

where abc and bcd is in different project files. In order to make it work, i
add the dll in the reference. However, once i add for one of the namespace,
then i cannot add to another. Thwn what should i do to sovle this case of
problem ??

Best wishes,
Micmic
I had this problem, and short of putting all code in the same project
you need to do a bit of thinking about how to organize your projects. It
actually can help in showing if you have two many dependencies.
 
Actually i don't mind by puting them in one project. But i would like to ask
whether you have different namespaces. ( But will it be too mess up if you
put into the same project?)
 
micmic said:
Actually i don't mind by puting them in one project. But i would like to ask
whether you have different namespaces. ( But will it be too mess up if you
put into the same project?)

Namespaces and assemblies as entirely separate concepts. You can have
multiple namespaces in an assembly, or (more rarely) namespaces which
have members spread across multiple assemblies.
 
As I recall for Java Interface, it will code the implement Class which
implement the Interface. But if I just wanna assign the value to which Class
is passed in 1 project . ie.

namespace A
{
public class abc
{
private int value1;
......................

public abc()
{
B.bcd xxx = new B.bcd(this);

}
public int Value1
{
get{return value1;}
set {value1=value;}

}
..................................

}

}

in file bcd.cs
namespace B
{
public class bcd
{
...................
public bcd (abc xxx)
{
.............................
xxx.Value1=uuu;
xxx.Value2=vvv;
xxx.Value3=www;
xxx.Value4=yyy;
}
}

}

I still can use the method you said ??

Best wishes,
Michael
 
Hi, micmic!
I have the Coding as following:
In File abc.cs
namespace A
{
public class abc
{

public abc()
{
B.bcd xxx = new B.bcd(this);

}
}

}

in file bcd.cs
namespace B
{
public class bcd
{
...................
public bcd (abc xxx)
{
.............................
}
}

}

where abc and bcd is in different project files. In order to make it work, i
add the dll in the reference. However, once i add for one of the namespace,
then i cannot add to another. Thwn what should i do to sovle this case of
problem ??

Your must create third class library and declare abstract class abc_base:

Lib 3:

namespace A
{
public abstract class abc_base
{
...
}
}

then Lib 1 must references to Lib2 and Lib3

namespace A
{
public class abc: abc_base
{
...
}
}

and Lib 2 reference to Lib3

using A;
namespace B
{
public class bcd
{
public bcd(A.abc_base qqq)
{
...
}
}
}



Sorry for bad english.
 
Micmic,
In addition to the other's comments.

It sounds like you need to use the Separated Interface Pattern.

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

In bcd.cs define interface Iabc, which has the methods that bcd needs to use
from abc. The constructor to bcd accepts an Iabc interface. The A project
would reference the B project, while the B project does not reference the A
project.

Something like:
In File abc.cs
namespace A
{
public class abc : Iabc
{

public abc()
{
B.bcd xxx = new B.bcd(this);

}
void method1()
{
}
}

}

in file bcd.cs
namespace B
{
public interface iabc
{
void method1();
}
public class bcd
{
...................
public bcd (abc xxx)
{
.............................
}
}

}

Hope this helps
Jay
 
but in the bcd.cs, i would like to assign the value for abc class

namespace A
{
public class abc
{
private int value1;
......................

public abc()
{
B.bcd xxx = new B.bcd(this);

}
public int Value1
{
get{return value1;}
set {value1=value;}

}
..................................

}

}

in file bcd.cs
namespace B
{
public class bcd
{
...................
public bcd (abc xxx)
{
.............................
xxx.Value1=uuu;
xxx.Value2=vvv;
xxx.Value3=www;
xxx.Value4=yyy;
}
}

}

Can I use the method you mention ?

Best wishes,
Micmic
 
Roman,
Doh! it was a quick example ;-)

Jay

Roman S. Golubin said:
Hi, Jay B. Harlow!

then correct bcd constructor:

in file bcd.cs

public bcd (iabc xxx)
 
micmic,
but in the bcd.cs, i would like to assign the value for abc class
The Iabc interface needs to define everything that bcd needs from abc. It is
the "third party" contract between abc & bcd.

So you need to have properties for Value1, Value2, Value3, and Value4
defined in the IAbc interface, that bcd can use that abc itself can
implement.

Iabc is the Separated Interface. Note Separated Interface could be
implemented with a base class (abstract or otherwise) instead of an actual
interface.
namespace A
public class abc : Iabc
{

namespace B

interface Iabc
{
int Value1 { get; set; }
int Value2 { get; set; }
int Value3 { get; set; }
int Value4 { get; set; }
int Value5 { get; set; }
}
public class bcd
{
...................
public bcd (Iabc xxx)
{
.............................
xxx.Value1=uuu;
xxx.Value2=vvv;
xxx.Value3=www;
xxx.Value4=yyy;
}
}

Hope this helps
Jay
 
Back
Top