Skip to content
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

Merged
merged 5 commits into from
May 12, 2023
Merged

Conversation

hrrrrustic
Copy link
Contributor

Close #64147

@ghost ghost added the community-contribution Indicates that the PR has been added by a community member label May 8, 2023
@ghost
Copy link

ghost commented May 8, 2023

Tagging subscribers to this area: @dotnet/area-extensions-caching
See info in area-owners.md if you want to be subscribed.

Issue Details

Close #64147

Author: hrrrrustic
Assignees: -
Labels:

area-Extensions-Caching

Milestone: -

@@ -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);
Copy link
Member

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.

Copy link
Contributor Author

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 !

Copy link
Contributor Author

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)

Copy link
Member

@stephentoub stephentoub May 8, 2023

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.

Copy link
Contributor Author

@hrrrrustic hrrrrustic May 8, 2023

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

Copy link
Member

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.

Copy link
Contributor Author

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 😄

Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Member

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.

Co-authored-by: Eric Erhardt <eric.erhardt@microsoft.com>
Copy link
Member

@eerhardt eerhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

@adamsitnik
Copy link
Member

@eerhardt is there anything that blocks us from merging this PR?

@eerhardt
Copy link
Member

@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 <132389385+watfordkcf@users.noreply.github.com>
Copy link
Member

@adamsitnik adamsitnik left a 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!

@adamsitnik adamsitnik merged commit b05b4e4 into dotnet:main May 12, 2023
@adamsitnik adamsitnik added this to the 8.0.0 milestone May 12, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Jun 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-Extensions-Caching community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove ! from Microsoft.Extensions.Caching.Abstractions
5 participants