-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove last !
from Caching.Abstractions
#85918
Conversation
Tagging subscribers to this area: @dotnet/area-extensions-caching Issue DetailsClose #64147
|
@@ -174,7 +174,8 @@ public static ICacheEntry SetOptions(this ICacheEntry entry, MemoryCacheEntryOpt | |||
|
|||
foreach (PostEvictionCallbackRegistration postEvictionCallback in options.PostEvictionCallbacks) | |||
{ | |||
entry.RegisterPostEvictionCallback(postEvictionCallback.EvictionCallback!, postEvictionCallback.State); | |||
ArgumentNullException.ThrowIfNull(postEvictionCallback.EvictionCallback); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this a beneficial change? It seems like it's adding another check-and-throw that's just duplicating what RegisterPostEvictionCallback is about to do, anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not opposite to simply remove !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is a small discussion #64147 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not opposite to simply remove !
EvictionCallback
is typed as PostEvictionDelegate?
. RegisterPostEvictionCallback
is typed to accept PostEvictionDelegate
. So we can't just remove the !
. I'm just not clear on why we need to change anything here, if all we're going to do is throw the same exception.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's fine to just pass null to the next check in RegisterPostEvictionCallback, I'll update it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My original thought was to throw a more appropriate exception here if postEvictionCallback.EvictionCallback
is null. ArgumentNullException
isn't appropriate for this situation because there wasn't a null
argument to this method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind, I wrote this together with I'm not opposite...
before your message, just get some internet lag :)
Yep, feel free to close PR and Issue, I just randomly found it and it looked easy to fix 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is the code that will get into this situation:
ICacheEntry entry = ...;
MemoryCacheEntryOptions options = new MemoryCacheEntryOptions();
options.PostEvictionCallbacks.Add(new PostEvictionCallbackRegistration());
entry.SetOptions(options);
Getting an ArgumentNullException
from the above code doesn't make much sense IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does something like throw new ArgumentException("Eviction callbacks are not allowed to be null here, but one of the PostEvictionCallbacks was", nameof(options))
looks better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would clean the message up a bit, but yes I think an ArgumentException
on options
is a better experience. Maybe something like:
"MemoryCacheEntryOptions.PostEvictionCallbacks contains a PostEvictionCallbackRegistration with a null
EvictionCallback at index {0}."
We should also add a test.
src/libraries/Microsoft.Extensions.Caching.Abstractions/src/CacheEntryExtensions.cs
Outdated
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Caching.Abstractions/src/CacheEntryExtensions.cs
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Caching.Memory/tests/MemoryCacheSetAndRemoveTests.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Eric Erhardt <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks!
src/libraries/Microsoft.Extensions.Caching.Memory/tests/MemoryCacheSetAndRemoveTests.cs
Outdated
Show resolved
Hide resolved
@eerhardt is there anything that blocks us from merging this PR? |
I don't believe so. The nit/whitespace comment above could be addressed, but it isn't a major concern. |
…CacheSetAndRemoveTests.cs Co-authored-by: Christopher Watford <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hrrrrustic thank you for your contribution!
Close #64147