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

Abstract out session store from SessionManager #4567

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

joshhanson314
Copy link
Member

@joshhanson314 joshhanson314 commented Mar 2, 2025

#4565 Added abstractions and a default implementation for a session store to be used with the Blazor state SessionManager. The default implementation continues to use in-memory persistence in a ConcurrentDictionary.

Added abstractions and a default implementation for a session store to be used with the Blazor state SessionManager. The default implementation continues to use in-memory persistence in a ConcurrentDictionary.
@joshhanson314 joshhanson314 changed the title #4565 Abstract out session store from SessionManager Abstract out session store from SessionManager Mar 2, 2025
@joshhanson314 joshhanson314 marked this pull request as ready for review March 2, 2025 17:37
@rockfordlhotka rockfordlhotka self-requested a review March 3, 2025 16:16
Copy link
Member

Choose a reason for hiding this comment

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

I think @StefanOssendorf would be much happier if this had async methods instead of sync methods 😁

Seriously though, if we're going to improve the code like this, I do think we should expect that people will use async stores for the data.

Choose a reason for hiding this comment

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

Yes please :)
You can always do sync with a Task/ValueTask method but not async with a void method :)

}

/// <inheritdoc />
public void DeleteSessions(SessionsFilter filter)
Copy link
Member

Choose a reason for hiding this comment

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

I don't like this method name. Perhaps it should be more explicit that this is expiring sessions?

/// </summary>
/// <param name="key"></param>
/// <param name="session"></param>
void CreateSession(string key, Session session);
Copy link
Member

Choose a reason for hiding this comment

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

Could the create/update be combined to a TryUpdate or something similar?

Choose a reason for hiding this comment

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

AddOrUpdate like the ConcurrentDictionary methods?

{
if (!Expiration.HasValue)
{
throw new ArgumentNullException("Expiration is required.");
Copy link
Member

Choose a reason for hiding this comment

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

No hard-coded use of English is allowed in the framework. This needs to be from a resource string or be ArgumentException(nameof(HasValue))

Copy link

@ossendorf-at-hoelscher ossendorf-at-hoelscher Mar 4, 2025

Choose a reason for hiding this comment

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

My suggestion to get rid of this exception would be: Don't allow the creation of an invalid object state.

  1. Make Expiration non nullable.
  2. Either add a constructor expecting the value and/or make the property required

With those changes the whole Validate() method becomes obsolete.

Copy link
Member

Choose a reason for hiding this comment

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

This is basically internal to the default implementation right?

As in, if someone were to use a different session store they'd probably use their own approach for expiring old data.

Choose a reason for hiding this comment

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

No. Currently this class is part of the public API.
ISessionStore.DeleteSession(SessionsFilter)

Copy link
Member

Choose a reason for hiding this comment

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

I really appreciate this refactoring - absolutely on the right track. While we're doing this, might as well also embrace async.

Perhaps also consider using file scoped namespaces so Simon doesn't need to come back later and clean that up?

@@ -44,9 +50,19 @@ public void UpdateSession(Session newSession)
{
ArgumentNullException.ThrowIfNull(newSession);
var key = _sessionIdManager.GetSessionId();
var existingSession = _sessions[key];
var existingSession = _sessionStore.GetSession(key)!;

Choose a reason for hiding this comment

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

Why do you suppress with !? You should check for it and handle the default case.

@ossendorf-at-hoelscher
Copy link

@rockfordlhotka This is a change for v10. It's a breaking one ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants