How to use Dependency Injection (DI) in C# Console Application with Logging

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
dependency injection is one of the most used Concept in software development and if I talk about the net code then we have some built-in support for some of the Frameworks for example if I'm working with ASP net code then I will have the built-in support for the dependence injection but what will happen if you want to use this dependency injection in console application is that still applicable let's understand how can we use the dependency injection in our console application and once we will integrate the dependency injection in this console application then we will also learn how can we use the loggings in this application because these two are something that are most common Concept in any software development so let's understand everything from scratch so here I'm using this Windows operating system and here I have installed the latest version of net go which is net 8 let's verify the installation by using this command which is net d d and the info and here you can see that I'm having the latest which is 8. we will learn everything from scratch and as a first step first I will create a console application over here and for that I have to write a command which is to net new console and then for the output let's say here I'm writing this di n console you can create your application by using the visual studio also but here I'm doing it by using the visual studio code okay let's hit the enter button and here you will notice that we will have a new application if I navigate to this particular folder which is di in console and open it in the VSS code editor and here you will see that we are having this application this is the DI in console and this is my CS Bros over here you will see that we are having this net 8.0 monik and the Nel is enable and all the basic default things we have over here now in this application I want to create the dependency injection to use the dependency injection over here let's create a very basic scenario so over here first I will create some repositories and I will also create one more class and by using that class we will use the repositor as a dependencies and we will learn how to to resolve them by using this console application so the concept is very simple but it is very useful so let's say here I'm having this employee repository do CS let's just copy the name hit the enter button and create a new class over here let's say it is employee repository we will add the method in just a bit and let's add one more interface so this is going to be I employee repository. Cs and let's use this interface over here this is very simple so here I will create one method which is let's say it is get name now we will use this interface over here in this employee repository class like this and now we have to implement this interface so what I can do I can use this quick fix and just implement this interface and from this one let's simply return a message which is going to be hello from employee Repository that's it so so far we have very basic scenario now I will have another class over here for example it is employee. CS remember we are having this employee repository and I employee repository now I'm creating this employee and let's create this class and employee this one perfect and in this employee class I want to use this repository as a dependence injection so here I will be using the concept of primary Constructor and and here I'm writing this I employee repository and let's say this is repository very simple now let's get one more method over here and here I'm writing this public string let's say get username you can give any meaningful name over here or in your scenario you might have a different situation and here I'm using this repository do G name so this is my very basic setup now I have to tell this console application that I want to use the dependence injection and for that I have to add a package over here and that package is available in the new get and here I can search for some package which is Microsoft dot extensions do dependency injection hit the enter button or you can basically click on this one and you will see that we are having this package which is dependence injection now if you are using the doet CLI then you have to write this command to add this package in your application which is toet ADD package and this is the name of your package and this is the version that you want to install if you using visual studio then you can search for the newat packages and you can simply search about uh this package and you can just install it but here because I'm dealing with the vs code so I have to use this net CLI so let's copy this package go back to our editor and let's open a terminal window there we go like this and now simply just right click over here and you will get this command at this place just hit the enter button and this will install this dependency injection package with version 8 in this application and if I open the Cs Pro File then you will notice that we are having this package over here and this is the only thing that is required to deal with the dependency injections in console application now we have to create a container in this application and we have to register all the services in that container let's do that and for that we will use this program.cs class because this is the entry point of this particular program here I have to create a new instance of the service collection so let's say it is services and just create a new instance by using this new keyword and here we have to register all our services and how can we do that we are having the services if I hit the toor then you will see we are having all the methods of the dependence injection we are having this add key discop we are having this Addis scoped add Singleton add transient and all of them are available over here now let's say I want to register my service by using the Singleton concept so we are having this singl ton so as per this concept first we have to use this interface and then the implementation and the interface is this I employee repository so let's do that over here put a comma and then the implementation the implementation is this employee repository this one go back to this program. CS class and over here let's use this now once you are done with this thing you have to use another method which is dot build service provider that's it you can also use it in another line for example here I'm using this we provider and put a semicolon over here and use this services and just do it like this now what we are having we are having the very basic setup of our container if you want to add more dependencies over here then you can simply you know just copy this line paste it again over here and just register your second service like this perfect and now I want to create the instance of this employee class and I want to basically call this method to get the output from the repository we are having this employee employee and this is the new right over here we have to provide something in the Constructor and what we can do so we are having this provider dot get service or you can also use that get required service and in this one I have to provide the interface which is this I employee repository this one that's it now what I can do I can basically use the console do right line method and over here by using this employee object I can call my method employee dot here you will see we are having this method which is get username and that is everything that is required to work with the dependency injection in your console application if I will just run this application so here let's say I'm writing this first clear this screen and use this do net run hit the enter button and here you will notice that we are getting the output which is this hello from employee repository if you are thinking about this warning this is coming because we have enabled the nullable in this application and to disable that we can basically open the Cs Pros file and just use the disable over here for this n labels and just run this application again and this time you will notice that we are not having that warning see we are having only this direct message now let's verify this by grading one more temporary service and basically I want to just change the implementation of this repository class so so what I can do is go back to this employee repository just copy it and let's just paste it again over here like this and this time let's use the temp keyword and let's just copy it go back to our program.cs class and over here let's just change this registration remember we are not making any change in this employee class we are only making the changes where we are registering our services now let's just run this application again and before that we have to make the change in the message over here save the changes go back over here onun this application and let's see what is the output this time this time you can notice we're getting the output from our temporary employee repository so this is how you can deal with the dependency injection in your console application what if I want to add loggings as well over here let's understand how can we use the loging because we are dealing with the dependenc injection so it is always a good practice to use the loging by using the dependence injection let's see how to do that and to work with the loging we have to install another package which is microsoft. extensions. login and because we are dealing with the console so I have to use this console let's search and here we are having this first package which is microsoft. extensions. log. console let's copy it go back to our console application and let's simply just paste it and basically install this package hit the enter button and this time you will notice that the package has been added in this application if I go back to the Cs proos file here you will notice we are having two packages this time this is this is for the logging and this is for the dependency injection now because we are using this dependency injection container so by using this concept I have to register my loging let's go back to this program. CS class and over here we can use this services do add loging this one and here we have to tell that I want to use the console loging so which we can use let's say it is Builder and Builder Dot add console that's it this is the only thing that is required to enable the logging in this application now I want to add the loger let's say in this employee repository so again I will use the primary Constructor concept so here I'm using this I loger and over here let's use the name of this repository this is how we use the loging in any application basically and that's the loger perfect here let's say I'm using this logger do log information or you can use any other method that are available in this logging service we are having all of them over here we are having this critical debug error information Trace everything is available over here and let's say here is the message loging from employee repository now we have to change the registration in our program. CS class and here instead of using this stamp we have to use our employee repository remember we are not making any change anywhere we are using this loging as a dependency injection and because we are already using the container I have enabled this dependency over here and if again I will just run this application by using this to net run let's hit the enter button and this time you will notice we are having two messages this first one is coming from our employee repository which is the value that we are returning and this is coming from the logging service let's try another example so this time let's say here I'm basically having this strike Ed block and in the strike C block let's say I'm just throwing the exception throw new exception and here I'm writing any messes basically so let's say this is my test exception like this and over here let's just log this information so I'm using this logger Dot log error and this time let's deal with the message so this is ex. message and let's just remove this throw keyword perfect now let's just run this application again and see what will be the output this time you will notice that we are having a couple of messages first one is this information which is logging from employee repository this is logging from employee repository and then you will see that we are having this message which is Hello from employee repos this one because we are not throwing the exception over here right we are just simply logging them and we are again getting this this is my test exception that's it what will happen if I will throw this exception and just run it again this time you will notice that we are not having this hello from employee repository message we are having this message from this information and then we are having the message from this Lo error and this is how you can use dependency injection in net 8 console application I hope the video was useful if you like it please like the video And subscribe to the channel thank you for watching have a great day
Info
Channel: Nitish Kaushik
Views: 1,267
Rating: undefined out of 5
Keywords: how to use dependency injection in c# console application, how to use dependency injection in c#, dependency injection in c# console application, DI in c# console application, dependency injection c# console application, dependency injection in console app c#, c# console dependency injection, logging in C# console app, c# logging console application, dotnet core dependency injection console application, dependency injection, c# dependency injection tutorial
Id: BLHyeXrt3Jk
Channel Id: undefined
Length: 13min 32sec (812 seconds)
Published: Thu Dec 28 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.