Skip to content

Optimize RedisCache IOC#1318

Merged
yileicn merged 1 commit intoSciSharp:masterfrom
yileicn:master
Apr 2, 2026
Merged

Optimize RedisCache IOC#1318
yileicn merged 1 commit intoSciSharp:masterfrom
yileicn:master

Conversation

@yileicn
Copy link
Copy Markdown
Member

@yileicn yileicn commented Apr 2, 2026

No description provided.

@qodo-code-review
Copy link
Copy Markdown
Contributor

Review Summary by Qodo

Register RedisCacheService as singleton in IoC container

✨ Enhancement

Grey Divider

Walkthroughs

Description
• Register RedisCacheService directly in IoC container
• Simplifies dependency resolution for Redis cache implementation
• Improves performance by pre-instantiating singleton service
Diagram
flowchart LR
  IoC["IoC Container"] -- "AddSingleton" --> RCS["RedisCacheService"]
  IoC -- "CreateInstance" --> ICS["ICacheService"]
  ICS -- "resolves to" --> RCS
Loading

Grey Divider

File Changes

1. src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs ✨ Enhancement +1/-0

Add RedisCacheService singleton registration

• Added direct singleton registration of RedisCacheService in the IoC container
• Service is now pre-registered before the ICacheService factory method
• Enables direct dependency injection of RedisCacheService without factory pattern

src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs


Grey Divider

Qodo Logo

@qodo-code-review
Copy link
Copy Markdown
Contributor

qodo-code-review bot commented Apr 2, 2026

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider


Remediation recommended

1. Duplicate cache service instance 🐞 Bug ⚙ Maintainability
Description
AddCacheServices registers RedisCacheService as a singleton but ICacheService still creates a
separate RedisCacheService via ActivatorUtilities.CreateInstance, so the container can end up with
two RedisCacheService objects when CacheType is RedisCache. This makes the new registration
ineffective and increases the risk of diverging behavior if RedisCacheService later gains internal
state or if callers inject RedisCacheService vs ICacheService.
Code

src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs[R52-55]

+        services.AddSingleton<RedisCacheService>();
        services.AddSingleton<ICacheService>(sp => cacheSettings.CacheType switch
        {
            CacheType.RedisCache => ActivatorUtilities.CreateInstance<RedisCacheService>(sp),
Evidence
The PR adds a direct singleton registration for RedisCacheService, but the ICacheService singleton
is still produced by calling ActivatorUtilities.CreateInstance<RedisCacheService>(sp) in the
factory, which creates a new instance instead of reusing the registered singleton. RedisCacheService
is a concrete ICacheService implementation, so this duplication is real when CacheType.RedisCache is
selected.

src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs[45-58]
src/Infrastructure/BotSharp.Core/Infrastructures/Cache/RedisCacheService.cs[7-14]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
`RedisCacheService` is registered as a singleton, but `ICacheService` is still created via `ActivatorUtilities.CreateInstance<RedisCacheService>(sp)`, which constructs a different instance. This defeats the intended IoC optimization and can lead to two different `RedisCacheService` objects in the container.

### Issue Context
If the intention is to allow direct injection of `RedisCacheService`, the `ICacheService` mapping should reuse that same singleton instance.

### Fix Focus Areas
- src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs[52-57]

### Suggested change
- Either remove `services.AddSingleton<RedisCacheService>();` if it’s not needed.
- Or (preferred if you want concrete injection) change the factory to return the registered singleton:
 - Register both concretes:
   - `services.AddSingleton<RedisCacheService>();`
   - `services.AddSingleton<MemoryCacheService>();`
 - Then:
   - `CacheType.RedisCache => sp.GetRequiredService<RedisCacheService>()`
   - `_ => sp.GetRequiredService<MemoryCacheService>()`
 This ensures a single instance per concrete and avoids `ActivatorUtilities.CreateInstance` for this mapping.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

ⓘ The new review experience is currently in Beta. Learn more

Grey Divider

Qodo Logo

@yileicn yileicn merged commit d192600 into SciSharp:master Apr 2, 2026
4 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.

1 participant