Same here! This is the simplest, most concise explanation of DI I’ve come across. I’ve just been assuming DI worked over PFM (as in “Pure F***in’ Magic”) LOL
I wish all who teaches technical concepts in universities were like you, it would have been so easier. You have amazing ways to deliver a hard concept in a very easy understandable way.
Well, after reading docs from Microsoft and asking .NET Developers about how DI works and why I need it I finally understand with your tutorial. Thank you very much for your work!
I failed my interview because of why we use interface , the interviewer wanted to go so in depth with this question in this video and the previous one I understood why we use interface and and DI thank you very much I searched a lot for this topic but you offer the easiest explanation Thank you from the bottom of my heart
What a brilliant video.. Great stuff. I judged the video by the thumbnail, and thought it wouldn't be great, but what a great explanation.. 🔥❤ "Never judge a book by it's cover"
I really like your explanation of the logic behind the use of AddSingleton instead of tightly coupling the implementation of IEmployeeRepository. Thank you for the great work! Absolute best!
Hello Rong - Thank you very much for the feedback. Means a lot. I have included all the .NET core tutorial videos, slides and text articles in sequence on the following page. Hope you will find it handy. www.pragimtech.com/courses/asp-net-core-mvc-tutorial-for-beginners/ When you have some time, can you please leave your rating and valuable feedback on the REVIEWS tab. It really helps us. Thank you. Good luck and all the very best with everything you are doing.
Why in the world can't people explain this simple like Venkat does I use to really wonder why for god shake why we need interface when things can be done straight forward UR GOD of programming bro just wonderful
GREAT VIDEO!!! I like how you gave a real world example of why this matters rather than assume everyone knows this. It would be so much better if other people would do this with their videos OR explanations on information websites. I will continue to see if you covered the other ways of Dependency Injection. This is good stuff.
Thank you Michael. Means a lot. If you are planning to become a full stack software engineer, I think the following video will be of some help. ruclips.net/video/RiKcSDbGVXw/видео.html
Great video, thanks. I might be confused about the different methods of DI (eg what you did here vs using Autofac). Question: Do you always have to register your interfaces in Startup? I watched another video where he didn't have to use Startup to tell Visual Studio which implementing class to use - he simply passed that class as an input when calling the method ("C# Dependency Injection Constructor" by Theenk, specifically 14:09-14:40). Can you please explain to me the difference in your two approaches? Also: - are "interfaces" synonymous with "services"? (see your video at 3:15) - is "registering" the same as "binding"?
My question is regarding services.AddSingleton(). How can I add multiple concrete implementations of IEmployeeRepository, and then be able to use one of them at run time depending upon the situation?
If you search around there are many more thorough tutorials available. If you notice his videos are short and "Sweet". There are many instructor led tutorials that do a far better job than Kudvenkat. He may be good at teaching things via his steps approach which shows you what to do in theory but you might be missing the bigger picture in the application of dependency injection via SOA.
I am not able to understand the concept of ASP.NET Core Dependency Injection. Could you kindly please explain the following code block because I don't understand the code ? public class HomeController : Controller { private IEmployeeRepository _employeeRepository; public HomeController(IEmployeeRepository employeeRepository) { _employeeRepository = employeeRepository; } public string Index() { return _employeeRepository.GetEmployee(1).Name; } }
@@ymtan Creating a object of dependent class with out using new keyword called Dependency injection.In our code accessing a concurrent class is MockEmployeeRepository (dependent class) in HomeController class need to create object. Whenever we are creating the object using new keyword,both the class are tightly coupled.So main concept of DP is loosely coupled.The solution is to pass dependent class or interface instance variable as a parameter to a home controller constructor.
In this video venkat sir explain very clearly,by changing the depend class constructor,immediately home controller getting an error.So by passing dependent class or interface instance,the error was gone.
what is the point of using Interface for Repositories and creating mock objects while you can create an in-memory database of DB context and passing directly to classes for unit testing? That's one of the features of .NET core
one doubt sir, If two classes implementing the same interface and having the same method, then if i call the method with that interface, which class method will be called?
You can use IEnumerable collection and linq for that. Inbuilt dependency injection is not supporting multiple classes from single interface. Below is the code sample. Services : public interface ITest { void writeMsg(string t); } class ATest : ITest { public void writeMsg(string t) { Console.WriteLine(t + "A"); } } class BTest : ITest { public void writeMsg(string t) { Console.WriteLine(t + "B"); } } Getting particular service through controller injection : ITest _itest; private readonly IEnumerable _itests; public HomeController(IEnumerable itestList) { _itests = itestList; _itest = _itests.First(o => o.GetType() == typeof(BTest)); } Here you can get the instance of BTest class. Hope this helps.
Can you please add a video which shows Multiple implementations for Dependency Injection like in the current video (Part 19) you show one implementation for MockEmployeeRepository, how to manage if in future we have other implementation for IEmployeeRepository?
Hi Venkat, im getting this error (System.NullReferenceException:"Object reference not set to an instance of an object." ). and the Application crashed. can anyone explain way ? Thanks
Can I use a repository pattern with .Net Core Entity framework so that I can switch out implementations? For example lets say I want to use a class like MockEmployeeRepository for testing but I also want the flexibility to use a SQL Server data store. Thanks !!!
Nice, much clearer now. One question though, can you please tell us if this is possible to register multiple implementations of same interface using DI and selectively use one at constructor level or method or property level as required.
@venkat, can you please explain how to inject multiple implementations of same interface otherwise if it is not possible doing so then what is use of interfaces and multiple classes inheriting same interface
You can use IEnumerable collection and linq for that. Inbuilt dependency injection is not supporting multiple classes from single interface. Below is the code sample. Services : public interface ITest { void writeMsg(string t); } class ATest : ITest { public void writeMsg(string t) { Console.WriteLine(t + "A"); } } class BTest : ITest { public void writeMsg(string t) { Console.WriteLine(t + "B"); } } Getting particular service through controller injection : ITest _itest; private readonly IEnumerable _itests; public HomeController(IEnumerable itestList) { _itests = itestList; _itest = _itests.First(o => o.GetType() == typeof(BTest)); } Here you can get the instance of BTest class. Hope this helps.
Hi Sir , one question here , As i have just one implementation here , but in reality i may implement 2 class with same interface utilization , now if i mentioned in start up file service.addsingleton and service.addsingleton, in this case what will happen . Please suggest .Thanks for your all videos , I really enjoying while learning with your videos .
Just one question, in the real-world application there will be multiple Interfaces and Classes and that will be a little bit hard work to implement Injection for each Interface. Is there any other way so that we implement those Interfaces and classes an efficient way?
I think you have to implement them like this for every interface and so on. But you can make it look a little cleaner then filling all of them in your startup.cs. You can make a Extension Method to keep all the DI's you want instead of filling all of them in your startup. It looks much cleaner. public static class ServiceCollectionExtensions { public static IServiceCollection RegisterServices( this IServiceCollection services) { services.AddTransient(); // and a lot more Services return services; } } And then in your startup.cs you write this for example. public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddMvc(); services.RegisterServices(); //
I really love your videos, but not this one. For me it was very confusing. Maybe a slide with a diagram of dependencies would have helped to make it clearer
I got an error in HomeController that "Inconsistent accessibility: parameter type 'IEmployeeRepository' is less accessible than method 'HomeController.HomeController(IEmployeeRepository)' " how to resolve that?
What if don't inject and when we need to change MockEmpRep() to SqlEmpRep() type we just changed the code inside the mock and as a result only one place needed to changed.? Please someone clarify me this.
can someone please explain to me how would I create an instance of HomeController? As I understand the DI Container will handle the dependency injection.
Dependency Injection was a mystery for me before this video, and I simply did not know why bother with all the additional code. Thanks man A LOT.
Same here! This is the simplest, most concise explanation of DI I’ve come across. I’ve just been assuming DI worked over PFM (as in “Pure F***in’ Magic”) LOL
I wish all who teaches technical concepts in universities were like you, it would have been so easier. You have amazing ways to deliver a hard concept in a very easy understandable way.
Well, after reading docs from Microsoft and asking .NET Developers about how DI works and why I need it I finally understand with your tutorial. Thank you very much for your work!
Finally!!! I've understood what Dependency Injection is. Thank you so much, man!!!
Simply I wanna say----> I just like it. Thanks a lot...........
Right to the point, no wasted time bloating the video, just a very clear explanation of DI. Thanks.
You sir are the goat. The best tutorials no matter the technology. THANK YOU!
Goat? do you know what 'goat' means?
@@alex_g lol
@@alex_g Great Of All Time!! and Yup he is! Thanks so much Sir.
I failed my interview because of why we use interface , the interviewer wanted to go so in depth with this question in this video and the previous one I understood why we use interface and and DI thank you very much I searched a lot for this topic but you offer the easiest explanation Thank you from the bottom of my heart
This is the best dot net core tutorial series in the internet
You are dong a great service to developer community. Your work is admired by everyone across the world. What a presentation! What a control!
A Very Very good explanation of Dependency Injection in ASP.NET Core, Thnaks man.
What a brilliant video.. Great stuff. I judged the video by the thumbnail, and thought it wouldn't be great, but what a great explanation.. 🔥❤ "Never judge a book by it's cover"
I really like your explanation of the logic behind the use of AddSingleton instead of tightly coupling the implementation of IEmployeeRepository. Thank you for the great work! Absolute best!
even though these videos are old the concepts havent changed. absolutely top quality stuff
Thanks Kudventkat for your time to sharing this helpful tuto, keep it up i appreciate it a lot
Great explanation.. Nobody explains like you . Hats off to you.
Best video that I have ever seen🙏 Thanks for all your help🙏❤️
Thanks for the explanation on instructor injection. It has helped my understanding of the dependency injection concept.
Your teaching method is very impressive and doubt clearing
Great tutorial! Clear and concise explanation!
Hello Rong - Thank you very much for the feedback. Means a lot. I have included all the .NET core tutorial videos, slides and text articles in sequence on the following page. Hope you will find it handy.
www.pragimtech.com/courses/asp-net-core-mvc-tutorial-for-beginners/
When you have some time, can you please leave your rating and valuable feedback on the REVIEWS tab. It really helps us. Thank you. Good luck and all the very best with everything you are doing.
Venkat you are a genius when you explain the concepts ..to the point ...thanks dear
Part 44 (from 8:33) of this Tutorial discussed about these 3 methods in details
Ultimate explanation
Finally!!! I have understood what DI is. Thanks Sir.
You are very welcome and thank you very much for taking the time to provide feedback.
very well explained, thank you!
The best. Ur explanations are awesome sir.
Excellent demo of DI, great job. Thanks Venkat.
Very great explanation, I want such tutorials taking less time to understand as it focuses on the main topic, thanks
Why in the world can't people explain this simple like Venkat does I use to really wonder why for god shake why we need interface when things can be done straight forward UR GOD of programming bro just wonderful
One word 'WOW'. Very well explained.
GREAT VIDEO!!! I like how you gave a real world example of why this matters rather than assume everyone knows this.
It would be so much better if other people would do this with their videos OR explanations on information websites.
I will continue to see if you covered the other ways of Dependency Injection.
This is good stuff.
great great great, and simple , thank you very much
the best tutorial with the best explanation. thank you
WOW , Thank you so much , very useful video
Very nicely explained. Thanks a lot Sir.
Thanks! That's exacly what I was looking for
Great video, just what I need for my university project.
Thank you Michael. Means a lot. If you are planning to become a full stack software engineer, I think the following video will be of some help.
ruclips.net/video/RiKcSDbGVXw/видео.html
Nice
Martin Fowler is a genius living legend. All patterns we know of nowadays stem from his brain
First time I understood dependency injection🙏🙏
Almost hit the like button TWICE!!!
What an amazing explanation. Kudos!
Thanks so much for explaining lot about DI. Its so simple and so deep to understand.
Thank you very much Venkat. Your videos are really helpful.
Great video, thanks. I might be confused about the different methods of DI (eg what you did here vs using Autofac).
Question: Do you always have to register your interfaces in Startup? I watched another video where he didn't have to use Startup to tell Visual Studio which implementing class to use - he simply passed that class as an input when calling the method ("C# Dependency Injection Constructor" by Theenk, specifically 14:09-14:40). Can you please explain to me the difference in your two approaches?
Also:
- are "interfaces" synonymous with "services"? (see your video at 3:15)
- is "registering" the same as "binding"?
Thank you so much what a excellent explanation
My question is regarding services.AddSingleton(). How can I add multiple concrete implementations of IEmployeeRepository, and then be able to use one of them at run time depending upon the situation?
Look into the Factory pattern.
Wow, Simply Wow
Amazing explanation. Thanks
Great tutorial bro thx !!
Your videos is so great..
❤❤Great Video Sir
Thanks teacher venkat, check in today !
Fantastic Video Gentleman
Thank you! Great video.
short and to the point, thanks
You make it so easy to understand, thank you sir
Great one!
I like this enjoyable videos a lot!!
Ur best! marvelous!!!
so to the point ❤
Thank you so much really helpful
You're doing great job.
you're the best
The "Previous" and "Next" video links are wrong pointing to video 17 and 18
...and a bit quick for a complex concept
Amazing Amazin Amaaaazing!!
Awesome bro ... just awesome :)
Great explanation, thank you
Dear sir, kindly make a tutorial for generic repository and unit of work.
We are badly missing it.
Thanks
If you search around there are many more thorough tutorials available. If you notice his videos are short and "Sweet". There are many instructor led tutorials that do a far better job than Kudvenkat. He may be good at teaching things via his steps approach which shows you what to do in theory but you might be missing the bigger picture in the application of dependency injection via SOA.
amazingly explained
Genius ❤
very cleared, thank you venkat
It was really well explained. Although I did not get the difference between singleton, scoped and transient, and when to use one over the others.
But in what scenario where we need to change mockrepository class to sqlrepository class implementation as you mentioned in video
Simply supb venkat sirrrrrr......
I am not able to understand the concept of ASP.NET Core Dependency Injection. Could you kindly please explain the following code block because I don't understand the code ?
public class HomeController : Controller
{
private IEmployeeRepository _employeeRepository;
public HomeController(IEmployeeRepository employeeRepository)
{
_employeeRepository = employeeRepository;
}
public string Index()
{
return _employeeRepository.GetEmployee(1).Name;
}
}
@@ymtan Creating a object of dependent class with out using new keyword called Dependency injection.In our code accessing a concurrent class is MockEmployeeRepository (dependent class) in HomeController class need to create object. Whenever we are creating the object using new keyword,both the class are tightly coupled.So main concept of DP is loosely coupled.The solution is to pass dependent class or interface instance variable as a parameter to a home controller constructor.
In this video venkat sir explain very clearly,by changing the depend class constructor,immediately home controller getting an error.So by passing dependent class or interface instance,the error was gone.
I have understand this only.Please correct if am wrong.
@@ymtan Previously,we are using third party tools like unity.mvc5 to providing and resolve the errors in DP .
what is the point of using Interface for Repositories and creating mock objects while you can create an in-memory database of DB context and passing directly to classes for unit testing? That's one of the features of .NET core
Awesome Thank you!!
one doubt sir, If two classes implementing the same interface and having the same method, then if i call the method with that interface, which class method will be called?
The one which is registered in ConfigureServices. If you try to register both you will get an error.
You can use IEnumerable collection and linq for that. Inbuilt dependency injection is not supporting multiple classes from single interface. Below is the code sample.
Services :
public interface ITest
{
void writeMsg(string t);
}
class ATest : ITest
{
public void writeMsg(string t)
{
Console.WriteLine(t + "A");
}
}
class BTest : ITest
{
public void writeMsg(string t)
{
Console.WriteLine(t + "B");
}
}
Getting particular service through controller injection :
ITest _itest;
private readonly IEnumerable _itests;
public HomeController(IEnumerable itestList)
{
_itests = itestList;
_itest = _itests.First(o => o.GetType() == typeof(BTest));
}
Here you can get the instance of BTest class.
Hope this helps.
Thank you for questions and shared relevant answers. They all help to improve.
Can you please add a video which shows Multiple implementations for Dependency Injection like in the current video (Part 19) you show one implementation for MockEmployeeRepository, how to manage if in future we have other implementation for IEmployeeRepository?
Hi Venkat, im getting this error (System.NullReferenceException:"Object reference not set to an instance of an object." ). and the Application crashed. can anyone explain way ?
Thanks
Did you fix it ?
@@usefultutorials200 Did you fix it? lol
Can I use a repository pattern with .Net Core Entity framework so that I can switch out implementations? For example lets say I want to use a class like MockEmployeeRepository for testing but I also want the flexibility to use a SQL Server data store. Thanks !!!
well explained!
Nice, much clearer now. One question though, can you please tell us if this is possible to register multiple implementations of same interface using DI and selectively use one at constructor level or method or property level as required.
No you can't must have for each interface one implementation otherwise the DI engine can't decide which one to use
@@amineherizi4687 How would you inject dependencies in tutorial example, IEmployee, PermanentEmployee, ContractEmployee
@venkat, can you please explain how to inject multiple implementations of same interface otherwise if it is not possible doing so then what is use of interfaces and multiple classes inheriting same interface
You can use IEnumerable collection and linq for that. Inbuilt dependency injection is not supporting multiple classes from single interface. Below is the code sample.
Services :
public interface ITest
{
void writeMsg(string t);
}
class ATest : ITest
{
public void writeMsg(string t)
{
Console.WriteLine(t + "A");
}
}
class BTest : ITest
{
public void writeMsg(string t)
{
Console.WriteLine(t + "B");
}
}
Getting particular service through controller injection :
ITest _itest;
private readonly IEnumerable _itests;
public HomeController(IEnumerable itestList)
{
_itests = itestList;
_itest = _itests.First(o => o.GetType() == typeof(BTest));
}
Here you can get the instance of BTest class.
Hope this helps.
Hi Sir ,
one question here , As i have just one implementation here , but in reality i may implement 2 class with same interface utilization , now if i mentioned in start up file service.addsingleton and service.addsingleton, in this case what will happen . Please suggest .Thanks for your all videos , I really enjoying while learning with your videos .
Subscribed!
👍👍👍👍👍👍
Great
Thank you for this great tutorial! Is there a place we can ask questions about the tutorial?
Hello Tamar - Please ask your question on the respective video as a comment. Hope this helps.
@@Csharp-video-tutorialsBlogspot I'm wondering if what he really means is, have you ever thought about actually answering people's questions?
you make it so easy to understand concepts,why so fast sir?
Nice tutorial.....Is there any other way to register the service except to use the extension method????
are there any point of using other dependency injection library like autofac etc. in asp.net core?
not really unless you feel like there is a missing feature.
Just one question, in the real-world application there will be multiple Interfaces and Classes and that will be a little bit hard work to implement Injection for each Interface. Is there any other way so that we implement those Interfaces and classes an efficient way?
I think you have to implement them like this for every interface and so on. But you can make it look a little cleaner then filling all of them in your startup.cs.
You can make a Extension Method to keep all the DI's you want instead of filling all of them in your startup. It looks much cleaner.
public static class ServiceCollectionExtensions
{
public static IServiceCollection RegisterServices(
this IServiceCollection services)
{
services.AddTransient();
// and a lot more Services
return services;
}
}
And then in your startup.cs you write this for example.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.RegisterServices(); //
@@ApeKing Thank you so much 💗👍 Understood...
I really love your videos, but not this one. For me it was very confusing. Maybe a slide with a diagram of dependencies would have helped to make it clearer
how to handle DI if there are multiple implementation of the interface and want to use both??
Nice explain.
What if I want to use a class library? a class library does not include the startup.cs class.
I got an error in HomeController that "Inconsistent accessibility: parameter type 'IEmployeeRepository' is less accessible than method 'HomeController.HomeController(IEmployeeRepository)' " how to resolve that?
Is your IEmployeeRepository interface public?
InvalidOperationException: A circular dependency was detected for the service of type 'WebApplication3.Models.IEmployeeRepository'. Getting this error
Thanks a lot...
What if don't inject and when we need to change MockEmpRep() to SqlEmpRep() type we just changed the code inside the mock and as a result only one place needed to changed.? Please someone clarify me this.
can someone please explain to me how would I create an instance of HomeController? As I understand the DI Container will handle the dependency injection.