Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 8, 2026

StatisticsService was registered as Singleton while depending on Transient-scoped repositories (IWordRepository, IUserRepository), causing the service to capture and retain the first instances indefinitely instead of getting fresh instances per scope.

Changes

  • Changed StatisticsService registration from AddSingleton to AddScoped in Backend/Startup.cs
// Before
services.AddSingleton<IStatisticsService, StatisticsService>();

// After  
services.AddScoped<IStatisticsService, StatisticsService>();

Scoped lifetime aligns with ASP.NET Core conventions for services with database dependencies and ensures proper disposal within HTTP request boundaries.

Original prompt

This section details on the original issue you should resolve

<issue_title>Fix StatisticsService Lifetime Registration</issue_title>
<issue_description>Update StatisticsService registration from Singleton to Scoped lifetime to fix
dependency injection mismatch.

In Backend/Startup.cs:

  • Change the StatisticsService registration from AddSingleton to AddScoped
  • No changes needed to StatisticsService.cs implementation</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.


This change is Reviewable

Summary by CodeRabbit

  • Chores
    • Optimized internal service lifecycle management for improved performance and resource efficiency.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Jan 8, 2026

📝 Walkthrough

Walkthrough

The dependency injection container configuration is modified to register StatisticsService with Scoped lifetime instead of Singleton. This ensures a new instance is created per request scope rather than reused globally throughout the application.

Changes

Cohort / File(s) Summary
Dependency Injection Lifetime Configuration
Backend/Startup.cs
Changed IStatisticsService registration from AddSingleton to AddScoped to align dependency injection lifetime with the service's actual usage requirements.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

A rabbit hops through code so neat, 🐰
One tiny change makes it complete!
Singleton to Scoped, a lifetime swap,
Fresh instances at every hop!
No singletons hoarding resources here,
Just cleaner scopes, fresh and clear! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: updating StatisticsService's DI lifetime from Singleton to Scoped to fix a dependency injection mismatch.
Linked Issues check ✅ Passed The PR fully addresses the linked issue #4088 by changing StatisticsService registration from AddSingleton to AddScoped in Backend/Startup.cs as required.
Out of Scope Changes check ✅ Passed The PR contains only the required change to Backend/Startup.cs with no out-of-scope modifications beyond the dependency injection lifetime update.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

📜 Recent review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d2f2aa3 and bc03594.

📒 Files selected for processing (1)
  • Backend/Startup.cs
🧰 Additional context used
🧬 Code graph analysis (1)
Backend/Startup.cs (1)
Backend/Services/StatisticsService.cs (2)
  • StatisticsService (12-370)
  • StatisticsService (20-26)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: CodeQL analysis (csharp)
  • GitHub Check: Analyze (csharp)
  • GitHub Check: docker_build
  • GitHub Check: test_build (8.0.x)
🔇 Additional comments (1)
Backend/Startup.cs (1)

277-277: Correct fix for the dependency injection lifetime mismatch.

Changing StatisticsService from Singleton to Scoped resolves the issue where a Singleton service was capturing Transient-scoped repository instances (IWordRepository, IUserRepository). With Scoped lifetime:

  • A fresh instance is created per HTTP request
  • Repository dependencies are properly instantiated and disposed within request scope
  • Aligns with ASP.NET Core conventions for services with data access dependencies

The service implementation is stateless with a lightweight constructor, so there are no performance or thread-safety concerns from this change.

Note: While reviewing the DI configuration, MergeService (line 262) appears to have the same lifetime mismatch pattern—it's registered as Singleton but depends on Transient repositories. Consider addressing that in a follow-up if not already tracked.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Co-authored-by: imnasnainaec <6411521+imnasnainaec@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix StatisticsService lifetime registration Fix StatisticsService dependency injection lifetime mismatch Jan 8, 2026
Copilot AI requested a review from imnasnainaec January 8, 2026 20:33
@imnasnainaec imnasnainaec marked this pull request as ready for review January 8, 2026 20:35
@imnasnainaec imnasnainaec requested a review from Copilot January 8, 2026 20:36
@imnasnainaec
Copy link
Collaborator

@coderabbitai review

@coderabbitai
Copy link

coderabbitai bot commented Jan 8, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a dependency injection lifetime mismatch where StatisticsService was registered as a Singleton while depending on Transient-scoped repositories. This caused the service to capture and retain the first repository instances indefinitely, leading to potential issues with stale database connections and incorrect data isolation between requests.

Key Changes:

  • Changed StatisticsService registration from AddSingleton to AddScoped in the DI container

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codecov
Copy link

codecov bot commented Jan 8, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.59%. Comparing base (d2f2aa3) to head (bc03594).

Additional details and impacted files
@@             Coverage Diff             @@
##           master    #4089       +/-   ##
===========================================
+ Coverage   74.57%   85.59%   +11.02%     
===========================================
  Files         295       54      -241     
  Lines       10938     4777     -6161     
  Branches     1372      589      -783     
===========================================
- Hits         8157     4089     -4068     
+ Misses       2385      544     -1841     
+ Partials      396      144      -252     
Flag Coverage Δ
backend 85.59% <ø> (ø)
frontend ?

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Collaborator

@imnasnainaec imnasnainaec left a comment

Choose a reason for hiding this comment

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

@imnasnainaec reviewed all commit messages and made 1 comment.
Reviewable status: 0 of 1 files reviewed, 1 unresolved discussion.


Backend/Startup.cs line 277 at r1 (raw file):

            // Statistics types
            services.AddScoped<IStatisticsService, StatisticsService>();

Updated issue: this should be AddTransient instead of AddScoped.

@imnasnainaec
Copy link
Collaborator

Replaced by #4093

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix StatisticsService Lifetime Registration

2 participants