Closed uhfath closed 2 years ago
Isn't opts instance of DbContextOptionsBuilder
? Why don't you use the non generic UseExceptionProcessor
method?
Isn't opts instance of
DbContextOptionsBuilder
? Why don't you use the non genericUseExceptionProcessor
method?
Yes. opts
is indeed of DbContextOptionsBuilder
type. The issue is that we can't alter this setup sequence. I've just showed it as an example.
The only way in our case is through interceptors which has to be registered beforehand. But this project uses internal visibility for them and that's why I've added a PR. Perhaps someone else might come across same limitation in their projects. It doesn't seem to hurt anything.
I see. Can you also add tests? Just verify that the correct type gets resolved from the container.
Kudos, SonarCloud Quality Gate passed!
Sure. Added appropriate tests.
But also I had to InternalsVisibleTo
project setting to all the .csproj files so tests would have access to internal classes, which interceptors currently are.
Do you have any plans on merging this for the next release?
In some cases where there is no possibility to use
DbContextOptionsBuilder
this extension adds an ability to useIServiceCollection
to addExceptionProcessorInterceptor
to a list of services with a service type ofIInterceptor
. Later this could be used to add interceptors where appropriate usingIServiceProvider
.For example. Our project uses this to setup a Db context:
And there is no way to alter this behavior or subclass a
MedServiceDbContext
to change it'sOnConfiguring
. This PR adds a helper method to cases like this without hurting any backward compatibility.To use it simply call
AddExceptionProcessor
on aIServiceCollection
like usual (i.e.services.AddExceptionProcessor
somewhere inConfigureServices
or elsewhere where appropriate) and then when configuring aDbContext
use the code like above.