1 00:00:00,240 --> 00:00:04,139 We can use the NuGet package manager to add the necessary package for 2 00:00:04,139 --> 00:00:05,241 our DI container. 3 00:00:05,241 --> 00:00:09,027 Right click on the project and I select Manage NugGet Packages. 4 00:00:10,741 --> 00:00:13,777 If the browse tab isn't selected go ahead and click on it. 5 00:00:13,777 --> 00:00:18,338 Simple injector provides a variety of quick start packages including one 6 00:00:18,338 --> 00:00:18,933 for MVC. 7 00:00:18,933 --> 00:00:25,700 To find it search for simpleinjector.mvc. 8 00:00:25,700 --> 00:00:28,220 The package that we're looking for 9 00:00:28,220 --> 00:00:33,700 is the Simpleinjector.MVC3 package it's kind of a confusing name. 10 00:00:33,700 --> 00:00:37,690 Even though it says MVC3 it's compatible with MVC5 which 11 00:00:37,690 --> 00:00:42,569 is the version of MVC that we're using go ahead and install the package. 12 00:00:51,335 --> 00:00:55,729 Installing the NuGat package adds some assembly references to our project, 13 00:00:57,344 --> 00:01:02,930 Including three assemblies related to SimpleInjector, 14 00:01:02,930 --> 00:01:06,579 and an assembly named WebActivator. 15 00:01:06,579 --> 00:01:10,359 The NuGat package also added a code file to our project 16 00:01:10,359 --> 00:01:14,813 In the App_Start folder named SimpleInjectorInitializer. 17 00:01:14,813 --> 00:01:19,010 This file contains the code that's being used to configure our DI container. 18 00:01:20,200 --> 00:01:26,489 This assembly attribute is being used to register our SimpleInjectorInitializer, 19 00:01:26,489 --> 00:01:31,917 Initialize method, to be called right after our application starts up. 20 00:01:31,917 --> 00:01:35,483 By using this method, SimpleInjector avoided having to 21 00:01:35,483 --> 00:01:40,730 modify our project's application start method in the global.asaxcs file. 22 00:01:40,730 --> 00:01:44,730 See the teacher's notes for more information about web activator. 23 00:01:44,730 --> 00:01:47,589 The Initialize method is use to instantiate and 24 00:01:47,589 --> 00:01:49,454 initialize our DI container. 25 00:01:49,454 --> 00:01:52,360 The first line of code instantiates the DI container. 26 00:01:52,360 --> 00:01:56,780 And the second line of code sets the DefaultScopedLifestyle 27 00:01:56,780 --> 00:01:59,830 to an instance of the class WebRequestLifestyle. 28 00:01:59,830 --> 00:02:02,590 Don't worry too much about what this line of code is doing right now, 29 00:02:02,590 --> 00:02:07,500 we'll talk more about lifestyles, and what the scope lifestyle is, in just a bit. 30 00:02:07,500 --> 00:02:10,566 Then, the InitializeContainer method is called, 31 00:02:10,566 --> 00:02:13,013 passing in an instance of the container. 32 00:02:13,013 --> 00:02:17,582 The InitializeContainer method is defined down here at the bottom of our class. 33 00:02:17,582 --> 00:02:21,295 We'll use this method to register our dependencies with the DI container. 34 00:02:22,675 --> 00:02:27,235 Notice that simple injector is referring to our dependencies as services. 35 00:02:27,235 --> 00:02:30,785 This is a commonly used term for dependencies as 36 00:02:30,785 --> 00:02:34,735 they typically provide services for other classes throughout our application. 37 00:02:34,735 --> 00:02:35,675 I'm gonna go ahead and 38 00:02:35,675 --> 00:02:40,200 remove this reminder, we'll come back to this method in just a bit. 39 00:02:40,200 --> 00:02:43,050 After calling the initialized container method, 40 00:02:43,050 --> 00:02:46,610 a method on the container is called RegisterMvcControllers. 41 00:02:47,850 --> 00:02:52,316 We pass into that method a reference to our current executing assembly. 42 00:02:52,316 --> 00:02:56,730 This is the assembly for our ASP.NET MVC application. 43 00:02:56,730 --> 00:03:01,400 This method is being used to discover all of the controllers in our application and 44 00:03:01,400 --> 00:03:04,300 then it registers those controllers with a DI container. 45 00:03:05,410 --> 00:03:09,960 After the controllers are registered, the verify method is called on the container. 46 00:03:09,960 --> 00:03:14,120 This method, as it sounds like, verifies the configuration of the container 47 00:03:14,120 --> 00:03:16,970 if any errors are found, an exception will be thrown. 48 00:03:16,970 --> 00:03:21,780 This last line of code sets the NVC's DependencyResolver to an instance 49 00:03:21,780 --> 00:03:26,090 of the SimpleInjectorDependencyResolver class, passing in our container. 50 00:03:27,190 --> 00:03:32,220 The DependencyResolver is responsible for locating dependencies in the DI container. 51 00:03:32,220 --> 00:03:35,380 For example, when the container is instantiating an instance of 52 00:03:35,380 --> 00:03:39,950 the Repository class it'll detect that it needs an instance of the context class 53 00:03:39,950 --> 00:03:43,132 to pass to the repositories class constructor. 54 00:03:43,132 --> 00:03:47,720 The DependencyResolver will be used to resolve the context class dependency 55 00:03:47,720 --> 00:03:51,150 to an instance of that class, contained within the DI container. 56 00:03:52,220 --> 00:03:57,290 Not only is the DependencyResolver used to resolve dependencies between our classes, 57 00:03:57,290 --> 00:04:01,340 but it's also used by NVC to resolve controller dependencies. 58 00:04:01,340 --> 00:04:04,920 Once NVC has mapped the route to a specific controller 59 00:04:04,920 --> 00:04:09,510 it'll ask the DependencyResolver for an instance of that controller class. 60 00:04:09,510 --> 00:04:13,855 That's why it was necessary to register our controllers with a DI container. 61 00:04:13,855 --> 00:04:18,085 Now that we've reviewed the boiler plate DI container initialization code in 62 00:04:18,085 --> 00:04:22,900 the initialized method let',s update the InitializeContainer method. 63 00:04:22,900 --> 00:04:27,240 We'll use the containers register method to register the types in our application 64 00:04:27,240 --> 00:04:30,490 that we want the DI container to manage as dependencies. 65 00:04:30,490 --> 00:04:34,680 When calling the register method we specify the type that want to register 66 00:04:34,680 --> 00:04:38,690 as the methods to net type parameter, we also need to add the namespace for 67 00:04:38,690 --> 00:04:40,190 the context class. 68 00:04:40,190 --> 00:04:43,782 Which is, using Treehouse.SolarSystem.Data. 69 00:04:46,165 --> 00:04:49,557 And we pass in a lifestyle enumeration value. 70 00:04:52,729 --> 00:04:56,374 The Lifestyle enumeration value that we pass to the Register method, 71 00:04:56,374 --> 00:05:00,360 tells the container how many instances of a type should be created. 72 00:05:00,360 --> 00:05:03,800 And how long each of those instances should live, 73 00:05:03,800 --> 00:05:08,530 simple injector provides three object lifetime types that we can choose from 74 00:05:08,530 --> 00:05:10,350 when we register our dependencies. 75 00:05:10,350 --> 00:05:14,800 With the transient lifetime a new instance is created every time that 76 00:05:14,800 --> 00:05:16,573 an instance is requested. 77 00:05:16,573 --> 00:05:20,790 With the scope lifetime for every request within an implicitly or 78 00:05:20,790 --> 00:05:24,931 explicitly defined scope a single instance will be returned and 79 00:05:24,931 --> 00:05:28,660 that instance will be disposed when the scope ends. 80 00:05:28,660 --> 00:05:33,691 And with the Singleton lifetime, only one instance is created per container. 81 00:05:33,691 --> 00:05:37,470 You can think of Singletons as global object instances. 82 00:05:37,470 --> 00:05:39,290 In our container configuration, 83 00:05:39,290 --> 00:05:44,130 we set our default scope lifetime to an instance of the WebRequestLifestyle class. 84 00:05:44,130 --> 00:05:48,670 By doing this, each WebRequest will be implicitly defined as a scope. 85 00:05:48,670 --> 00:05:53,250 So registry dependencies with a ScopedLifestyle will mean that an instance 86 00:05:53,250 --> 00:05:56,330 will be instantiated at the beginning of a request and 87 00:05:56,330 --> 00:06:02,310 shared across any object servicing that request that are dependent on that type. 88 00:06:02,310 --> 00:06:06,355 When the request ends, the container will dispose of the instance. 89 00:06:06,355 --> 00:06:08,567 Let's finish registering our dependencies. 90 00:06:11,988 --> 00:06:16,985 It's typical to have a single database context and repository instance for 91 00:06:16,985 --> 00:06:18,005 each request. 92 00:06:18,005 --> 00:06:22,266 So let's add a data dependencies as Scope dependencies. 93 00:06:22,266 --> 00:06:26,358 So we added the context class but now let's add the repository class. 94 00:06:31,448 --> 00:06:34,882 And that's it these are our applications to dependencies. 95 00:06:34,882 --> 00:06:37,484 So now let's test our DI container. 96 00:06:37,484 --> 00:06:38,868 Before we run the application, 97 00:06:38,868 --> 00:06:41,386 let's set a break point in our controller's constructor 98 00:06:51,299 --> 00:06:55,869 Then let's run the application and, we hit our break point. 99 00:06:55,869 --> 00:06:58,693 This is the first of two times that we hit our break point. 100 00:06:58,693 --> 00:07:03,533 This first time is when the configuration of our container is being verified. 101 00:07:03,533 --> 00:07:08,090 If we continue execution we'll hit our break point a second time. 102 00:07:08,090 --> 00:07:11,870 If we hover over the repository parameter we can see 103 00:07:11,870 --> 00:07:14,320 that it has a reference to a repository object. 104 00:07:15,760 --> 00:07:17,996 And if we expand the repository object, 105 00:07:17,996 --> 00:07:22,292 we can see that its context private field has a reference to a context object. 106 00:07:24,800 --> 00:07:27,980 And if we continue execution, here's out list of planets. 107 00:07:29,260 --> 00:07:32,980 Congrats, you've just configured DI for our NVC application. 108 00:07:34,030 --> 00:07:38,290 As our application grows, we can continue to add new dependencies to our DI 109 00:07:38,290 --> 00:07:43,032 containers configuration and declare dependencies via our class constructors. 110 00:07:43,032 --> 00:07:46,914 It's a clean, efficient way to manage our app's dependencies. 111 00:07:46,914 --> 00:07:52,640 Adding a DI container to an ASP.NET MVC application isn't difficult to do. 112 00:07:52,640 --> 00:07:57,590 But ASP.NET Core makes it even simpler, let's see how next.