กระบวนการฉีด (แปลง) วัตถุที่เป็นคู่ (ขึ้นอยู่กับ) ลงในวัตถุที่แยกออก (อิสระ) เรียกว่าการพึ่งพาการฉีด
ประเภทของการฉีดอ้างอิง
DI มีสี่ประเภท -
-
คอนสตรัคเตอร์ฉีด
-
เซ็ตเตอร์ฉีด
-
การฉีดตามอินเทอร์เฟซ
-
เครื่องฉีดระบุตำแหน่งบริการ
ฉีดอินเทอร์เฟซ
Interface Injection นั้นคล้ายกับ Getter และ Setter DI, Getter และ Setter DI ใช้ getter และ setter ที่เป็นค่าเริ่มต้น แต่ Interface Injection ใช้อินเตอร์เฟสการสนับสนุนซึ่งเป็นชนิดของ 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);