

- 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
What is dependency inversion principle and how to implement in C#?
High-level modules should not depend on low-level modules. Both should depend on abstractions.Abstractions should not depend on details. Details should depend on abstractions.This principle is primarily concerned with reducing dependencies among the code modules.
Example
Code Before Dependency Inversion
using System; namespace SolidPrinciples.Dependency.Invertion.Before{ public class Email{ public string ToAddress { get; set; } public string Subject { get; set; } public string Content { get; set; } public void SendEmail(){ //Send email } } public class SMS{ public string PhoneNumber { get; set; } public string Message { get; set; } public void SendSMS(){ //Send sms } } public class Notification{ private Email _email; private SMS _sms; public Notification(){ _email = new Email(); _sms = new SMS(); } public void Send(){ _email.SendEmail(); _sms.SendSMS(); } } }
Code After Dependency Inversion
using System.Collections.Generic; namespace SolidPrinciples.Dependency.Invertion.Before{ public interface IMessage{ void SendMessage(); } public class Email: IMessage{ public string ToAddress { get; set; } public string Subject { get; set; } public string Content { get; set; } public void SendMessage(){ //Send email } } public class SMS: IMessage{ public string PhoneNumber { get; set; } public string Message { get; set; } public void SendMessage(){ //Send Sms } } public class Notification{ private ICollection<IMessage> _messages; public Notification(ICollection<IMessage> messages){ this._messages = messages; } public void Send(){ foreach (var message in _messages){ message.SendMessage(); } } } }
- Related Questions & Answers
- What is Liskov Substitution principle and how to implement in C#?
- What is Interface segregation principle and how to implement it in C#?
- What is functional dependency and transitive dependency (DBMS)?
- How to implement Dependency Injection using Property in C#?
- How to implement Single Responsibility Principle using C#?
- How to implement Open Closed principle using C#?
- What are the different ways to implement dependency injection and their advantages in C#?
- What is Facade and how to implement in C#?
- How to implement dependency injection using Interface-based injection in C#?
- What is Data Dependency?
- What is Multivalued Dependency (DBMS)?
- What is dependency injection in PHP?
- What is functional dependency in DBMS?
- What is Transitive dependency in DBMS?
- What is multivalued dependency in DBMS?
Advertisements