- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Articles
- How to implement Dependency Injection using Property in C#?
- Dependency Injection in C#
- Explain dependency injection in C#
- What is dependency injection in PHP?
- What are the different ways to implement dependency injection and their advantages in C#?
- Difference between Dependency Injection and Factory Pattern.
- Difference between IOC and Dependency Injection in Spring.
- 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?
- What is dependency inversion principle and how to implement in C#?
- How to implement interface in anonymous class in C#?
- How to implement ObjLongConsumer interface using lambda expression in Java?

Advertisements