Examining DI in ASP.NET Core8:47 with James Churchill
Let's take a look at how DI works in the ASP.NET Core web framework.
ASP.NET MVC Example DI Container Implementations
The example project from this workshop, Treehouse.SolarSystem, has been updated to work with each of the following DI containers. Just click on a link to review the configuration for that specific DI container!
Unlike ASP.NET in VC, 0:00 the ASP.NET core web framework provides a built in DI container. 0:01 Let's add an ASP.NET core project to our solution, 0:07 and see how the built in DI container works. 0:09 To add a new project to our solution, right click on the solution, and 0:13 select Add > New Project. 0:17 Then, to add a ASP.NET Core application to our project, 0:22 select the ASP.NET Core web application project template. 0:25 I'll name my project Treehouse.AspNetCore. 0:31 At the top of the second dialogue, be sure you have .NET Core selected, 0:41 and select ASP.NET Core 2.0. 0:47 And then select the Web Application (Model View Controller) project template. 0:49 This will create an ASP.NET Core MVC styled application. 0:55 I'm also not including any authentication, and I'm not enabling Docker support. 0:59 Now, let's add our service. 1:07 I'll start by adding a folder named Services. 1:13 And then, add in a class to that folder. 1:21 Let's name our class TestService. 1:28 And then in our class, let's add a method named GetAboutContent. 1:33 It could be public, and let's use string for the return type, GetAboutContent. 1:39 And then, let's return a string literal. 1:48 How about, Here's the About view 1:51 content from the TestService service! 1:56 Now, we need to configure our service. 2:05 To do that, we need to open the StartUp file in the root of our project. 2:08 The StartUp class contains two methods. 2:14 A Configure method that's used to configure the middleware pipeline for 2:19 our application, and a method named ConfigureServices that, 2:22 as the name implies, is used to configure the services for our application. 2:27 The IServiceCollection interface provides three methods for 2:32 add in dependencies to the build in DI container. 2:35 AddTransient, which adds a service with the Transient lifestyle. 2:38 AddSingleton which adds a service with the Singleton lifestyle. 2:43 And AddScoped, which adds a service with the Scope lifestyle. 2:48 Let's add our service using the Scope lifetime, services.AddScoped. 2:55 And this is a generic method, so we supply TestService as the generic parameter. 3:00 Don't forget to add the name space to using AspNetCore services, 3:08 and include a set of parenthesis. 3:13 So far in this workshop, 3:16 we've been registering concrete types of our DI container. 3:17 But, you could also register interfaces, and 3:20 specify what concrete type should be instantiated for that interface. 3:22 To see this in action, let's add an ITestService interface to our project. 3:27 I'll right-click on the Services folder and say Add > Class, 3:37 even though we're adding a service. 3:40 I'll name this class or interface ITestService. 3:43 Change class to interface, and add our GetAboutContent method. 3:53 Remember, string is the return type, and then GetAboutContent. 4:01 Remember that an interface is a code contract. 4:07 It specifies what members, properties, and methods a class should implement. 4:09 But, it doesn't tell the class how to implement those members. 4:14 Now, let's update our service to implement this interface. 4:17 : ITestService. 4:22 It's that simple. 4:25 Our TestService class implements the GetAboutContent method by returning 4:28 a string literal. 4:32 But another implementation might get this content from a database, or 4:33 even an external third party service. 4:37 Now that we have our interface, let's update our DI container configuration. 4:39 To do that, we need to add another generic type parameter for our interface. 4:49 This is telling the DI container that when an instance of the ITestService interface 4:53 is needed, to use the TestService concrete type. 4:58 Registering interfaces with our DI container is a powerful concept. 5:01 To change what concrete type is used for 5:05 this interface, we just need to change this call to the AddScoped method, 5:07 by changing our second generic type parameter. 5:11 After doing that, every class that is dependent on the ITestService interface 5:14 would get that new concrete type. 5:18 Now, let's inject the service into our controller. 5:20 To start, let's add a private field. 5:25 Private, and then for the type, we want to use our interface, ITestService. 5:28 Don't forget, we also need to add a using statement for 5:35 the Treehouse.AspNetCore.Services name space. 5:38 For the name of the private field, let's just use _testService, and 5:42 set that to null. 5:47 We also need to add a constructor, public HomeController, 5:49 and then remember, we're using constructor injection here. 5:55 So we need a parameter to declare our dependency. 5:58 ITestService, and then let's just give this a name, testService. 6:02 Then in the body of the constructor, 6:09 let's set the private field _testService to the parameter. 6:11 Then, down at the about action method, we can replace this 6:17 string literal that's setting the ViewData Message key 6:23 to a call to our testService.GetAboutContent method. 6:28 Now, let's test our application. 6:32 And if you're like me, 6:35 sometimes you make the mistake of starting the wrong application. 6:37 Let's stop this app. 6:41 And in the solution explorer, we need to right click on our new project, 6:43 Treehouse.AspNetCore, and select Set as StartUp Project. 6:47 Then, press F5 to start the application again. 6:53 And here's our website. 6:56 Let's browse to the about page And 6:59 here' s the content from our TestService. 7:04 The native support for 7:09 DI is one of the many features that makes ASP.NET Core a joy to use. 7:10 The built-in DI container provides the functionality that will cover the majority 7:15 of your use cases that you're likely to encounter. 7:19 If you find that you have a requirement that the built-in DI container 7:23 can't satisfy, then you can replace it with a third party library. 7:26 See the teacher's notes for more information. 7:30 Now that you've learned what DI is and 7:33 how to use it, you might be wondering, when should I use it? 7:35 As you've seen, adding a DI container to an ASP.NET MVC application 7:39 isn't difficult to do, but for some small projects might be overkill. 7:44 As with most things in software development, weigh the pros and 7:49 cons, and make the best decision for your project at that point in time. 7:52 That being said, I tend to find that I use DI at most of my projects. 7:57 And if you're using ASP.NET Core, you'll probably want to take advantage of its 8:02 built-in DI container, given that you don't incur any of the additional overhead 8:06 or complexity of the adding an additional library to your project. 8:11 In future Treehouse content, we'll continue to use DI. 8:15 So be sure to spend some time practicing installing, configuring, and 8:19 using a DI container. 8:23 Also, in the teacher's notes, our links to multiple versions of the MVC project that 8:25 we used in this workshop, implemented once for each of the popular DI containers. 8:31 Reviewing the code for these projects will give you a sense of how each of the DI 8:36 containers compare to each other. 8:40 Have fun using DI in your next project, and we'll see you next time. 8:43
You need to sign up for Treehouse in order to download course files.Sign up