-
-
Notifications
You must be signed in to change notification settings - Fork 835
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Inconsistent behavior of different RegistrationExtensions.WithProperty overloads regarding nullable properties #1427
Comments
This is sort of an interesting thing where I'm not entirely sure what the right answer is. The reason I say that is that, generally speaking, we don't allow Autofac to "resolve null values." For example, you can't do this: builder.RegisterInstance<IService>(null); And if you have property injection enabled on something and say On the other hand, as you point out, it doesn't look like things are consistent from a parameter perspective - that we sometimes do allow null as a provided parameter and sometimes don't. And I agree, it should be consistent. My question is - in which direction should parameters be made consistent? Should we stop null everywhere (why are folks intentionally injecting a null?) or should we allow null everywhere ("I want to intentionally set this one parameter to null to force a particular constructor/property behavior")? On first glance it looks like most of the parameter mechanisms allow for null, which means making it consistently allow null for explicitly provided parameters would be the least breaking way to go, though it could be argued this is inconsistent with the nature of resolved values since you can't resolve a null. @alistairjevans what do you think? |
I fully understand your concerns. In our case, we have a class with various required properties. |
I think if the majority of the I consider |
Sounds good. I'll see if I can get a PR in to consistently allow null for this, unless someone beats me to it. |
Describe the Bug
The
WithProperty(Expression<Func<TLimit, TProperty>> propertyExpression, TProperty propertyValue)
throws whenpropertyValue
is null, regardless whetherTProperty
is nullable. Nullable analysis does not flag an issue when passingnull
.The relevant check is here:
Autofac/src/Autofac/RegistrationExtensions.cs
Lines 687 to 690 in b222d0f
All other
WithProperty
overloads acceptnull
ornull!
.Steps to Reproduce
Expected Behavior
I would expect
WithProperty(Expression<Func<TLimit, TProperty>> propertyExpression, TProperty propertyValue)
to accept anull
propertyValue
whenTProperty
is nullable, similar to the other overloads.Exception with Stack Trace
Dependency Versions
Autofac: 7.1.0
The text was updated successfully, but these errors were encountered: