davidfowl / DotNetCodingPatterns

A collection of coding patterns in no particular order
1.38k stars 99 forks source link

Get or Create Service and IDisposable #13

Open dazinator opened 3 years ago

dazinator commented 3 years ago

There is a note at the bottom of this section: https://github.com/davidfowl/DotNetCodingPatterns/blob/main/1.md#creating-instances-of-types-from-an-iserviceprovider

NOTE: Disposable instances created with this API will not be disposed by the DI container.

It's interesting to note that ActivatorUtilities also has a GetServiceOrCreateInstance method. However its not possible to tell when calling this API, if the instance returned was resolved from a registration, or was not registered and so a new instance was created. Given the note above, if the service was IDisposable - how can the consumer know if they are, or are not responsible for disposing the instance when obtaining services like this? It feels like the GetServiceOrCreateInstance method should supply an out parameter indicating whether the service was returned and is being tracked, or whether an instance was created and therefore is not being tracked ?

davidfowl commented 3 years ago

Don't use that method to create disposable instances. Seriously though, we should document it, it's a flaw in the design.

ExceptionCaught commented 3 years ago

hi @davidfowl . another question related to this. There are a few cases where the IServiceProvider got injected into the constructor. Isn't this a service locator pattern? A few articles on the inernet see this as a anti-pattern and will not suggest to do so. What is the main factor when considering such pattern?

BorisWilhelms commented 3 years ago

@ExceptionCaught A common use-case for this, is when you have "incompatible" lifetimes. where you need to consume a scoped service inside of a singleton service. For example. using a Database Context (scoped) in Hosted Services (singleton).

ExceptionCaught commented 3 years ago

hi @BorisWilhelms . in this case, the Hosted Services is singleton and initiated during the application starts, does it mean we will do IServiceProvider.GetService<DBContext>() in one of the methods. but not in the constructor?

thoemmi commented 3 years ago

@ExceptionCaught In your singleton, you will have to create your own scope, like

using (var scope = serviceProvider.CreateScope())
{
    var dbContext = scope.ServiceProvider.GetService<DBContext>();
    // ...
}

Try to keep the scope's lifecycle as short as possible.