1 00:00:07,260 --> 00:00:07,710 Hello. 2 00:00:08,490 --> 00:00:14,700 In this lecture, we're going to talk about Question 44, what is inversion of control? 3 00:00:15,150 --> 00:00:22,170 Inversion of control is the design approach, according to which the control flow of a program is inverted 4 00:00:22,500 --> 00:00:30,270 instead of the programmer controlling the flow of a program, the external sources so frameworks, services 5 00:00:30,270 --> 00:00:32,910 or other components take control of it. 6 00:00:33,550 --> 00:00:35,910 Let's consider two simple examples. 7 00:00:36,210 --> 00:00:40,200 First is a Cossar application interacting with the user. 8 00:00:46,220 --> 00:00:53,780 In this program, the cold is in control, it decides when the user answers the questions shown in the 9 00:00:53,780 --> 00:00:54,380 console. 10 00:00:55,190 --> 00:00:58,070 And here is the implementation of this program. 11 00:00:58,640 --> 00:01:01,010 Now let's see a different approach. 12 00:01:06,980 --> 00:01:14,030 In this application, the court doesn't control when exactly the user will fill the form and when the 13 00:01:14,030 --> 00:01:16,100 final message will be printed. 14 00:01:20,610 --> 00:01:26,850 The action of the user, so clicking on the submit button triggers the event of printing the output 15 00:01:26,850 --> 00:01:27,330 here. 16 00:01:28,020 --> 00:01:30,360 Let's see the code of this application. 17 00:01:35,040 --> 00:01:41,730 The control flow of the program is inverted compared to the traditional flow where the cold decides 18 00:01:41,730 --> 00:01:44,900 what exactly some action happens here. 19 00:01:44,940 --> 00:01:52,530 The framework in this case Windows Forms is in charge and it executes particular pieces of code based 20 00:01:52,530 --> 00:01:55,440 on the user's actions that trigger events. 21 00:01:56,100 --> 00:02:02,910 Inversion of Control is sometimes referred to as the Hollywood principle, which says, Don't call us. 22 00:02:03,030 --> 00:02:04,530 We will call you. 23 00:02:05,070 --> 00:02:11,640 In this case, we didn't call this method from the cold directly is the framework that called this method 24 00:02:11,640 --> 00:02:14,940 via an event that this method is subscribed to. 25 00:02:15,630 --> 00:02:22,740 According to Martin Fowler, who is the author of the great book Refactoring and in General Authority 26 00:02:22,740 --> 00:02:29,490 in topics of software development, design, etc. The invention of control is what makes the difference 27 00:02:29,640 --> 00:02:31,860 between a framework and a library. 28 00:02:32,370 --> 00:02:39,450 Mr. Fowler defines a library, as are essential set of functions that you can call these days. 29 00:02:39,510 --> 00:02:46,200 Usually organized in two classes, each call does some work and returns control to the client. 30 00:02:46,650 --> 00:02:53,610 On the other hand, a framework embodies some abstract design, which more behavior a routine in order 31 00:02:53,610 --> 00:02:54,390 to use it. 32 00:02:54,420 --> 00:03:00,990 You need to insert your behavior into various places in the framework, either by suppressing or by 33 00:03:00,990 --> 00:03:03,060 plugging in your own glasses. 34 00:03:03,690 --> 00:03:07,530 So this is exactly what happened in our Windows Phones application. 35 00:03:08,130 --> 00:03:13,770 It's the framework that handles the connection between the submit button and this method. 36 00:03:14,490 --> 00:03:17,880 There are many ways in which the control can be inverted. 37 00:03:18,330 --> 00:03:22,260 In the example we've seen, it was implemented by using events. 38 00:03:22,890 --> 00:03:29,640 Events were triggered by the user's actions on the interface, thus executing some particular methods 39 00:03:29,640 --> 00:03:30,450 in cold. 40 00:03:30,930 --> 00:03:36,630 Speaking more generally, the inversion of control happens when there are some kind of a callback is 41 00:03:36,630 --> 00:03:44,490 defined as a callback is an executable called so omitted in C Sharp that the gets passed as an argument 42 00:03:44,640 --> 00:03:45,840 to some other code. 43 00:03:46,560 --> 00:03:54,840 Let's consider this simple example the read line by line method uses a callback an action passed as 44 00:03:54,840 --> 00:03:55,680 a parameter. 45 00:03:56,220 --> 00:04:02,970 Once the entire input has been read, the callback will be executed in real life products. 46 00:04:03,120 --> 00:04:10,830 It often happens that after some data is read from a database API or anything else that takes some time 47 00:04:10,830 --> 00:04:18,150 to execute, a callback is invoked informing some other piece of the code that it can start its work 48 00:04:18,450 --> 00:04:21,690 as the data it requires is ready to be used. 49 00:04:22,140 --> 00:04:28,290 Another example of inversion of control could be the template method that we learned about earlier in 50 00:04:28,290 --> 00:04:29,070 the course. 51 00:04:29,550 --> 00:04:36,120 In the lecture about it, you mentioned the example of setup and teardown methods from a unique framework. 52 00:04:36,480 --> 00:04:40,950 It's another case when the framework calls the methods that we defined. 53 00:04:41,490 --> 00:04:48,300 The template is defined in the unit itself, where it is decided that first the setup must be called, 54 00:04:48,390 --> 00:04:51,540 then the actual test and then the teardown. 55 00:04:51,810 --> 00:04:56,310 But the actual implementation of those steps is defined by the programmer. 56 00:04:57,000 --> 00:05:01,050 Dependency injection is another example of inversion of control. 57 00:05:01,380 --> 00:05:06,630 The code that some class needs to execute is injected from the outside. 58 00:05:07,020 --> 00:05:10,770 We don't have control over what exactly will be called. 59 00:05:10,920 --> 00:05:17,010 The decision is made for us by someone who provides the concrete type as the constructor parameter. 60 00:05:17,370 --> 00:05:20,520 We only declare that we need some dependency. 61 00:05:21,060 --> 00:05:24,550 Using an interface is similar to having a callback. 62 00:05:25,110 --> 00:05:32,460 It would actually be possible to have dependency injection we interfaces, but by simply providing the 63 00:05:32,460 --> 00:05:35,670 dependencies as funks that can be executed. 64 00:05:36,090 --> 00:05:43,590 For example, instead of having some people data reader interface, we could just have a func that returns 65 00:05:43,590 --> 00:05:44,820 a collection of people. 66 00:05:45,450 --> 00:05:51,900 Let's summarize Inversion of control is the design approach, according to which the control flow of 67 00:05:51,900 --> 00:05:57,600 the program is inverted instead of a programmer controlling the flow of a program. 68 00:05:57,810 --> 00:06:05,100 The external sources like frameworks, services or other components, take control of it when discussing 69 00:06:05,100 --> 00:06:06,990 this topic during the interview. 70 00:06:07,020 --> 00:06:09,570 You can be asked What is a callback? 71 00:06:09,960 --> 00:06:16,770 A callback is an executable code that gets passed as an argument to some other code in C Sharp. 72 00:06:16,770 --> 00:06:20,010 It's usually implemented we funk's or actions. 73 00:06:20,760 --> 00:06:24,090 What is the difference between a framework and a library? 74 00:06:24,810 --> 00:06:28,440 A library is just a set of functions that we can reuse. 75 00:06:28,800 --> 00:06:34,380 An example of a library could be some set of classes providing some mathematical operations. 76 00:06:34,440 --> 00:06:42,870 It's a framework embodies some abstract design with moral behavior in when using a framework, we plug 77 00:06:42,870 --> 00:06:45,480 our own classes and methods into it. 78 00:06:45,960 --> 00:06:51,300 It's the frameworks responsibility to call those classes and methods are proper ones. 79 00:06:51,630 --> 00:06:57,420 So in short, the framework relies on inversion of control, but the library does not. 80 00:06:57,960 --> 00:07:00,660 All right, let's eat in this lecture. 81 00:07:01,140 --> 00:07:03,990 Thanks for watching, and I'll see you in the next one.