1 00:00:00,630 --> 00:00:02,300 So this is great. 2 00:00:02,300 --> 00:00:06,090 We've refactored our controller code to declare its single dependency, 3 00:00:06,090 --> 00:00:09,360 the repository class as a constructor parameter. 4 00:00:09,360 --> 00:00:13,230 But what's going to supply an instance of our repository to the controller? 5 00:00:13,230 --> 00:00:17,540 If we were instantiating instances of the controller ourselves, then we could supply 6 00:00:17,540 --> 00:00:21,880 the necessary dependency when we up the controller object instance. 7 00:00:21,880 --> 00:00:25,870 For instance, we could instantiate an instance of the context class. 8 00:00:29,214 --> 00:00:34,336 Then an instance of the repository class, passing in the instance of 9 00:00:34,336 --> 00:00:39,830 the context class, cuz that's the dependency of the repository class. 10 00:00:39,830 --> 00:00:45,338 Then we can construct an instance of our controller. 11 00:00:45,338 --> 00:00:49,230 PlanetsController, passing in our instance of the repository. 12 00:00:49,230 --> 00:00:53,230 But NVC is responsible for instantiating our controllers. 13 00:00:53,230 --> 00:00:55,080 So, this doesn't work. 14 00:00:55,080 --> 00:00:58,540 In fact, if we try running our app, we'll get an error. 15 00:00:59,950 --> 00:01:03,810 No parameterless constructor defined for this object. 16 00:01:03,810 --> 00:01:07,160 This error is telling us that if you see a tip to create an instance of 17 00:01:07,160 --> 00:01:07,770 our controller, 18 00:01:07,770 --> 00:01:12,940 it wasn't able to do so, because it no longer has a default constructor. 19 00:01:12,940 --> 00:01:15,790 A constructor without any parameters. 20 00:01:15,790 --> 00:01:18,340 We need something that can manage the creation and 21 00:01:18,340 --> 00:01:20,898 lifetime of our controllers' dependencies. 22 00:01:20,898 --> 00:01:26,450 To do that, we can integrate into our NVC app something called a DI container. 23 00:01:26,450 --> 00:01:30,060 There are many DI container options available for .NET. 24 00:01:30,060 --> 00:01:32,740 Here are some of the most popular libraries. 25 00:01:32,740 --> 00:01:35,970 The concepts of working with a DI container are more or 26 00:01:35,970 --> 00:01:39,790 less the same regardless of which you decide to use. 27 00:01:39,790 --> 00:01:44,150 In fact, most of the time, switching to a new container will just affect your 28 00:01:44,150 --> 00:01:47,140 app's configuration and startup code. 29 00:01:47,140 --> 00:01:50,340 When deciding which DI container to use in your project, 30 00:01:50,340 --> 00:01:53,430 let's consider the following selection criteria. 31 00:01:53,430 --> 00:01:56,940 Usability, or developer ease of use. 32 00:01:56,940 --> 00:02:02,020 Is the container easy for developers to install, configure, and use? 33 00:02:02,020 --> 00:02:03,170 Performance. 34 00:02:03,170 --> 00:02:06,560 Is the container fast enough for your specific situation? 35 00:02:06,560 --> 00:02:08,070 Documentation. 36 00:02:08,070 --> 00:02:11,590 Are the docs detailed and easy to follow? 37 00:02:11,590 --> 00:02:13,490 And viability. 38 00:02:13,490 --> 00:02:16,720 Is the library being regularly maintained and supported? 39 00:02:17,720 --> 00:02:21,880 In this workshop, we'll be using a simple injector for our DI container. 40 00:02:21,880 --> 00:02:26,830 It's easy to use, it's fast, and at the time of this recording, it's Repo and 41 00:02:26,830 --> 00:02:28,910 GitHub is actively being maintained. 42 00:02:29,920 --> 00:02:32,730 Next, let's install into our project and 43 00:02:32,730 --> 00:02:35,180 configure this simple injector DI container.