Inversion of control (introduction of IoC / dependencies) for dummies

Possible duplicate:
What is control inversion?

Well, I'm new to this site, and I saw that people are really ready to help, so Imma will take advantage of this and just ask another question if you don't mind.

So, I read a lot, I swear, BUT, I just can't figure it out. WHAT Is there an inversion of control in the world (IoC or dependency injection)? Why do ASP.NET MVC + Repository Pattern projects use it so much? And finally, what do they mean by “containers” and when do they say “Inject my Controllers”?

I know that this may be an old topic (or even a stupid question), but I just can’t get answers to “mannequins”.

+4
source share
2 answers

Think of dependency injection / control inversion a bit more than a large factory object, a declarative, configuration-driven virtual constructor. Instead of clogging your code with calls to the “new” hardwire type that your client class uses, you now have that virtual constructor that creates the objects for you.

What advantage is that all this complexity is buying you?

Creating an object is now declarative. If you base your design on the appropriate interfaces, you can ask the factory to create a proxy server that implements the same interface when convenient. All sorts of good things are now possible: aspect-oriented programming, transparent remote operations, declarative transactions, etc.

+4
source

The simple answer: it allows you to pass on the “things” that any given object will use to do its job.

A permeable example: say that the object wants to get time for some purpose, you pass it "ITimeService" and calls it "GetTime" for this.

The goal of this is to “de-para” from the class to have a strong relationship with things you cannot wish for and to help test.

In my humble opinion, some people are mistaken, but the testing argument is valid, and, of course, this is an approach that may be useful in due time.

A more attractive answer: Martin Fowler for inversion of control .

+3
source

Source: https://habr.com/ru/post/1332697/


All Articles