Deep understanding of dependency injection: http://kb.cnblogs.com/page/45266/
. Net reflection and dependency Injection
Interface reflection:
Interface Layer: the interface is defined as the Dal layer.
Interface idal
Use reflection technology (reflection) to create interface instances required to lock idal in factory Mode
Example:
String Path = "Dal ";
Private Dal. idal createidal (string Str)
{
String classname = path + ". Dal" + STR;
Return (DAL. idal) system. reflection. Assembly. Load (PATH). createinstance (classname );
}
Public String getstr (string Str)
{
Idal Dal = createidal (STR );
Return Dal. getstring ();
}
Dependency injection:
According to the requirement mode of dependency Injection
Interface: IA
A: IA
Interface: IB
B: IB
Implement in A: IA
Instantiation of IB (using reflection technology)
In the main program, you can implement the IA instance and call the IB member function.
Example:
Define the class library idal
IA. CS
Using system;
Namespace idal
{
Public interface IA
{
String show ();
}
}
Ib. CS
Using system;
Namespace idal
{
Public interface IB
{
String getcode ();
}
}
A. CS
Using system;
Namespace idal
{
Public Class A: IA
{
Private IB = factory. createib ();
Public String show ()
{
Return Ib. getcode ();
}
}
}
B. CS
Using system;
Namespace idal
{
Public Class B: IB
{
Public String getcode ()
{
Return "B ";
}
}
}
Factory. CS // interface factory required for reflection
Using system;
Namespace idal
{
Public class factory
{
Static string Path = "idal ";
Static string classname = "";
Public static idal. Ia createia ()
{
Classname = path + ". ";
Return (idal. Ia) system. reflection. Assembly. Load (PATH). createinstance (classname );
}
Public static idal. IB createib ()
{
Classname = path + ". B ";
Return (idal. Ib) system. reflection. Assembly. Load (PATH). createinstance (classname );
}
}
}
Call a function
Ia = factory. createia ();
IA. Show ();
From: http://blog.csdn.net/angelzjk/article/details/5620324