將耦合(依賴)物件注入(轉換)為解耦(獨立)物件的過程稱為依賴注入。
依賴注入的型別
DI 有四個型別−
#建構子注入
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中文網其他相關文章!