依赖注入模式 Dependency Injection Pattern (C#)
1. Constructor Injection
public class Boy
{
private IDog smallDog;
public Boy(IDog smallDog)
{
this.smallDog = smallDog;
}
}
public interface IDog
{
//method
}
2. Setter Injection
public class Boy
{
private IDog smallDog;
public IDog SmallDog
{
get { return smallDog; }
set { smallDog = value; }
}
}
public interface IDog
{
//method
}
3. Interface Injection
public class Boy : IDogMgr
{
private IDog smallDog;
public void InjectDog(IDog smallDog)
{
this.smallDog = smallDog;
}
}
public interface IDog
{
//method
}
public interface IDogMgr
{
void InjectDog(IDog smallDog);
}