Dependency Injection for ASP NET Core 6.0 - An UPDATED 2022 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there this is yannick for tutorial cu and this video here has been really requested a lot of times we will talk about dependency injection in asp.net so let's get started right now so that you see how easy it is to use and how you can make use out of it and if you are new to our channel make sure to hit the subscribe button right now so that you no longer miss any high quality programming related content let's get started so i already set up an asp.net 6 mvc project that's completely the bare template that already gets provided and the only thing that i have done is i created a folder and called it services so why would we even make use of dependency injection let's just assume you are developing your asp.net program and you come to the point where you want to create a really cool service something maybe like an email sender or whatever the question many people really have is how can i make use of this email sender service in every controller for example so that i can start sending mates from different positions so before we talk about how you can register and use the service let's first of all create one right now so go ahead and add a new c-sharp class to the service folder and let's call it email sender service now there are a lot of ways to use dependency injection but the most common one used is to make use of interfaces so let's go ahead and create an interface for our email sender service so let's call this i email sender service let's simply create a method right there which is send mail for example we can do this with a return type of void let's call it send mail right we automatically get that adjusted from ai so that's fine and now our class email sender service will implement the interface so let's go ahead with i email sender service so right now our service is complaining right here because we do not make use of the sent email void that we have so we're not implementing it let's go ahead and do that hold the cursor right above the service control dot and we can see implement interface let's just go ahead and remove that one and we can here add a simple to do right so it's really not about the functional code of how you can send a mail it's how you can make use of the service and call the send email method from any controller in asp.net alright let's make a short break right now do you have any wishes for programming content if you want to see some specific topics go ahead and write it into the comments below because then we really know what you want to see and we can create the tutorial videos for you we are done with our service right now now we need to register it and make use of it so let's switch to our program.cs and if you are familiar with asp.net 3 then you know that previously there was a startup file and the program cs5 separated but this got all removed in asp.net 6 so right now everything is combined and if you would like to register a service so like the one we have just created you will do that before we call the builder.build right so here the application gets built and before that we can register services to our container so let's go ahead and do that that's called builder dot services and inside of that services collection we can go ahead and register our service right now so let's do this as scoped what it means i will talk about that just in a second and for scoped we go ahead and say we want to have that of type interface right so let's go ahead and call i email sender service right we need to get our namespace reference right here we can simply do that importing the services folder on the services namespace and the type which we are implementing is email sender service so there we have both configured here the interface and the implementation right because you can make uh the interface and then you can create a different class so that you have maybe email sender service two or three which has maybe some variations different code so you can say hey this interface this is what i want to use and this implementation right so this is how it's working and add scoped here is simply how long the active instance will be available right so in this specific term when you make an http request this email sender service will get used and if you just create a new http request so or maybe our program receives a new http request there will be a new email sender service and you can have that with add scoped and for example add singleton and for a singleton it will get shared between the request so this is just how the instance of the object will get created and used and how long it will be alive so this is just about lifetime so what this line of code here is really doing it just adds our service to the dependency injection system now this is all we have to do when it comes to registering let's take a look on how you can make use out of it so let's go ahead and switch to one of our controllers this is just the bear nvc template right here so let's go to the home controller and let's see what we can do to make use of our service so you might already know that we use the constructor to get the services from the dependency injection right here it's working with a logger let's just get rid out of it we can remove it so just like this or you can even go ahead and remove the constructor because i can show it to you really from the scratch let's create a constructor if you're not familiar with it we can have a shortcode for this it's called ctor so let's go ahead and press tab top now we got public home controller so the next step is to go inside of the parameters from the constructor and to take the interface so i email send a service let's call it service you can for sure take another name that one is nice and short now go ahead and create a private read-only variable of type i email send a service and let's call it service so our private read-only variable has the underscore the parameter from the constructor has no underscore so this is really important now let's assign our private variable with the value from the dependency injection so that we can really make use of the service this is all we have to do now inside of our controller we have the service which will get its value or the instance right inside of the constructor when the object gets created so the home control object and now you can see that when we go into the index let's go ahead and call our service dot send email for example hello world so as i said you maybe want to choose a different name maybe you can go ahead and say email service or email sender service but i really want to keep it short so our service is the email sender service right so we need to take a look at the naming let's set a breakpoint in line 20 right here and press on start and when the application is loaded our service should have an instance and we should be able to make use out of it so the dependency ejection will have its job done and as you can see it already stopped and when we hold the cursor right above the surface we can see that we have an instance right here if we would not have then we would see null but even before we would see null we would see an error on our page right here which would say dependency injection was not able to create the instance for the interface and make use of it inside of the home controller so yeah this is how you can make use out of it if i continue we for sure get to the page etc so everything will be working so just keep that easy steps in mind create an interface create a class which implements the interface register it as a service inside the program right before you build the application and then take the service from the constructor or from the dependency injection inside of the constructor and you are already done and you can for sure do this in every controller so i can go ahead into another controller and do the same stuff again and just take the email sender service and make use out of it so this is really how you can create a service and make use out of the dependency injection so thanks for watching and see you next time and if you haven't subscribed already do it now because we deliver you high quality programming content and you really don't want to miss that so subscribe right now
Info
Channel: tutorialsEU - C#
Views: 38,667
Rating: undefined out of 5
Keywords: dependency injection in asp.net core, asp.net dependency injection, dependency injection, visual studio, c# dependency injection tutorial, dotnet, dependency injection explained, dependency injection tutorial, csharp, c sharp, di, ioc, tutorial, programming, code, visual studio 2022, asp.net core 6, dotnet 6, asp.net core api, .net core, asp.net core, tutorialseu, asp net, mvc, asp net core, tim corey, asp net mvc, dotnet core, asp net core mvc, freecodecamp, dev, iamtimcorey, how to
Id: 9J9a77ga9R0
Channel Id: undefined
Length: 8min 10sec (490 seconds)
Published: Tue Apr 05 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.