Closed stephenlautier closed 6 years ago
@stephenlautier can you create a simple example showing your use case. The reality is you are mixing to different ideas together in one case you are telling the container you specifically don't want it to throw an exception if you return null yet you want it to throw an exception when you return null.
@stephenlautier In regards to your proposal:
AllowInstanceAndFactoryToReturnNull = true
GetRequiredService not throwing an exception is an incorrect behavior and I think should be addressed.
AllowInstanceAndFactoryToReturnNull = false
I look at differently. Grace doesn't hide errors in it's "Try" methods. If an exception or a dependency is missing in an object graph an exception is thrown even in a "Try". Registering a Factory and then returning null is considered an error in Grace (you're agreeing to provide a dependency then choosing not to).
I'll look into changing the behavior for AllowInstanceAndFactoryToReturnNull = true
Yes, to be honest I wasn't 100% sure with the AllowInstanceAndFactoryToReturnNull = false
and GetService
whether it should throw or not.
The reason i concluded that maybe it should be like that because if you would use a library which might use GetService
internally and its optional, you will get different behaviors. Infect i had this issue, since we had one optional service in a specific case, so had to enable AllowInstanceAndFactoryToReturnNull
.
So basically what i was then more in favor is to be as following
GetService
= optional GetRequiredService
= mandatoryGetService
directly will be controlled via the config (not sure if it make sense tho since most probably it will call GetService
itself internallyJust a suggestion ofc, but the main one was the GetRequiredService
. Thanks!
@stephenlautier for the moment I'm going to just address the GetRequiredService
problem but at some point I'll probably add an AllowNull
method for exporting factories and turn it on for factories coming from the service collection for compatibility sake.
I've pushed a beta to nuget with a null reference check for GetRequiredService let me know how it works
@ipjohnson thats good enough for me, thanks!
@stephenlautier did this take care of it for you?
@ipjohnson yes it seems fine now. sorry for the late response
Since we switched to
Grace
, several Microsoft DI extensions seems a bit inconsistent when usingGetService
andGetRequiredService
.Given
AllowInstanceAndFactoryToReturnNull = true
GetRequiredService
is invoked should throw - currently its not throwingGetService
is invoked should NOT throw - working fineGiven
AllowInstanceAndFactoryToReturnNull = false
GetRequiredService
is invoked should throw - working fineGetService
is invoked should NOT throw - currently its throwingBasically
GetService
andGetRequiredService
should work as the name suggests -GetRequiredService
should always throw;GetService
shouldnt throw.What do you think?