In IntroductionConstructor Injection,Property InjectionAndMethod call InjectionIt is particularly mentioned that circular references (Circular references) should not appear, because it is difficult to detect such problems. The best solution is to writeCodeAvoid this situation whenever possible.
Let's take a look at several scenarios of circular references. We should try to avoid this situation when using the dependency injection framework:
1. The objects generated through constructor injection reference each other in the parameters of the constructor.
Public Class Class1
{
Public Class1 (class2 Test2)
{
..
}
}
Public ClassClass2
{
PublicClass2 (class1 test1)
{
}
}
Constructor injection is implemented when class1 is loaded through the Unity container. Because class1 depends on class2, class2 is loaded first. Constructor injection will also be performed when class2 is loaded. Because class2 is dependent on class1, class1 ...... in this way, continuous loading becomes an endless loop and cannot be jumped out.
2. The object generated through constructor injection serves as the parameter of the constructor.
Let's look at the example:
Public Class Class1
{
Public Class1 (class1 test1)
{}
}
3. Mutual reference of objects generated through method call Injection
Let's look at the example:
Public Class Class1
{
[Injectionmethod]
Public Void Method1 ()
{
Method2 ();
}
[Injectionmethod]
Public VoidMethod2 ()
{
Method1 ();
}
}
4. objects generated through property (setter) injection reference each other
Let's look at the example:
Public Class Class1
{
[Dependency]
Public String Propert1
{
Get
{
Return Propert2;
}
}
[Dependency]
Public StringPropert2
{
Get
{
ReturnPropert1;
}
}
}
By: inrie (Hong Xiaojun)
Source:Http://www.cnblogs.com/inrie