💡𝐃𝐞𝐩𝐞𝐧𝐝𝐞𝐧𝐜𝐲 𝐈𝐧𝐣𝐞𝐜𝐭𝐢𝐨𝐧 𝐢𝐧 .𝐍𝐄𝐓 Dependency injection involves providing a class with its required dependencies from an external source rather than having the class create them itself. This helps to decouple the object creation process from the caller, leading to a more modular and flexible system. In other words, it allows a class to focus on its core functionality rather than worrying about how to create and manage its dependencies. 𝐖𝐡𝐲 𝐝𝐨 𝐰𝐞 𝐧𝐞𝐞𝐝 𝐝𝐞𝐩𝐞𝐧𝐝𝐞𝐧𝐜𝐲 𝐢𝐧𝐣𝐞𝐜𝐭𝐢𝐨𝐧? By using DI classes are decoupled from each other so you make changes at one place and it is reflected all over the places. 𝐇𝐨𝐰 𝐭𝐨 𝐢𝐦𝐩𝐥𝐞𝐦𝐞𝐧𝐭 𝐝𝐞𝐩𝐞𝐧𝐝𝐞𝐧𝐜𝐲 𝐢𝐧𝐣𝐞𝐜𝐭𝐢𝐨𝐧? ✅ In .NET 6 we implement DI in Program.cs class by using builder.Services ✅ For previous versions of .NET, to implement DI we need to add the service in “ConfigureServices” method which is in Startup.cs file 𝐃𝐢𝐟𝐟𝐞𝐫𝐞𝐧𝐭 𝐰𝐚𝐲𝐬 𝐨𝐟 𝐢𝐦𝐩𝐥𝐞𝐦𝐞𝐧𝐭𝐢𝐧𝐠 𝐃𝐞𝐩𝐞𝐧𝐝𝐞𝐧𝐜𝐲 𝐈𝐧𝐣𝐞𝐜𝐭𝐢𝐨𝐧 There are three ways of doing DI: 1.Scoped ➡️ It will create an instance per scope, if we are in same scope same instance would be used. Whenever we go out of scope new instance would be created. 2. Transient ➡️ It creates new instances every time its injected. 3. Singleton ➡️ It instantiates one global object for all requests coming to the server from any user. 𝐁𝐞𝐧𝐞𝐟𝐢𝐭𝐬 𝐨𝐟 𝐃𝐞𝐩𝐞𝐧𝐝𝐞𝐧𝐜𝐲 𝐈𝐧𝐣𝐞𝐜𝐭𝐢𝐨𝐧 1.Improved testability: Dependency injection makes it easier to write unit tests for your code, as you can easily substitute mock objects for the real dependencies. 2. Enhanced flexibility: By injecting dependencies from the outside, you can easily change the implementation of a class's dependencies without having to modify the class itself. This makes it easier to adapt your application to changing requirements. 3. Increased modularity: Dependency injection encourages the use of small, single-purpose classes that are easy to test and reuse in different contexts. This can lead to a more modular and maintainable codebase. 4. Better separation of concerns: Dependency injection helps to separate the concerns of different parts of your application, making it easier to understand and maintain the code. 5. Enhanced decoupling: Dependency injection promotes loose coupling between classes, which can make your application more resilient to change and easier to test. 📩If you like my posts you might be interested in my weekly Dotnet Newsletter where I send an actionable tip on every Saturday , Subscribe https://lnkd.in/dNHxJGRG .#dotnetcore #dotnet #csharp #dependencyinjection #di #dependency #singletondependency #scopedependency
Great post. I would like to add a bit of my thought here. Most of the time we associate DI with classes and more often with the help of interfaces. There is another perspective that relates more to functional programming and a higher-order paradigm where DI relates to callback functions. In .net we could understand it with the use of Delegate. In other terms, we could also define DI just over methods too. A higher-order function seeks dependency in terms of function as an argument to do its work.
Can you share more on "Scoped"? Scope means user or function? I am always getting confused on this
I will add a something different approach a little bit more generic. xxxxx.AddScoped(typeof(IGenericRepository<>),typeof(GenericRepository<>)); or xxxxx.AddScoped(typeof(IGenericRepository<,>),typeof(GenericRepository<,>)); The different in 2 samples is the number of generic parameter(s) in the generic Interface/Class
Great share and great explanation. In the image it is not clear the transient lifecycle. In the same request I can use more instance of the same class injected
One of my favorite questions to ask in an interview - service lifetimes in dependency injection. Doesn't applies to .NET only. Same service lifetimes are pretty common in all other languages as well. These are the basics every software engineer should be aware of. Good post!
I would prefer to use singleton as it instantiates one object to whole life cycle of requests on server, that will take less memory space as compared to others. correct me if I'm wrong. Btw Great post.!
which one have a better performance?
Nice post, thanks for sharing it with everyone on LinkedIn, a nice follow up to this would be how the pictured interfaces help with inversion of control!
Thanks for the sharing. In my opinion visual representation here is not clear and a bit misleading. 1. Naming of parameters are ticklish 🥲. 2. Same type of repository passed 2 times in same controller is something you will usually never see in a real scenario. 3. If in scoped scenario request 2 is injecting instance 2, transient scenario request 2 would inject instance 3 and instance 4. Have a nice day.
I help you boost your .NET to the next level
2yI'm starting a weekly newsletter related to .NET updates; subscribe to it if you're interested. https://mwaseemzakir.substack.com/