Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The Dependency Inversion Principle with .NET Framework classes

I'm trying to understand SOLID principles, in particular The Dependency Inversion Principle.

In this is SO answer it is explained very well.

I think I have understood that I can't create any instance of a class inside my class. Is it right?

But if I have to save to disk some content, can I create an instance of System.IO.File or do I have to inject it?

I don't understand where is the limit, if I can't instance my own classes or if I can't either instance .NET Framework classes (or whatever other framework).

UPDATE:
I think File is a bad example because is declared as static.

By the way, does this principle apply to static classes?

like image 454
VansFannel Avatar asked Jul 20 '17 11:07

VansFannel


People also ask

Why do we need dependency inversion C#?

Benefits of Implementing the Dependency Inversion Principle Our classes are not tightly coupled with the lower-tier objects and we can easily reuse the logic from the high-tier modules. So, the main reason why DIP is so important is the modularity and reusability of the application modules.

What is the best description of the Dependency Inversion Principle?

The Dependency Inversion Principle (DIP) states that high level modules should not depend on low level modules; both should depend on abstractions. Abstractions should not depend on details.

What is Dependency Injection in C# with example?

Dependency Injection (DI) is a software design pattern that allows us to develop loosely coupled code. DI is a great way to reduce tight coupling between software components. DI also enables us to better manage future changes and other complexity in our software.

What is Inversion of Control in C# with example?

Inversion of Control (IoC) in C#: The main objective of Inversion of Control (IoC) in C# is to remove the dependencies (remove tight coupling) between the objects of an application which makes the application more decoupled and maintainable.


1 Answers

The S of SOLID stands for SRP (Single Responsibility Principle). You won't violate it by using System.IO.File inside a class directly, once you keep that class with one single responsibility.

It's a good idea trying to abstract the purpose behind using System.IO.File. Let's suppose you need it to generate a log. Then you would probably do something like:

public interface IMyLogger
{
    void GenerateLog(IEnumerable<string> content);
}

public class FileLogger: IMyLogger
{
    public void GenerateLog(IEnumerable<string> content)
    {
        System.IO.File.WriteAllLines("C:/Log", content);
    }
}

Maybe it's not just a log, it's something more important, like generating a file so other system/app (even external) read it and do some job.

If you are trying to use a DDD approach, the interface could belong to your domain, and the implementation could belong in the application. Then you register your interface as a service and inject it.

The class which needs an IMyLogger actually doesn't need to know how is the log being generated, it just needs the job to be done.

You can apply the same idea when you need to send an email inside some business logic in your domain. Instead of making a connection to an Exchange inside your domain directly, create an interface INotifier and a MailNotifier implementing it to be injected.

like image 168
Alisson Reinaldo Silva Avatar answered Nov 06 '22 07:11

Alisson Reinaldo Silva