- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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 Property 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
Setter Injection
Getter and Setter Injection injects the dependency by using default public properties procedure such as Gettter(get(){}) and Setter(set(){}).
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"; } } public class BusinessLogicImplementation{ private IService _client; public IService Client{ get { return _client; } set { _client = value; } } public void SetterInj(){ Console.WriteLine("Getter and Setter Injection ==> Current Service : {0}", Client.ServiceMethod()); } }
Consuming
BusinessLogicImplementation ConInjBusinessLogic = new BusinessLogicImplementation(); ConInjBusinessLogic.Client = new ClaimService(); ConInjBusinessLogic.SetterInj();
Advertisements