1 00:00:00,004 --> 00:00:01,787 Unlike ASP.NET in VC, 2 00:00:01,787 --> 00:00:07,470 the ASP.NET core web framework provides a built in DI container. 3 00:00:07,470 --> 00:00:09,960 Let's add an ASP.NET core project to our solution, 4 00:00:09,960 --> 00:00:13,550 and see how the built in DI container works. 5 00:00:13,550 --> 00:00:17,550 To add a new project to our solution, right click on the solution, and 6 00:00:17,550 --> 00:00:20,290 select Add > New Project. 7 00:00:22,060 --> 00:00:25,663 Then, to add a ASP.NET Core application to our project, 8 00:00:25,663 --> 00:00:29,500 select the ASP.NET Core web application project template. 9 00:00:31,650 --> 00:00:37,282 I'll name my project Treehouse.AspNetCore. 10 00:00:41,900 --> 00:00:47,329 At the top of the second dialogue, be sure you have .NET Core selected, 11 00:00:47,329 --> 00:00:49,921 and select ASP.NET Core 2.0. 12 00:00:49,921 --> 00:00:55,190 And then select the Web Application (Model View Controller) project template. 13 00:00:55,190 --> 00:00:59,400 This will create an ASP.NET Core MVC styled application. 14 00:00:59,400 --> 00:01:04,323 I'm also not including any authentication, and I'm not enabling Docker support. 15 00:01:07,100 --> 00:01:09,204 Now, let's add our service. 16 00:01:13,460 --> 00:01:16,549 I'll start by adding a folder named Services. 17 00:01:21,660 --> 00:01:23,867 And then, add in a class to that folder. 18 00:01:28,600 --> 00:01:33,740 Let's name our class TestService. 19 00:01:33,740 --> 00:01:37,910 And then in our class, let's add a method named GetAboutContent. 20 00:01:39,450 --> 00:01:45,784 It could be public, and let's use string for the return type, GetAboutContent. 21 00:01:48,445 --> 00:01:51,060 And then, let's return a string literal. 22 00:01:51,060 --> 00:01:56,382 How about, Here's the About view 23 00:01:56,382 --> 00:02:02,628 content from the TestService service! 24 00:02:05,980 --> 00:02:08,780 Now, we need to configure our service. 25 00:02:08,780 --> 00:02:12,510 To do that, we need to open the StartUp file in the root of our project. 26 00:02:14,570 --> 00:02:17,300 The StartUp class contains two methods. 27 00:02:19,400 --> 00:02:22,770 A Configure method that's used to configure the middleware pipeline for 28 00:02:22,770 --> 00:02:27,700 our application, and a method named ConfigureServices that, 29 00:02:27,700 --> 00:02:31,190 as the name implies, is used to configure the services for our application. 30 00:02:32,710 --> 00:02:35,900 The IServiceCollection interface provides three methods for 31 00:02:35,900 --> 00:02:38,840 add in dependencies to the build in DI container. 32 00:02:38,840 --> 00:02:43,990 AddTransient, which adds a service with the Transient lifestyle. 33 00:02:43,990 --> 00:02:48,473 AddSingleton which adds a service with the Singleton lifestyle. 34 00:02:48,473 --> 00:02:54,400 And AddScoped, which adds a service with the Scope lifestyle. 35 00:02:55,520 --> 00:03:00,558 Let's add our service using the Scope lifetime, services.AddScoped. 36 00:03:00,558 --> 00:03:05,740 And this is a generic method, so we supply TestService as the generic parameter. 37 00:03:08,410 --> 00:03:13,470 Don't forget to add the name space to using AspNetCore services, 38 00:03:13,470 --> 00:03:15,070 and include a set of parenthesis. 39 00:03:16,200 --> 00:03:17,411 So far in this workshop, 40 00:03:17,411 --> 00:03:20,420 we've been registering concrete types of our DI container. 41 00:03:20,420 --> 00:03:22,871 But, you could also register interfaces, and 42 00:03:22,871 --> 00:03:27,480 specify what concrete type should be instantiated for that interface. 43 00:03:27,480 --> 00:03:31,799 To see this in action, let's add an ITestService interface to our project. 44 00:03:37,052 --> 00:03:40,428 I'll right-click on the Services folder and say Add > Class, 45 00:03:40,428 --> 00:03:43,230 even though we're adding a service. 46 00:03:43,230 --> 00:03:48,274 I'll name this class or interface ITestService. 47 00:03:53,940 --> 00:04:00,510 Change class to interface, and add our GetAboutContent method. 48 00:04:01,950 --> 00:04:06,060 Remember, string is the return type, and then GetAboutContent. 49 00:04:07,280 --> 00:04:09,840 Remember that an interface is a code contract. 50 00:04:09,840 --> 00:04:14,110 It specifies what members, properties, and methods a class should implement. 51 00:04:14,110 --> 00:04:17,530 But, it doesn't tell the class how to implement those members. 52 00:04:17,530 --> 00:04:22,988 Now, let's update our service to implement this interface. 53 00:04:22,988 --> 00:04:25,480 : ITestService. 54 00:04:25,480 --> 00:04:26,363 It's that simple. 55 00:04:28,720 --> 00:04:32,052 Our TestService class implements the GetAboutContent method by returning 56 00:04:32,052 --> 00:04:33,400 a string literal. 57 00:04:33,400 --> 00:04:37,600 But another implementation might get this content from a database, or 58 00:04:37,600 --> 00:04:39,980 even an external third party service. 59 00:04:39,980 --> 00:04:43,782 Now that we have our interface, let's update our DI container configuration. 60 00:04:49,130 --> 00:04:53,440 To do that, we need to add another generic type parameter for our interface. 61 00:04:53,440 --> 00:04:58,183 This is telling the DI container that when an instance of the ITestService interface 62 00:04:58,183 --> 00:05:01,690 is needed, to use the TestService concrete type. 63 00:05:01,690 --> 00:05:05,810 Registering interfaces with our DI container is a powerful concept. 64 00:05:05,810 --> 00:05:07,460 To change what concrete type is used for 65 00:05:07,460 --> 00:05:11,630 this interface, we just need to change this call to the AddScoped method, 66 00:05:11,630 --> 00:05:14,680 by changing our second generic type parameter. 67 00:05:14,680 --> 00:05:18,910 After doing that, every class that is dependent on the ITestService interface 68 00:05:18,910 --> 00:05:20,920 would get that new concrete type. 69 00:05:20,920 --> 00:05:23,540 Now, let's inject the service into our controller. 70 00:05:25,160 --> 00:05:27,430 To start, let's add a private field. 71 00:05:28,520 --> 00:05:33,970 Private, and then for the type, we want to use our interface, ITestService. 72 00:05:35,390 --> 00:05:38,996 Don't forget, we also need to add a using statement for 73 00:05:38,996 --> 00:05:42,460 the Treehouse.AspNetCore.Services name space. 74 00:05:42,460 --> 00:05:47,118 For the name of the private field, let's just use _testService, and 75 00:05:47,118 --> 00:05:48,290 set that to null. 76 00:05:49,600 --> 00:05:55,410 We also need to add a constructor, public HomeController, 77 00:05:55,410 --> 00:05:58,710 and then remember, we're using constructor injection here. 78 00:05:58,710 --> 00:06:02,890 So we need a parameter to declare our dependency. 79 00:06:02,890 --> 00:06:09,630 ITestService, and then let's just give this a name, testService. 80 00:06:09,630 --> 00:06:11,831 Then in the body of the constructor, 81 00:06:11,831 --> 00:06:15,595 let's set the private field _testService to the parameter. 82 00:06:17,880 --> 00:06:23,044 Then, down at the about action method, we can replace this 83 00:06:23,044 --> 00:06:28,007 string literal that's setting the ViewData Message key 84 00:06:28,007 --> 00:06:32,990 to a call to our testService.GetAboutContent method. 85 00:06:32,990 --> 00:06:34,730 Now, let's test our application. 86 00:06:35,828 --> 00:06:37,860 And if you're like me, 87 00:06:37,860 --> 00:06:40,460 sometimes you make the mistake of starting the wrong application. 88 00:06:41,760 --> 00:06:42,760 Let's stop this app. 89 00:06:43,850 --> 00:06:47,508 And in the solution explorer, we need to right click on our new project, 90 00:06:47,508 --> 00:06:53,330 Treehouse.AspNetCore, and select Set as StartUp Project. 91 00:06:53,330 --> 00:06:56,920 Then, press F5 to start the application again. 92 00:06:56,920 --> 00:06:59,128 And here's our website. 93 00:06:59,128 --> 00:07:04,613 Let's browse to the about page And 94 00:07:04,613 --> 00:07:07,670 here' s the content from our TestService. 95 00:07:09,380 --> 00:07:10,340 The native support for 96 00:07:10,340 --> 00:07:15,662 DI is one of the many features that makes ASP.NET Core a joy to use. 97 00:07:15,662 --> 00:07:19,880 The built-in DI container provides the functionality that will cover the majority 98 00:07:19,880 --> 00:07:23,000 of your use cases that you're likely to encounter. 99 00:07:23,000 --> 00:07:26,520 If you find that you have a requirement that the built-in DI container 100 00:07:26,520 --> 00:07:30,570 can't satisfy, then you can replace it with a third party library. 101 00:07:30,570 --> 00:07:32,300 See the teacher's notes for more information. 102 00:07:33,610 --> 00:07:35,640 Now that you've learned what DI is and 103 00:07:35,640 --> 00:07:39,760 how to use it, you might be wondering, when should I use it? 104 00:07:39,760 --> 00:07:44,260 As you've seen, adding a DI container to an ASP.NET MVC application 105 00:07:44,260 --> 00:07:49,340 isn't difficult to do, but for some small projects might be overkill. 106 00:07:49,340 --> 00:07:52,680 As with most things in software development, weigh the pros and 107 00:07:52,680 --> 00:07:56,630 cons, and make the best decision for your project at that point in time. 108 00:07:57,680 --> 00:08:02,410 That being said, I tend to find that I use DI at most of my projects. 109 00:08:02,410 --> 00:08:06,823 And if you're using ASP.NET Core, you'll probably want to take advantage of its 110 00:08:06,823 --> 00:08:11,300 built-in DI container, given that you don't incur any of the additional overhead 111 00:08:11,300 --> 00:08:15,780 or complexity of the adding an additional library to your project. 112 00:08:15,780 --> 00:08:19,220 In future Treehouse content, we'll continue to use DI. 113 00:08:19,220 --> 00:08:23,640 So be sure to spend some time practicing installing, configuring, and 114 00:08:23,640 --> 00:08:25,580 using a DI container. 115 00:08:25,580 --> 00:08:31,140 Also, in the teacher's notes, our links to multiple versions of the MVC project that 116 00:08:31,140 --> 00:08:36,490 we used in this workshop, implemented once for each of the popular DI containers. 117 00:08:36,490 --> 00:08:40,539 Reviewing the code for these projects will give you a sense of how each of the DI 118 00:08:40,539 --> 00:08:42,420 containers compare to each other. 119 00:08:43,500 --> 00:08:46,580 Have fun using DI in your next project, and we'll see you next time.