Skip to content

Conversation

@iammukeshm
Copy link
Member

Summary

Refactored SessionService.cs to reduce cyclomatic complexity as identified in Issue #1177.

Changes

1. Extract DeviceTypeClassifier (New File)

  • Moved device type detection logic from SessionService.GetDeviceType to a dedicated static helper class
  • Uses LINQ Any() for cleaner keyword matching
  • Reduced cyclomatic complexity from 9 to 3

2. Add SessionCleanupHostedService (New File)

  • Background service that runs hourly to clean up expired sessions
  • Removes sessions that have been expired for more than 30 days (audit retention)
  • Properly handles cancellation and errors
  • Registered in IdentityModule

3. SessionService Simplification

  • Removed the inline GetDeviceType method
  • Now delegates to DeviceTypeClassifier.Classify()
  • Net reduction: ~350 lines (was 372)

Files Changed

File Change
SessionService.cs Modified - removed GetDeviceType, uses DeviceTypeClassifier
DeviceTypeClassifier.cs New - static helper for device classification
SessionCleanupHostedService.cs New - background cleanup service
IdentityModule.cs Modified - register hosted service

Build Status

✅ 0 errors, 0 new warnings

Closes #1177

jarvis added 2 commits January 26, 2026 20:38
…terfaces

- Create ICurrentUserService interface combining ICurrentUser and ICurrentUserInitializer
- Create IRequestContextService interface extending IRequestContext
- Update CurrentUserService to implement ICurrentUserService (internal sealed)
- Update RequestContextService to implement IRequestContextService (internal sealed)
- Update DI registration to register services with their new interfaces
- Maintain backward compatibility with existing ICurrentUser and IRequestContext consumers

Closes #1180
Refactored SessionService.cs to reduce cyclomatic complexity:

- Extract DeviceTypeClassifier: Moved device type detection logic to a
  dedicated static helper class, reducing CC from 9 to 3 (uses LINQ Any)
- Add SessionCleanupHostedService: Background service for automated
  session cleanup (runs hourly, removes sessions expired >30 days)
- Register hosted service in IdentityModule for automatic cleanup

Changes:
- SessionService.cs: ~350 lines (was 372), removed GetDeviceType method
- DeviceTypeClassifier.cs: New helper class (~40 lines)
- SessionCleanupHostedService.cs: New background service (~65 lines)
- IdentityModule.cs: Register SessionCleanupHostedService

Build: 0 errors, 0 new warnings

Closes #1177
@iammukeshm iammukeshm merged commit 2887f64 into develop Jan 27, 2026
8 checks passed
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.

[Refactor] Reduce SessionService.cs complexity (372 lines)

2 participants