Dependency injection

- [Instructor] The final configuration stepthat we need to talk aboutis dependency injection.Dependency injection is now a first class citizenwith ASP.NET Core.Granted, it's always been available with ASP.MVC,but you had to essentially bolt it on yourself.Now it is built-in.Constructor injection is a default mechanism,and there are many built-in services available,for example, creating entity framework DB context classes.

You can also register your own custom interfacesand there's three standard ways of doing that,transient, scoped, and singleton.So transient gets instantiated for every objectthat needs it.So you must think about that from a performance standpoint.Scoped is once per request,and singleton, as its name implies,gets instantiated as a singleton across the application.It is important to note that you don't needto actually implement the singleton patternin something that is scoped as a singleton.

The DI container takes care of that.The dependency injection containerhandles disposing of objectswhere it is controlling their lifetime.You say that affect how we coded the dispose methodin our repository.Back in the startup.cs class,let's go to the configure servicesand let's add a singleton for the configuration.So services.add,singleton,and the construct we're going to use hereis to just pull in its own referenceof the configuration that was built in the constructorinto the dependency injection container.

So we don't need to recreate the configurationif we need it, we can just pass inan instance of I configuration root.Now after configuring the MVC core,we want to add in the DB context,and this is one of those built-in servicesthat you can use with the built-in DI container.So it's services.AddDbContext.It is of type store context.

And then we want to configure some options.And the first is to use SQL serverand then we need to get the connection string.So we use that configuration object.And there are some helper methods,as long as you use the same naming convention,you can use get connection stringand the connection string is named SpyStore.

If you change those values,then you'll need to update that line of code.And then, we'll use sqlOptionsand that's sqlOptions.EnableRetryOnFailure.And I have one too many parenthesis here.

And there we go.Now we have to add in our product repoand shopping cart repo.So we say services dot, and we want these to be scoped,so AddScoped,and we tell it that anytime there's a requestfor an IProductRepo,and we'll have to add in a usingfor SpyStoreDAL.Repos.Interfaces,that we want to actually create an instanceof ProductRepo.

And then we'll have to add any usingfor SpyStoreDAL.Repos.So just to confirm those using statements.Line 15 and 16, SpyStoreDAL.Reposand SpyStoreDAL.Repos.Interfaces.And then the final service that we will addis another scoped,and this is for the IShoppingCartRepoand we want it to create an instanceof ShoppingCartRepo.

So anywhere within the ASP.MVC projectand any of the assemblies referenced by that,if there is a constructor lookingfor an IProductRepo or an IShoppingCartRepo,the dependency injection frameworkwill take care of instantiating that for us.Down here in the configure method,we want to do one more thing.We want to call the initializerto put data in the database if we are in development.

Since it is a flush and fill,you certainly wouldn't want to do thatin a production environment.So we need to create a scope so the DI containercan provide us with the store context.Now this is kind of seeing how the sausage is made.You don't necessarily need to do this often.

But we call ApplicationServices.GetRequiredServiceand that's an IServiceScopeFactory.CreateScope.Now that we have the service scope,we can say var context =serviceScope.ServiceProviderGetService of type StoreContext.

Now that we have the context,we just need to call initializer.InitializeDatapassing in the context.This added another using statement for us on line 15,SpyStoreDAL.EF.Initializers.Now you can see the context hereis showing up with a squiggleand it doesn't really know if I'm passing ina service provider or a store context.

This is very simple.Let's just make it instead of var,use StoreContext.And that completes configuring the application.

Resume Transcript Auto-Scroll

Author

Released

8/3/2017

ASP.NET Core is the exciting new release from Microsoft for web development, allowing developers to ship their apps to run anywhere, including Windows, Mac, and Linux. If you're considering whether a move from ASP.NET Web API, this course will provide an overview of the procedure for migrating your site to the new platform. Author Phil Japikse shows how to inspect your ASP.NET Web API project, create a Core MVC Services project, and migrate and configure all elements of the project to take full advantage of the features of Core. Learn how to update packages, add additional packages such as Entity and AutoMapper, migrate the data access code and controllers, and unit test the new services.