pakrym / jab

C# Source Generator based dependency injection container implementation.
MIT License
1.03k stars 33 forks source link

Support nullable parameters #147

Closed skarllot closed 9 months ago

skarllot commented 10 months ago

The issue or feature being addressed

Details on the issue fix or feature implementation

skarllot commented 10 months ago

Added diagnostic test

skarllot commented 10 months ago

Do you think that JAB0013 should be raised even when dependency is found?

pakrym commented 10 months ago

Do you think that JAB0013 should be raised even when dependency is found?

I think the warning should be something like "{} parameter will never be null when constructing using a service provider. Add a default value to make the service reference optional." and fire for all nullable required parameters. It feels like there is no point in defining a nullable non-optional parameter.

Feel free to pull the warning out of this PR, I will be away from the computer for more than a week, so I'd rather get this PR in and you unblocked.

skarllot commented 9 months ago

I've created 2 diagnostics, one for non-registered nullable dependency and other for registered nullable dependency. And changed CodeWriter to always display type as not null.

pakrym commented 9 months ago

Great work! Thank you!

pakrym commented 9 months ago

Out it 0.8.7

Marv51 commented 9 months ago

Hi, could I ask about the reasoning behind "JAB0014: X parameter to construct Y will never be null when constructing using a service provider."

my class that raises the warning looks like this:

public Foo(Bar? service, Bar2 service2){
}

I use Jab for my application, but construct the service manually in test cases.

The warning is absolutely correct Jab will always provide a service for the nullable parameter but for convenience in test cases this parameter is optional. I could pretty easily rearrange the parameter to Foo(Bar2 service2, Bar? service = null) to make this warning go away, but I don't quite understand how that is an improvement?