C#C
C#3y ago
__dil__

❔ Dependency Injection

Disclaimer: I come from a low-level language, never did Java and very new in C#.

I started to read the Avalonia UI MVVM tutorial, and it mentioned that usually you'd use DI to avoid coupling the model and the viewmodel.

This got me curious about what exactly is DI. My understanding is that you have a service that uses a dependency. Instead of using the dependency directly, you make the service use an interface for that service, and provide an instance of that interface somehow.

So, the "replace concrete type with interface" I get.

My main confusion is about how to provide the instance. From what I read, it's either

1) passing a concrete type instance to a constructor that expects the interface type.

Or

2) Something involving "frameworks and "automatic registration".

Option 1) is pretty intuitive, not much to be confused about here, but option 2) is complete gibberish to me.
Was this page helpful?