C# Dependency Injection with Real Time Example

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we are going to learn about dependency injection dependency injection is a software design pattern it allows us to develop loosely coupled code the intent of a dependency injection is to make code maintainable dependency injections help us to reduce the tight coupling among the software component so first we'll understand therefore what is actually type of tight coupling just open a new project just let the console application so first we understand that of what actually is a tight coupling create one class like a student and one more class that is a data service get that matter now here if I want to consume this to the data service class so what in the traditional way that I am going to create data service object and obj dot just leave them at her then we'll be zero get me so here you can see that one that particular student class is depend upon the data service class that is called an tight coupling because when you are going to change anything here so he had to change here also so that I record is a tight coupling so how we can solve this type of problem with a dependency injection dependency Jackson is the main level is a three part one is that client class second one is the service class third one is the injector class the client class is a class which depends on the service class and service class the class is a class that provide service to the client class and what is the injector class the injector class injects the service class object into the client class one is the client class second one is the service class a third one is that injector to us here if you looked at the client class is a class which depends upon the service class so first I am going to create that one service class first we create that interface that is an eye surface and here that we are going to create two method get' first name then what get last name now going to create that service class that this one is a second one first I'm going to create a service class so how will it leave the service classes like public class Student Service I'm going to inherit that I service now going to implement just hearing them right student first name and same I'm going to copy student ask them then again I am going to create one more service class that is a teacher service methods I surface going to employment this one so just copy this one we are going to test teacher and this last teacher so created that one that service class now now second we are going to create that client class so how did create that last class let's create a public one class business logic service and that there are three types of dependency injections first is the constructor second one is the method method dependency injection and third one is the properties that is a setter dependency injection so here I am going to use that constructor dependency injection so constructor what about the citizen you have to clear the country constructor and here you have two here they're going to inject that particular service first you have to initialize this one now you can call this one this dot service dot get first name and this start service dot taped last name now this one is recorded thankless this one is a client class this one is that service class this one a teacher also this one also a service class now one more thing is that injector class now you're going to take this one that a program class that is a Gordon director class so here just create that object business project then new business logic service so he and he have to pass that one that going to inject that is called a student service of you can use that teachers of which say which service they are going to pass so this one is called injecting you are injecting something consider ready now run the application now you can see here that a student first met a student announced time if he will change here that service you want to inject that teacher service then it will call that teacher teacher now you can see here teacher first time that we just ask them but here there is one problem because here because in that clap your injector Glassman's like a that program class you are actually are defining that one he you are going to call that teacher service you are going to inject that from teacher service in the next video we will learn that third particle with the help of the third-party tools that is called as if you know that when injector is there and unity that is that is a third parentis how we can solve this type of problems I hope it's equally as thanks for watching
Info
Channel: Tek Tuition
Views: 73,157
Rating: undefined out of 5
Keywords: types of dependency injection c#, dependency injection c# mvc, dependency injection c# interview questions, dependency injection c# kudvenkat, dependency injection c# unity, advantages of dependency injection c#, dependency injection c# framework, dependency injection c# msdn
Id: daYmMZ28-Y0
Channel Id: undefined
Length: 9min 59sec (599 seconds)
Published: Wed Jul 17 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.