Hello.
In this lecture, we're going to talk about Question 44, what is inversion of control?
Inversion of control is the design approach, according to which the control flow of a program is inverted
instead of the programmer controlling the flow of a program, the external sources so frameworks, services
or other components take control of it.
Let's consider two simple examples.
First is a Cossar application interacting with the user.
In this program, the cold is in control, it decides when the user answers the questions shown in the
console.
And here is the implementation of this program.
Now let's see a different approach.
In this application, the court doesn't control when exactly the user will fill the form and when the
final message will be printed.
The action of the user, so clicking on the submit button triggers the event of printing the output
here.
Let's see the code of this application.
The control flow of the program is inverted compared to the traditional flow where the cold decides
what exactly some action happens here.
The framework in this case Windows Forms is in charge and it executes particular pieces of code based
on the user's actions that trigger events.
Inversion of Control is sometimes referred to as the Hollywood principle, which says, Don't call us.
We will call you.
In this case, we didn't call this method from the cold directly is the framework that called this method
via an event that this method is subscribed to.
According to Martin Fowler, who is the author of the great book Refactoring and in General Authority
in topics of software development, design, etc. The invention of control is what makes the difference
between a framework and a library.
Mr. Fowler defines a library, as are essential set of functions that you can call these days.
Usually organized in two classes, each call does some work and returns control to the client.
On the other hand, a framework embodies some abstract design, which more behavior a routine in order
to use it.
You need to insert your behavior into various places in the framework, either by suppressing or by
plugging in your own glasses.
So this is exactly what happened in our Windows Phones application.
It's the framework that handles the connection between the submit button and this method.
There are many ways in which the control can be inverted.
In the example we've seen, it was implemented by using events.
Events were triggered by the user's actions on the interface, thus executing some particular methods
in cold.
Speaking more generally, the inversion of control happens when there are some kind of a callback is
defined as a callback is an executable called so omitted in C Sharp that the gets passed as an argument
to some other code.
Let's consider this simple example the read line by line method uses a callback an action passed as
a parameter.
Once the entire input has been read, the callback will be executed in real life products.
It often happens that after some data is read from a database API or anything else that takes some time
to execute, a callback is invoked informing some other piece of the code that it can start its work
as the data it requires is ready to be used.
Another example of inversion of control could be the template method that we learned about earlier in
the course.
In the lecture about it, you mentioned the example of setup and teardown methods from a unique framework.
It's another case when the framework calls the methods that we defined.
The template is defined in the unit itself, where it is decided that first the setup must be called,
then the actual test and then the teardown.
But the actual implementation of those steps is defined by the programmer.
Dependency injection is another example of inversion of control.
The code that some class needs to execute is injected from the outside.
We don't have control over what exactly will be called.
The decision is made for us by someone who provides the concrete type as the constructor parameter.
We only declare that we need some dependency.
Using an interface is similar to having a callback.
It would actually be possible to have dependency injection we interfaces, but by simply providing the
dependencies as funks that can be executed.
For example, instead of having some people data reader interface, we could just have a func that returns
a collection of people.
Let's summarize Inversion of control is the design approach, according to which the control flow of
the program is inverted instead of a programmer controlling the flow of a program.
The external sources like frameworks, services or other components, take control of it when discussing
this topic during the interview.
You can be asked What is a callback?
A callback is an executable code that gets passed as an argument to some other code in C Sharp.
It's usually implemented we funk's or actions.
What is the difference between a framework and a library?
A library is just a set of functions that we can reuse.
An example of a library could be some set of classes providing some mathematical operations.
It's a framework embodies some abstract design with moral behavior in when using a framework, we plug
our own classes and methods into it.
It's the frameworks responsibility to call those classes and methods are proper ones.
So in short, the framework relies on inversion of control, but the library does not.
All right, let's eat in this lecture.
Thanks for watching, and I'll see you in the next one.