
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
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 Articles
- What is Liskov Substitution principle and how to implement in C#?
- What is Interface segregation principle and how to implement it in C#?
- How to implement Dependency Injection using Property in C#?
- How to implement dependency injection using Interface-based injection in C#?
- What are the different ways to implement dependency injection and their advantages in C#?
- How to implement Single Responsibility Principle using C#?
- How to implement Open Closed principle using C#?
- What is functional dependency and transitive dependency (DBMS)?
- What is Facade and how to implement in C#?
- What is proxy design pattern and how to implement it in C#?
- What is Data Dependency?
- C++ Program to Count Inversion in an Array
- What is functional dependency in DBMS?
- What is Transitive dependency in DBMS?
- What is multivalued dependency in DBMS?

Advertisements