0% found this document useful (0 votes)
5 views1 page

Understanding Dependency Inversion Principle

The Dependency Inversion Principle states that high-level modules should not depend on low-level modules, both should depend on abstractions. The code shows an interface IReader, a concrete FileReader class implementing IReader, and a DataProcessor class depending on the IReader abstraction rather than the concrete FileReader class.

Uploaded by

Arpan Saha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

Understanding Dependency Inversion Principle

The Dependency Inversion Principle states that high-level modules should not depend on low-level modules, both should depend on abstractions. The code shows an interface IReader, a concrete FileReader class implementing IReader, and a DataProcessor class depending on the IReader abstraction rather than the concrete FileReader class.

Uploaded by

Arpan Saha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Dependency Inversion Principle

public interface IReader


{
public string Read();
}

public class FileReader:IReader


{
public string Read()
{
return "Data Read from file";
}
}

//business logic depends on abstraction IReader


public class DataProcessor
{

private readonly IReader _reader


//constructor injection
public DataProcessor(IReader reader)
{
_reader=reader;
}

public void ProcessData()


{
string data= _reader.Read();
[Link](" Data read :"+data);
}
}

public class Driver


{

public static void Main(string[]args)


{
// initialising the file reader
IReader reader = new FileReader();

DataProcessor dp = new DataProcessor(reader);


[Link]();

You might also like