DI best practices
DI is integral to most ASP.NET projects, and minimal APIs are often particularly reliant on them. As a result, it’s important to ensure that we are following best practices when it comes to dependencies and the methods of accessing them.
There are some simple rules of thumb when it comes to implementing DI in minimal APIs. We look at these rules in the next few sections.
Avoiding the service locator pattern
There is an anti-pattern in minimal APIs known as the service locator pattern. In this pattern, instead of explicitly injecting your dependency, you inject IServiceProvider containing the dependency, and then you fish the service out of it within the body of your method or function.
An example of the service locator pattern is shown in the following code, in which the POST method we made for creating orders is altered to use IServiceProvider:
app.MapPost( "/order", ...