
将耦合(依赖)对象注入(转换)为解耦(独立)对象的过程称为依赖注入。
依赖注入的类型
DI 有四种类型−
构造函数注入
Setter注入
基于接口的注入
服务定位器注入
接口注入类似对于 Getter 和 Setter DI,Getter 和 Setter DI 使用默认的 getter 和 setter,但接口注入使用支持接口(一种设置接口属性的显式 getter 和 setter)。
public interface IService{
string ServiceMethod();
}
public class ClaimService:IService{
public string ServiceMethod(){
return "ClaimService is running";
}
}
public class AdjudicationService:IService{
public string ServiceMethod(){
return "AdjudicationService is running";
}
}
interface ISetService{
void setServiceRunService(IService client);
}
public class BusinessLogicImplementationInterfaceDI : ISetService{
IService _client1;
public void setServiceRunService(IService client){
_client1 = client;
Console.WriteLine("Interface Injection ==>
Current Service : {0}", _client1.ServiceMethod());
}
}BusinessLogicImplementationInterfaceDI objInterfaceDI = new BusinessLogicImplementationInterfaceDI(); objInterfaceDI= new ClaimService(); objInterfaceDI.setServiceRunService(serviceObj);
以上就是如何在C#中使用基于接口的注入来实现依赖注入?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号