IOC ,  Dependecy Invertion

Inversion of Control

example :  DataAccessLayer with SQL and Oracle

The Dependency Inversion Principle (DIP) is one of the five SOLID principles of object-oriented design, and it aims to reduce the coupling between high-level and low-level modules by introducing an abstraction layer. This principle makes the system more modular, flexible, and easier to maintain.

Key Points of Dependency Inversion Principle

Benefits of Dependency Inversion Principle

Example

Let's consider an example where we have a high-level module OrderService that depends on a low-level module EmailService.

Without Dependency Inversion Principle:

csharp

public class EmailService

{

    public void SendEmail(string message)

    {

        // Send email logic

    }

}


public class OrderService

{

    private EmailService _emailService;


    public OrderService()

    {

        _emailService = new EmailService();

    }


    public void ProcessOrder()

    {

        // Order processing logic

        _emailService.SendEmail("Order processed successfully.");

    }

}


In this example, OrderService depends directly on the EmailService, creating tight coupling between the two classes.

With Dependency Inversion Principle:

csharp

public interface IEmailService

{

    void SendEmail(string message);

}


public class EmailService : IEmailService

{

    public void SendEmail(string message)

    {

        // Send email logic

    }

}


public class OrderService

{

    private readonly IEmailService _emailService;


    public OrderService(IEmailService emailService)

    {

        _emailService = emailService;

    }


    public void ProcessOrder()

    {

        // Order processing logic

        _emailService.SendEmail("Order processed successfully.");

    }

}


In this improved example, OrderService depends on the abstraction IEmailServ


Dependacy Inversion Principle

Core Concepts of DIP:

Benefits:

Example:

Imagine you have a high-level OrderProcessor class that depends on a low-level EmailService class to send confirmation emails. Without DIP, you might have something like this:

csharp

public class OrderProcessor

{

    private EmailService _emailService;


    public OrderProcessor()

    {

        _emailService = new EmailService();

    }


    public void ProcessOrder()

    {

        // Process order

        _emailService.SendEmail();

    }

}


This tightly couples OrderProcessor to EmailService. To apply DIP, you introduce an abstraction (interface):

csharp

public interface IEmailService

{

    void SendEmail();

}


public class EmailService : IEmailService

{

    public void SendEmail()

    {

        // Send email

    }

}


public class OrderProcessor

{

    private readonly IEmailService _emailService;


    public OrderProcessor(IEmailService emailService)

    {

        _emailService = emailService;

    }


    public void ProcessOrder()

    {

        // Process order

        _emailService.SendEmail();

    }

}


Now, OrderProcessor depends on the IEmailService interface rather than a concrete EmailService class, which follows the Dependency Inversion Principle.