DEV Community

Discussion on: How YOU can Learn Dependency Injection in .NET Core and C#

Collapse
 
softchris profile image
Chris Noring

hey.. so the whole idea of DI, at least in my mind is about three things 1) reuse services that are expensive to create 2) be able to share state. 3) make testing easier/code needs modification less seldom, you rely on contracts, i.e Interfaces Singleton pattern is a clear case where we can share state. Because it's created only once, you have fields on that service that you write to, e.g a CartService can be a singleton with an Items fields that you can keep adding items to and you can ensure that there are no more than one CartService created