sublimeaces
sublimeaces

Reputation: 79

Need help understanding the Dependency Inversion Principle in this context.

Just started working and someone mentioned this concept and I still don't understand. I initially thought it meant that the view should use a class IN BETWEEN your business logic class. So that the in between class will always return a default value even if the code behind fails.

They told me this was wrong and said it has more to do with using statements like using HTMLAGILITYPACK. How are you supposed to make it so that the business logic is not dependent on something as crucial to the program as HTMLAGILITYPACK? Does that mean I should import ANOTHER .dll that can search html and write an "in case htmlagilitypack fails somehow" use this .dll instead?

I don't understand. Thank you!

(program purpose) The program goes to a website and downloads some elements using htmlagilitypack and then fills classes with the info gained from the website.

Upvotes: 0

Views: 386

Answers (2)

Kyle B
Kyle B

Reputation: 2909

Dependency Inversion allows you to write code that doesn't depend on external dependencies.
To your question "Does that mean I should import ANOTHER .dll that can search html" is wrong, dependency if utilized properly should allow for you to bring in another DLL without any changes to your business logic code.

Consider the following method:

The below code has a dependency on the HTML Agility Pack. Both HtmlWeb, web.Load, and whatever web.load returns are dependencies.

public string GetDocumentInfo(string path)
{
    var web = new HtmlWeb();
    var doc = web.Load(path);
    // ... etc ... etc
}

Now consider a revision:

The below code only depends on the IDocumentReader interface (an interface that you can define).

public string GetDocumentInfo(string path, IDocumentReader reader)
{
    return reader.Read(path);
} 

The reason you would want to do this is so that later you can create another class that implements IDocumentReader that reads XML, or that reads .CSV files, etc. etc. Now your code just needs a class that implements the IDocumentReader interface and doesn't care what framework you use inside the class that implements it.

Upvotes: 1

yekanchi
yekanchi

Reputation: 843

you're understanding is wrong. There are tens of Tutorials around the web and it's not something that you can understand just by asking it here at StackOverFlow. you should read the software design pattern and architecture books. here i mention you most important aspects of Dependency Inversion Principle in my opinion but encourage you to read the following links for and understanding.

1- High Level Modules Should Not Depend On Low Level Modules

2- Abstraction Should Not Depend on Details, Details Should Depend on Abstractions.

for further reading and implementation techniques simply read these tutorials., they are nice and almost adequate for beginners.

Dependency Inversion Principle (provided with C# Examples)

Inversion of Control – An Introduction with Examples in .NET

Second link of course covers more comprehensive concepts related.

Upvotes: 1

Related Questions