DI in Asp.net core, abstract

I'm just now getting into the DI stuff in asp.net, I've used DI patterns before but only in xaml and windesktop stuff, and even then I never really understood it.

Having gone over the docks thoroughly and many a time... I'm starting to get a grasp if you could help me confirm the things i've gathered. I'm in the process of writing a bigger piece of code based on this, I'll put it up in review channel once I'm done.

things I've noticed:
DI is basically a global Object with a bunch of predefined interfaces: With the service-collection implementation at app level you define a collection of interfaces which all other elements of your code "could" depend on and instead of passing the object around you pass around the interface you depend on. You then provide the implementation at app level and you're done, asp.net resolves the interface to the object dependency at runtime.

The instantiation gets moved to startup.cs: With .net 8 startup.cs isn't a thing anymore, where you normally would run your singletons, factories and configurations which would have dependencies in the environment and change their implementation. Now it gets done in the appbuilder.Services.Add--(ConfigClass). In the config class you run your configs, which should be a jsonfile with configurations of the implementation.

move of imperative/boilerplate code from application logic: Without DI you're stuck doing configuration logic for each object either in your razor page or your controller. Like when your model has dependencies in some other REST-client so you run the request in the controller, when all you really want to do is to have it populated with the data. To avoid this you move the population logic and request instantiation and response handling to your DI container, and in the controller you can just run the interface function.

If i missed anything, or have some misconceptions type it please out 🙂
Was this page helpful?