

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to implement dependency injection using Interface-based injection in C#?
The process of injecting (converting) coupled (dependent) objects into decoupled (independent) objects is called Dependency Injection.
Types of Dependency Injection
There are four types of DI −
Constructor Injection
Setter Injection
Interface-based injection
Service Locator Injection
Interface Injection
Interface Injection is similar to Getter and Setter DI, the Getter, and Setter DI use default getter and setter but Interface Injection uses support interface a kind of explicit getter and setter which sets the interface property.
Example
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()); } }
Consuming
BusinessLogicImplementationInterfaceDI objInterfaceDI = new BusinessLogicImplementationInterfaceDI(); objInterfaceDI= new ClaimService(); objInterfaceDI.setServiceRunService(serviceObj);
- Related Questions & Answers
- How to implement Dependency Injection using Property in C#?
- Dependency Injection in C#
- Explain dependency injection in C#
- What is dependency injection in PHP?
- Difference between Dependency Injection and Factory Pattern.
- Difference between IOC and Dependency Injection in Spring.
- What are the different ways to implement dependency injection and their advantages in C#?
- Difference Between Constructor Injection and Setter Injection in Spring
- How to Combat Injection Attacks?
- What is SQL Injection?
- What is Code Injection? (How it Works, How to Prevent)
- What is SQL injection? How can you prevent it?
- How to implement the ObjIntConsumer interface using lambda expression in Java?
- How to implement the Runnable interface using lambda expression in Java?
- What is dependency inversion principle and how to implement in C#?
Advertisements