ServiceProviderExtensions architecture #539
Description
I've been trying to understand our service provider architecture and I've got some questions/suggestions regarding ServiceProviderExtensions
.
Spot the difference
- What is the difference between
GetService
andTryGetService
inServiceProviderExtensions
? Documentation says thatTryGetService
is a "Safe variant of GetService that doesn't throw exceptions if the service is not found." except AFAICSGetService
just callsTryGetService
, casts the result and doesn't throw anyway. - What is the difference between
TryGetService
andGetExportedValue
inServiceProviderExtensions
? They appear to be the same, even though they don't call each other (the code appears to be simply copy/pasted and modified slightly).
Trying to be too clever?
I think understand what the GetService
, TryGetService
etc methods on ServiceProviderExtensions
are trying to do: if the service provider is an IUIProvider
, then call GetService
on it, otherwise get an instance of IUIProvider
from the service provider and call GetService
on that. However I wonder if this is more confusing than helpful:
- If you call
provider.GetService(type(T))
then this mechanism will be bypassed, meaning thatprovider.GetService(type(T))
will give different results toprovider.GetService<T>()
- It masks the fact that there are different service providers for different types of resources.
IMO it would be clearer to see e.g.:
var uiProvider = serviceProvider.GetService<IUIProvider>();
uiProvider.GetSerivce<IConnectionManager>();
Even if this is longer, it gives me some important information: it that tells me that serviceProvider
at that point isn't an IUIProvider
and that an IUIProvider
is needed to get an IConnectionManager
.
Wrong Place
The AddCommandHandler
methods don't look like they should be in ServiceProviderExtensions
to me.