Now I know I have been using it sometimes not knowing about ))) and not fully understanding why and how... And sometimes I did not use it when I should have been... Now because of your simple and clear video I know why, how and what to do! Thanks a lot!
THIS is the way to introduce dependency injection! I really appreciate showing the traditional method, explaining D.I.P. and implementing the interface. Really helped me understand the basic principals.
this is more like a real life example, I'm tired of all the animal and car examples. short and sweet, now I can understand it from a theory perspective. thank you!
Creating an instance of a Garage Class and adding populating its CarList Property with a List of Car instances only makes sense to someone if they already understand OO Principles. Otherwise, it is meaningless nonsense. I love examples that mean and do something.
I made a drastic change in my career path from Physics Engineering to Software , OpenSource and other sharing cultures in this field stick out from the rest, thanks for your contribution , I'll have mine in years to come , stay afloat :)
wow thank you soo much you make it soo easy to understand I did not understand in the first time when I watched the video but when I watch it again and focus, I understand it so much
Can we use DI to pass data between pages. What happens when the use reloads the pages. I know that for Blazor Server for example the connection is lost so the data is lost.
I don't see a scenario where you directly use DI for page navigation. But take a look at the following video where I explain a lot about state handling in Blazor components. th-cam.com/video/Thwozpi0V58/w-d-xo.html
@@ClaudioBernasconi What in your opinion is the best way to share state between pages? Say that I want to store the user's details once the user has logged in . The way I do it is to have a class (or interface) with the right properties , register this in the DI and inject that interface in the page I want to retrieve it from. However as I explained above this gets lost when the user reloads the page. Any thoughts om that?
@@ClaudioBernasconi The way I thought of it is to create a class , insert that in the DI system as Scoped Service and then inject in any page. 'is that a problem? The only problem I have with that is that it doesn't survive page refreshes.
I have a question for you Claudio. Now with latest .NET version... Do you think is necesary use Autofac as Dependency Injection manager? What's your opinion?
Great question! For newer tech stacks like aspnetcore the built-in DI solution is enough for 95% of use cases. I still recommend using Autofac for technologies that don't have that, for example, WinForms or WPF, which both run on dotnet6.
Thanks a lot for this valuable explanaiton. in case you have a new class, also, you will create an interface to it to register it, so, the place you want to consume it, you have to change the interface injection to the new interface, please correct me if I'm wrong. Also, I have inheritance "Abstract Class" with some common implementation for subclasses, using DI with inheritance is impossible, because they are different concepts. I tried it a lot, I didn't succeed. Appreciate if you correct me if I'm wrong. Thanks
It's hard to discuss specific code situations in TH-cam comments. I don't understand your question, I'm sorry. I suggest creating a code example on GitHub and sharing that repository with other developers, for example on StackOverflow or in similar communities. Feel free to tag me on Twitter and I will take a look at it.
One very huge benefit of using dependency injection is testability. I was just writing some Unity stuff and class A is depending on values coming from class B (database connector). I really just wanna test class A however. Class B is very complex. So I implemented dependency injection. In real game class A gets real implementation of class B. In unit test however class A gets mocked class B that is simply returning some hoo-haa (and not requiring real database) so that I can focus on testing class A. I don't know how would I have achieved this without DI.
Thanks for using interfaces to explain it, Q. what if user class has some variables to give to notification service while creating it? And you don't know that variable in program class.
You cannot provide information from the User class to the NotificationService when creating it. What you want to do is provide all information when calling the notify method.
At 4:25 you made the INotification Interface depend on the "User" class. Would be better if this depended on an abstraction rather than a class called "User".
In this case, I directly reference the User class because the class does not contain logic and therefore is a data class. You can always define an IUser interface. However, I don't think that you gain a lot in this case. Can you elaborate on why you would do it? I'm keen to learn more about it.
Your explanation on Dependency Injection is very Nice. Thank you very Much. But i tried adding webNotification class, and do have to changes not only in Program.cs but also in User.cs . Is there anyway to do the same without modifying User and not sending notification Service as a list.
Thanks for your feedback. It is correct that you have to change the Program.cs file. But you do not need to change the User class because it accepts an instance of type INotificationService. If you implement WebNotification and inherit from INotificationService, you can pass an instance of that type to the User class without changing anything. The idea here is to replace the current ConsoleNotification. If you want to have both notifications, then you are right. In this case, you need to change the second argument of the user class to a list of INotificationService. I hope this helps.
Yes, DIP is about replacing an instance reference with an interface when using constructor injection but with your example you have brought up a new reference of ConsoleNotification in client class (Program -> void Main()). With this, you have allowed the method UserNameChanged be invoked in the open, making it confusing and not so intuitive. I hope you get my point, if all you wanted is to make the notificationService available in Main() then whats the point of using the same in another constructor of User class ? Rather, you would want to achieve this via an IRepository of some sort in a MVC / WebApi Controller class using constructor injection. Its more appropriate to be used in that way.
I appreciate your feedback. I read the comment multiple times, and I am sorry, I don't understand. Maybe you could add a diagram showing what you mean? Anyway, I'm sure you got some value out f the video, and I appreciate you took the time to let others know about your idea. Again, thank you.
If you change your interface, all implementations of said interface will have to change. That's why you want to have stable interfaces and concrete (exchangable) implementations in your software system.
Hi Claudio, as I interpret it you changed the programs behaviour. Before you instantiated the notification service inside the "User" class which represents a so called composition. Changing this by relocating the instantiation of the notification service outside of this class handing the instance over as an input parameter makes the "User" and the "NotificationService" be connected as an aggregation instead of a composition, which is not the same. To do so, instead of handing over the instance as a input parameter you have to define a "constructor" class (e.g. "NotificationService") implementing a function containing "... = new ConsoleNotification()" and returning an instance of that (in this case) "ConsoleNotification" class which than is called by the "User" constructor.
Hi Niklas, thanks for your comment. Yes, I changed the program to decouple the notification mechanism from the User class. I made a structural change to how the classes are composed. I admit that I am not aware of how those academic terms like composition and aggregation differentiate themselves. However, I think I clearly explained how Dependency Injection works and how you can implement it in C#. Do you see any issue with my explanation? Do you think I explained it wrong?
@@ClaudioBernasconi Well this is funny. Sorry, I just was incompetent! No you are absolutely right. Well what happened is, that I found out that dependency injection is about converting it from a composition (instantiate object inside a constructor) to an aggregation (hand over the reference of an object to the constructor). So, sorry for making you getting confused about my claim. Its coexistence of multiple word for the same thing is little nasty.
@@niklasroth2508 No worries, I'm glad you wrote again. Programming can be confusing, especially if there are multiple terms for the same thing or vice-versa. Anyway, thank you for your feedback, and I wish you all the best.
Hello, I understand this case is meant to be a simple example. But in a situation like this, would there be any reason to not just make ConsoleNotification a static instead of instancing or injecting?
Thanks for your question. You cannot exchange static implementations at runtime. And you also cannot fake static method calls in unit/integration tests. That's the main reason why this pattern can make sense even in smallish projects.
Thanks for your feedback. What would you want to abstract in the User class? You could extract the changeUsername behavior into its own class and use the same mechanism to separate an interface and its implementation. But if the class remains as shown in the class diagrams, I'm not sure how you would like to do it. Feel free to explain your question in more detail. I'm happy to help out as much as I can.
Like delegates and events, dependency injection allows you to separate abstraction from implementation. Nonetheless, those are different concepts that solve different problems. I hope you learned something from this video.
Your notification interface is depending from your user implementation which not necessarily is a problem, but it's not shown in the dependency diagram and could be changed by adding an user interface and using this as parameter for the notification method.
Hi Brah, thanks for your feedback. For me, clickbait means the content is not delivering on the promise of either the title or the thumbnail of a video. This video is not titled "Why DI is useful". It is titled "Introduction to Dependency Injection in C#" and 50'000+ learned something from this video. However, I'm sure there are other people uploading videos in their free time for you to watch for free who can explain what you want to learn. In case you want to learn it from me: Dependency Injection helps you inverse the direction of the dependency. Instead of having a tightly coupled system where classes depend on other concrete implementations, you use interfaces and depend on those. It allows you to swap the implementation of a given interface without modifying the other parts of your system. Is it a pattern you should always use? No. Should you learn about how it works and you want to use it in your projects? That's entirely up to you.
You use User type forthe parameter in method NotifyUserNameChanged in Interface INotificationService. Doesn't that mean you have the interface depending on User class? Kind of breaking the concept of abstraction. May be you should write "void NotifyUserNameChanged (object user);" instead?
I'd say, in this case, it is okay to use the User class. It is a data class that does not reveal business logic to its consumer. And we want to build applications sharing data, right? You could technically abstract the user class, but in my opinion, there needs to be an apparent reason for that. I cannot see that in this example.
Hello, sweet video. One question, is it necessary to inject into constructor? Is it possible to use injection as method arguments? For example if we have a method for user : SetNotificationService(INotification service) ? I am asking because , we cannot create instance of user without this service in your example , and i think it is not very good(it creates another dependency). What do you think? And what if we want later to change this service from console to web service ? In your example it is not possible
It is tough to understand and solve your problem in a TH-cam comment. Consider asking your questions as separate questions on Stackoverflow. What I can tell you is that property and method injection are possible using Autofac (autofaccn.readthedocs.io/en/latest/register/prop-method-injection.html). However, you have to remember that it is still a dependency between two types, whether you use constructor or method injection.
I typed out this demo and it didn't work. You are trying to put a ConsoleNotification object (notificationService) in the User class constructor that requires a INotificationService type. At this line in the Program class... var user1 = new User("Tim", notificationService); the contrusctor is wanting a iNotificationService type and you are giving it a ConsoleNotification object.
That should work due to polymorphism. The ConsoleNotification class should implement the INotificationService interface. Therefore you can provide an instance of the ConsoleNotification class whenever an argument of type INotificationService is expected.
Yeah, but in the end you still have a dependency to INotificationService. We've just changed our dependency from one type to another. Though, I guess we can now drink Coors, Miller, Budweiser or Michelob, instead of just one, just by using IBeer interface.. :-)
The difference is that if Miller needs wheat as a dependency and Budweiser needs rye, you don't have a dependency from the brewery class to all those ingredients but only to IBeer. It does not matter how many more beer implementations you add. That's the trick here.
Would be great if you would leave download or Github links to your code. Otherwise great. Edit: With this nice small example it is also okay without :)
Thanks for your feedback, Tiari! Unfortunately, there is no repo for this example, but I'll remember that this is something people would benefit from. Thanks. 😎
The User class is a data class and does not contain an implementation (logic). In this example, abstracting the User class does not make sense. It's important to differentiate between classes that implement logic and data classes to understand why abstractions help to decouple your application.
5 ปีที่แล้ว +3
I always had a hard time wrapping my head around dependancy injection. your video helped me understand better. thanks man!
I always thought dependency injection was a hard topic because it was always explained to me in overly complicated ways. People would mention dependency injection in certain parts of our codebase, and I never understood it. I always thought... "Where is this magical dependency injection you speak of?" It turns out I was using it all along without even realizing it 😂
Don't worry. It isn't a simple topic. Maybe try to code along with this video and build your own example? If you have a specific question, ask it on StackOverflow with your code sample. I'm sure people will help you. It took me a few weeks to understand the topic when I first encountered it years ago. Don't give up.
SSSSSSSSSSSSSSS sound very noisy .... all data from the vedio was a goooooood but when you speek ( s ) Sound vary very noise ... sory about this comment
Thanks for your feedback. I don't think it's very noisy, however, this video is almost 3 years old and I improve the audio quality on future videos. You might want to take a look.
Have you ever used Dependency Injection? Do you like it? Let me know what you think!
Now I know I have been using it sometimes not knowing about ))) and not fully understanding why and how... And sometimes I did not use it when I should have been... Now because of your simple and clear video I know why, how and what to do! Thanks a lot!
@@ArkFen Thank you very much for your kind feedback. Stay safe.
Thank you claudio, for this simple, short and clear example. I hope you expand this to add more classes, like business and data layer with ninject.
But the DI in the ctor have a contra then in a prop oder in a method
THIS is the way to introduce dependency injection! I really appreciate showing the traditional method, explaining D.I.P. and implementing the interface. Really helped me understand the basic principals.
Thank you so much for the feedback, Rush. I'm glad you learned something from this video. :-)
this is more like a real life example, I'm tired of all the animal and car examples.
short and sweet, now I can understand it from a theory perspective.
thank you!
Thanks for your kind feedback. I am glad it helped you understand that topic.
Creating an instance of a Garage Class and adding populating its CarList Property with a List of Car instances only makes sense to someone if they already understand OO Principles. Otherwise, it is meaningless nonsense. I love examples that mean and do something.
you helped me in a way I can't even explain, thanks a lot!
Don't worry. I'm glad to have you here. 🙂
5:44-5:48 had to hear it 3 times and I am like "ooooh, I get it now" thank you very much.
I struggled to grasp exactly what the Dependency Inversion principle meant. Thank you for the simple and straightforward explanation ❤
Great to see you learned something from my video. You are awesome!Thanks for watching. 👍
Just wanted to say how useful this was in explaining a topic I was otherwise struggling to understand! Thank you!
Thank you, Benn.
This video should have a minimum of a million views. What an amazing video!
You made my day! Nobody has ever said such a nice thing about one of my videos. 🙏🙃
This is the best explanation I have seen of Dependency Injection.
Thank you so much for your kind comment!
Now that is what we call a short but up to the point explanation!! Kudos..!
Thank you so much. It takes a lot of time to create videos like this. 😊
@@ClaudioBernasconi Agree 100% I can feel the efforts 🙂
I made a drastic change in my career path from Physics Engineering to Software , OpenSource and other sharing cultures in this field stick out from the rest, thanks for your contribution , I'll have mine in years to come , stay afloat :)
Thank you.
An instructor who uses light IDE theme! That's just what I needed.
Thanks for your comment. 👋🙂
I know you made this 4 years ago, but as a SE student learning about OOP this is a huge help. Ty!
Great to hear this little video is still of help to you after so many years. Keep on pushing, and you'll become a great developer! 👏
Short, sweet and to the point. Thank you
Thank you so much for your kind feedback.
Ok for some reason no one else could explain this to me and now I understand it and feel like its so simple. Appreciate you
Thank you so much for your kind words.🤝 I'm happy you learned something from my video. 🙂
wow thank you soo much you make it soo easy to understand I did not understand in the first time when I watched the video but when I watch it again and focus, I understand it so much
Wow, thank you so much. 🙏
This is best explanation about D.I I've come across!
Thanks a lot Claudio!
Thank you! 😎
Thank you Claudio Bernasconi, finally got the point about Dependency Injection and I'm grateful about that.
Thank you, Igor!
A great example: short, simple yet explaining the essence of DI. Not many are capable of doing this.
Thank you so much for your feedback. I'm happy the video was helpful to you. 🙂
Thanks for clearing it up!
Well explained! Thanks a million mate!
Thank you for watching. 😎🙏
The explanation was 10/10! Love it!
Thank you!
Great little video here, perfectly paced and very clear.
Many thanks!
This was well explained and had nice pacing. Great video!
Thanks a lot. 🙏
I saw a lot of videos on DI but did not understand a thing. But this video was very simple and easy to understand :)
Thank you for your kind words.
Very concise explanation. Well done.
Thank you. 😎
Danke! Jetzt has sogar ich verstande 😁
Can we use DI to pass data between pages. What happens when the use reloads the pages. I know that for Blazor Server for example the connection is lost so the data is lost.
I don't see a scenario where you directly use DI for page navigation. But take a look at the following video where I explain a lot about state handling in Blazor components. th-cam.com/video/Thwozpi0V58/w-d-xo.html
@@ClaudioBernasconi What in your opinion is the best way to share state between pages? Say that I want to store the user's details once the user has logged in . The way I do it is to have a class (or interface) with the right properties , register this in the DI and inject that interface in the page I want to retrieve it from. However as I explained above this gets lost when the user reloads the page. Any thoughts om that?
@@ClaudioBernasconi The way I thought of it is to create a class , insert that in the DI system as Scoped Service and then inject in any page. 'is that a problem? The only problem I have with that is that it doesn't survive page refreshes.
This was great. I get it now. :) Thank you.
Thanks Gordon, very much appreciated.
Well explained. Thanks
very informative I like the way you explain concept with mode details. thanks
Thank you very much. 😉
@@ClaudioBernasconi You are welcome and you have a new subscriber 🙂
I have a question for you Claudio.
Now with latest .NET version... Do you think is necesary use Autofac as Dependency Injection manager?
What's your opinion?
Great question! For newer tech stacks like aspnetcore the built-in DI solution is enough for 95% of use cases. I still recommend using Autofac for technologies that don't have that, for example, WinForms or WPF, which both run on dotnet6.
@@ClaudioBernasconi
Thanks Claudio
Have a nice day
Simple, quick and precise. Subbed.
Thank you very much. I'm glad you liked the video.
thanks! very clear explnation!!
Thank you. 😉
Finally i understood, Thank you so much !
Glad it helped!
Thanks for the video. It is useful. I'm trying to get a hang of dependency injection in an API test automation project.
Thank you for providing feedback. I wish you all the best on your journey.
Thanks a lot for this valuable explanaiton. in case you have a new class, also, you will create an interface to it to register it, so, the place you want to consume it, you have to change the interface injection to the new interface, please correct me if I'm wrong. Also, I have inheritance "Abstract Class" with some common implementation for subclasses, using DI with inheritance is impossible, because they are different concepts. I tried it a lot, I didn't succeed. Appreciate if you correct me if I'm wrong. Thanks
It's hard to discuss specific code situations in TH-cam comments. I don't understand your question, I'm sorry. I suggest creating a code example on GitHub and sharing that repository with other developers, for example on StackOverflow or in similar communities. Feel free to tag me on Twitter and I will take a look at it.
One very huge benefit of using dependency injection is testability. I was just writing some Unity stuff and class A is depending on values coming from class B (database connector). I really just wanna test class A however. Class B is very complex. So I implemented dependency injection. In real game class A gets real implementation of class B. In unit test however class A gets mocked class B that is simply returning some hoo-haa (and not requiring real database) so that I can focus on testing class A. I don't know how would I have achieved this without DI.
Thank you very much for the video! Very good explanation, the example is super simple and clear
Thanks for your comment. I'm happy if it helps. 🙂
thank you!!!
Thanks for using interfaces to explain it, Q. what if user class has some variables to give to notification service while creating it? And you don't know that variable in program class.
You cannot provide information from the User class to the NotificationService when creating it. What you want to do is provide all information when calling the notify method.
@@ClaudioBernasconi Ohh, got it. Thanks 👍
1 more Q. Does this mean In every case, I should have blank constructor of low level modules?
Or initialize dependency with all needed properties in program class and then pass it into dependent module
At 4:25 you made the INotification Interface depend on the "User" class. Would be better if this depended on an abstraction rather than a class called "User".
In this case, I directly reference the User class because the class does not contain logic and therefore is a data class. You can always define an IUser interface. However, I don't think that you gain a lot in this case. Can you elaborate on why you would do it? I'm keen to learn more about it.
Very clear and concise explanation and examples. Thank you and well done!
Thank you very much.
Also, you gave the exact usage of Interfaces. Thank you!
Thank you. 😎
Very nice and easy to follow explanation. Good work.
Thanks for the love that you dedicated for explaining this important concept on this video.
Thank you so much for your kind words. I appreciate your feedback a lot.
Hi, your teaching method is nice, have you considered creating tutorials on C# design patterns?
Thanks a lot. I haven't recorded anything yet, but I might in the future. 😎
@@ClaudioBernasconi Looking forward to it, i hope sooner than later. cheers
Thanks well explained!
Easy to understand
Thank you so much, Xavier. I'm glad my video helped you understand the topic.
awesome explanation mate!
Thank you!
thank you so much bro , it was helpful
Glad to hear that, Miad.
Very clear explanation.
Thank you 🙂
Thank you that's what I needed to know.
Your explanation on Dependency Injection is very Nice. Thank you very Much. But i tried adding webNotification class, and do have to changes not only in Program.cs but also in User.cs . Is there anyway to do the same without modifying User and not sending notification Service as a list.
Thanks for your feedback. It is correct that you have to change the Program.cs file. But you do not need to change the User class because it accepts an instance of type INotificationService. If you implement WebNotification and inherit from INotificationService, you can pass an instance of that type to the User class without changing anything. The idea here is to replace the current ConsoleNotification. If you want to have both notifications, then you are right. In this case, you need to change the second argument of the user class to a list of INotificationService. I hope this helps.
Hi Sir,
Please make video on difference between Finalize vs Dispose in C# (with program)
Great tutorial, thank you
Yes, DIP is about replacing an instance reference with an interface when using constructor injection but with your example you have brought up a new reference of ConsoleNotification in client class (Program -> void Main()). With this, you have allowed the method UserNameChanged be invoked in the open, making it confusing and not so intuitive. I hope you get my point, if all you wanted is to make the notificationService available in Main() then whats the point of using the same in another constructor of User class ?
Rather, you would want to achieve this via an IRepository of some sort in a MVC / WebApi Controller class using constructor injection. Its more appropriate to be used in that way.
I appreciate your feedback. I read the comment multiple times, and I am sorry, I don't understand. Maybe you could add a diagram showing what you mean? Anyway, I'm sure you got some value out f the video, and I appreciate you took the time to let others know about your idea. Again, thank you.
Does changing the components inside the INotification interface affects all the other classes that are depending on it? PS: I am new to programming.
If you change your interface, all implementations of said interface will have to change. That's why you want to have stable interfaces and concrete (exchangable) implementations in your software system.
This is very informative. Thanks Claudio :)
Thank you very much.
A great help, thank you very much.
Thank you. 🙏
Excellent and simple!
May I ask what tool you used for your classes diagram?
I made them using Lucidchart (Lucidapp). They are made by hand, not generated from code.
@@ClaudioBernasconi Nice tool, I just looked onto their site. I used yEd before, which is great, too, but not so for UML etc.
Danke!
Thank you.Very well explained
Very helpful, Thanks a lot
Thank you. 🙂
Hi Claudio, as I interpret it you changed the programs behaviour. Before you instantiated the notification service inside the "User" class which represents a so called composition. Changing this by relocating the instantiation of the notification service outside of this class handing the instance over as an input parameter makes the "User" and the "NotificationService" be connected as an aggregation instead of a composition, which is not the same.
To do so, instead of handing over the instance as a input parameter you have to define a "constructor" class (e.g. "NotificationService") implementing a function containing "... = new ConsoleNotification()" and returning an instance of that (in this case) "ConsoleNotification" class which than is called by the "User" constructor.
Hi Niklas, thanks for your comment. Yes, I changed the program to decouple the notification mechanism from the User class. I made a structural change to how the classes are composed. I admit that I am not aware of how those academic terms like composition and aggregation differentiate themselves. However, I think I clearly explained how Dependency Injection works and how you can implement it in C#. Do you see any issue with my explanation? Do you think I explained it wrong?
@@ClaudioBernasconi Well this is funny. Sorry, I just was incompetent! No you are absolutely right. Well what happened is, that I found out that dependency injection is about converting it from a composition (instantiate object inside a constructor) to an aggregation (hand over the reference of an object to the constructor).
So, sorry for making you getting confused about my claim. Its coexistence of multiple word for the same thing is little nasty.
@@niklasroth2508 No worries, I'm glad you wrote again. Programming can be confusing, especially if there are multiple terms for the same thing or vice-versa. Anyway, thank you for your feedback, and I wish you all the best.
Could you give me an example of what a WebNotification service would look like in this application program class? Thanks !!!!
class WebNotification : INotificationService { public void notifyUsernameChange(string username) { //call some web API to send web notifications } }
@@ClaudioBernasconi Thanks !!!
very nice video!
Thank you 🙏
Hello, I understand this case is meant to be a simple example. But in a situation like this, would there be any reason to not just make ConsoleNotification a static instead of instancing or injecting?
Thanks for your question. You cannot exchange static implementations at runtime. And you also cannot fake static method calls in unit/integration tests. That's the main reason why this pattern can make sense even in smallish projects.
@@ClaudioBernasconi thanks!
Great video! Short and sweet. I do have a question though. Strictly speaking wouldn’t you also want to make a similar abstraction for the User class?
Thanks for your feedback. What would you want to abstract in the User class? You could extract the changeUsername behavior into its own class and use the same mechanism to separate an interface and its implementation. But if the class remains as shown in the class diagrams, I'm not sure how you would like to do it. Feel free to explain your question in more detail. I'm happy to help out as much as I can.
is this like delegates & events?
Like delegates and events, dependency injection allows you to separate abstraction from implementation. Nonetheless, those are different concepts that solve different problems. I hope you learned something from this video.
damn, my mind is blown. Such a useful feature
Thanks, Anthony.
Your notification interface is depending from your user implementation which not necessarily is a problem, but it's not shown in the dependency diagram and could be changed by adding an user interface and using this as parameter for the notification method.
I guess you are right about that. Thanks for adding that piece to the puzzle.
i wish this video would explain what DI is useful for. and only then dive into "how to use it".
Hi Brah, thanks for your feedback. For me, clickbait means the content is not delivering on the promise of either the title or the thumbnail of a video. This video is not titled "Why DI is useful". It is titled "Introduction to Dependency Injection in C#" and 50'000+ learned something from this video. However, I'm sure there are other people uploading videos in their free time for you to watch for free who can explain what you want to learn.
In case you want to learn it from me: Dependency Injection helps you inverse the direction of the dependency. Instead of having a tightly coupled system where classes depend on other concrete implementations, you use interfaces and depend on those. It allows you to swap the implementation of a given interface without modifying the other parts of your system.
Is it a pattern you should always use? No. Should you learn about how it works and you want to use it in your projects? That's entirely up to you.
@@ClaudioBernasconi i agree. edited.
You use User type forthe parameter in method NotifyUserNameChanged in Interface INotificationService. Doesn't that mean you have the interface depending on User class? Kind of breaking the concept of abstraction. May be you should write "void NotifyUserNameChanged (object user);" instead?
I'd say, in this case, it is okay to use the User class. It is a data class that does not reveal business logic to its consumer. And we want to build applications sharing data, right? You could technically abstract the user class, but in my opinion, there needs to be an apparent reason for that. I cannot see that in this example.
Hello, sweet video. One question, is it necessary to inject into constructor? Is it possible to use injection as method arguments? For example if we have a method for user : SetNotificationService(INotification service) ? I am asking because , we cannot create instance of user without this service in your example , and i think it is not very good(it creates another dependency). What do you think? And what if we want later to change this service from console to web service ? In your example it is not possible
It is tough to understand and solve your problem in a TH-cam comment. Consider asking your questions as separate questions on Stackoverflow. What I can tell you is that property and method injection are possible using Autofac (autofaccn.readthedocs.io/en/latest/register/prop-method-injection.html). However, you have to remember that it is still a dependency between two types, whether you use constructor or method injection.
I typed out this demo and it didn't work. You are trying to put a ConsoleNotification object (notificationService) in the User class constructor that requires a INotificationService type. At this line in the Program class... var user1 = new User("Tim", notificationService); the contrusctor is wanting a iNotificationService type and you are giving it a ConsoleNotification object.
That should work due to polymorphism. The ConsoleNotification class should implement the INotificationService interface. Therefore you can provide an instance of the ConsoleNotification class whenever an argument of type INotificationService is expected.
thank you - this is soo good
Good video.
Thank you.
Yeah, but in the end you still have a dependency to INotificationService.
We've just changed our dependency from one type to another.
Though, I guess we can now drink Coors, Miller, Budweiser or Michelob, instead of just one, just by using IBeer interface.. :-)
The difference is that if Miller needs wheat as a dependency and Budweiser needs
rye, you don't have a dependency from the brewery class to all those ingredients but only to IBeer. It does not matter how many more beer implementations you add. That's the trick here.
@@ClaudioBernasconi Good point.
Would be great if you would leave download or Github links to your code. Otherwise great. Edit: With this nice small example it is also okay without :)
Thanks for your feedback, Tiari! Unfortunately, there is no repo for this example, but I'll remember that this is something people would benefit from. Thanks. 😎
Thanks
🙂
I still don't get it... The notification still depends on the User since it gets User instead of IUser.
The User class is a data class and does not contain an implementation (logic). In this example, abstracting the User class does not make sense. It's important to differentiate between classes that implement logic and data classes to understand why abstractions help to decouple your application.
I always had a hard time wrapping my head around dependancy injection. your video helped me understand better. thanks man!
Thank you very much for your kind feedback.
thanks a lot brother
Thank you, Mohamed!
3:10 you don't put your code in a single class???? Fuck I've been doing it wrong for years!!! Thanks for the lesson.
I always thought dependency injection was a hard topic because it was always explained to me in overly complicated ways.
People would mention dependency injection in certain parts of our codebase, and I never understood it. I always thought... "Where is this magical dependency injection you speak of?"
It turns out I was using it all along without even realizing it 😂
Yeah, sometimes topics feel overwhelming or complicated when they aren't. Glad this video helped you understand Dependency Injection. 😎
still dont get it :(
Don't worry. It isn't a simple topic. Maybe try to code along with this video and build your own example? If you have a specific question, ask it on StackOverflow with your code sample. I'm sure people will help you. It took me a few weeks to understand the topic when I first encountered it years ago. Don't give up.
👌👍
SSSSSSSSSSSSSSS sound very noisy .... all data from the vedio was a goooooood but when you speek ( s ) Sound vary very noise ... sory about this comment
Thanks for your feedback. I don't think it's very noisy, however, this video is almost 3 years old and I improve the audio quality on future videos. You might want to take a look.
without source code provided, programming tutorial becomes time consuming and confusing to learn.
I suggest you watch all the videos of the series and get your copy of the source code here: github.com/claudiobernasconi/DependencyInjection.git