What is Dependency Injection in .NET?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] dependency injection it is a common interview question and explaining that with a real world example will show that you really understand the concept my name is broken and welcome to dotnet Mastery if you enjoy free content related to.net programming make sure to subscribe to the channel that way you will be notified when new content is uploaded now back to the main question what exactly is dependency injection dependency injection is a design pattern in which a class or object has its dependent classes injected rather than directly creating them this is done so that we do not have to create manage and dispose the object and it improves the loose coupling between classes now that is too much technical do not worry let me simplify that with a real world example that almost everyone can relate to in this example we have Bob here who wants to go on hiking now it is a long hike and for that he will require many supplies like Maps flashlight protein bars and much more so he places all of them in his backpack and next day when he goes on the hiking he takes the backpack with him the back that he has acts as a container so during the hike when he needs anything he can take it out of the container and use them this is the simple concept you put the items that you need in a container and when you need them they are already present in the container so you can directly fetch them from the container now that is a little too easy to understand but how does it relate to programming world to show that let me elaborate a situation without dependency injection in our application we have three pages and we have some common functionality that we want to use across all the three pages like we want to send emails and we want to access our database in all the three pages that we have so first let's say we have to access database in order to do that what we typically do is create object of our class which is used to access the database let me call that DB for this example then we might call some method inside that object and retrieve data once we are done we will be disposing that object that was created this object creation logic we will have to write in all the three pages and same thing we will have to do with email as well by doing that we are actually solving the issue of having email and database access in our page but as you can see we have a lot of duplicate code that is being written here and when we design we try to build the perfect solution but sadly most of the time we do not know the complete future of a project it could very well happen that in future because of some reason you might have to create a new class for sending email or maybe a new data access class what happens in that case well it is simple for now in all the pages where we had the old object we will replace that with the new class like in this case we have DB underscore new class and we have email underscore new and when we think about that it does not seem like a big issue that is because we only have three pages right now but what if your application had 30 pages or 300 pages you will have to modify that in all the pages and on top of that you will be dealing with creating the object managing and disposing them that is a time consuming effort when we have to do that in all the pages so what is the solution to avoid all of this and get an optimal architecture the answer to that is dependency injection let me show you how the same situation would look like with dependency injection again we would have the three pages and we would have email and database functionality these are common things that we had before as well but right now we would have something special which is a dependency injection container and in that container we will register both email and database service so there we will have an interface for I email and idb and we will have the implementation inside email and DB class when any page would access these functionalities they would ask for an implementation of ITB or I email the pages do not know what implementation will be given to them dependency injection container is responsible for all of that so what the framework will do is when a page request implementation of email interface it will look in the container to find out that email interface is implemented inside the email class so it will automatically create the object and pass it onto the page that way we do not have to deal with creating object disposing or managing that in our page pages will look very clean because they will just have the interface and implementation is provided by the dependency injection container the best thing is in future if you have to change or replace the implementation you do not have to do in all the places because you only registered the implementation in the container so only in that file you will have to go and replace with the new implementation so a DB and email we just need to replace them with DB new and email new and all the pages will automatically get the new implementation so with that you can see email and database are now Loosely coupled with the other classes and that is the beauty of dependency injection now dependency injection is built in if you are using.net framework so you just have to register the service and dotnet framework will take care of everything else I hope with that short video you have a better understanding of dependency injection if you enjoyed the video make sure to like the video and leave a comment if you have any other question or topics that I should cover next you can check out more free videos on dotnet mastery.com until then Happy coding
Info
Channel: DotNetMastery
Views: 16,664
Rating: undefined out of 5
Keywords:
Id: KMErAbXRQdg
Channel Id: undefined
Length: 7min 30sec (450 seconds)
Published: Fri Jan 13 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.