Skip to content

feat(registries): 扩展注册表接口功能并实现基础类#42

Merged
GeWuYou merged 2 commits into
mainfrom
feat/registry-interface-extension
Feb 20, 2026
Merged

feat(registries): 扩展注册表接口功能并实现基础类#42
GeWuYou merged 2 commits into
mainfrom
feat/registry-interface-extension

Conversation

@GeWuYou
Copy link
Copy Markdown
Owner

@GeWuYou GeWuYou commented Feb 20, 2026

  • 移除 IRegistry 接口中的 in 泛型约束
  • 添加 Unregister 方法用于移除指定键的项
  • 添加 GetAll 方法用于获取所有键值对
  • 添加 Values 方法用于获取所有注册值
  • 添加 Keys 属性用于获取所有键
  • 添加 Count 属性用于获取项的数量
  • 在 KeyValueRegistryBase 中实现新增的接口方法
  • 添加 System.Collections.ObjectModel 引用支持只读集合

Summary by Sourcery

Extend the registry abstraction with richer query and management capabilities and implement them in the base key-value registry.

New Features:

  • Add key enumeration and item count properties to the registry interface.
  • Add methods to unregister entries, retrieve all key-value pairs, and get all values as read-only collections on the registry interface and base implementation.

Enhancements:

  • Update the key-value registry base class to support the expanded registry interface using read-only dictionary and collection types.

- 移除 IRegistry 接口中的 in 泛型约束
- 添加 Unregister 方法用于移除指定键的项
- 添加 GetAll 方法用于获取所有键值对
- 添加 Values 方法用于获取所有注册值
- 添加 Keys 属性用于获取所有键
- 添加 Count 属性用于获取项的数量
- 在 KeyValueRegistryBase 中实现新增的接口方法
- 添加 System.Collections.ObjectModel 引用支持只读集合
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Feb 20, 2026

Reviewer's Guide

Extends the generic registry abstraction by removing contravariant constraints on the key type, enriching the IRegistry interface with querying and mutation capabilities, and providing a concrete base implementation of these new members in KeyValueRegistryBase using read‑only collection wrappers.

Updated class diagram for IRegistry and KeyValueRegistryBase

classDiagram
    class IRegistry_T_Tr_ {
        <<interface>>
        +IEnumerable~T~ Keys
        +int Count
        +Tr Get(T key)
        +IRegistry_T_Tr_ Registry(T key, Tr value)
        +bool Unregister(T key)
        +IReadOnlyDictionary~T,Tr~ GetAll()
        +IReadOnlyCollection~Tr~ Values()
    }

    class KeyValueRegistryBase_TKey_TValue_ {
        <<abstract>>
        -IDictionary~TKey,TValue~ Map
        +IRegistry_TKey_TValue_ Registry(TKey key, TValue value)
        +IRegistry_TKey_TValue_ Registry(IKeyValue_TKey_TValue_ mapping)
        +bool Unregister(TKey key)
        +IReadOnlyDictionary~TKey,TValue~ GetAll()
        +IReadOnlyCollection~TValue~ Values()
        +IEnumerable~TKey~ Keys
        +int Count
    }

    class IKeyValue_TKey_TValue_ {
        <<interface>>
        +TKey Key
        +TValue Value
    }

    KeyValueRegistryBase_TKey_TValue_ ..|> IRegistry_TKey_TValue_
    KeyValueRegistryBase_TKey_TValue_ ..> IKeyValue_TKey_TValue_
Loading

File-Level Changes

Change Details Files
Extend IRegistry interface with richer query and mutation surface and remove contravariant key constraint.
  • Remove the 'in' variance modifier from the IRegistry key type parameter to make it invariant.
  • Add Keys enumerable property and Count property to expose registered keys and item count.
  • Add Unregister method to remove entries by key and return success status.
  • Add GetAll method returning an IReadOnlyDictionary of all key/value pairs.
  • Add Values method returning an IReadOnlyCollection of all registered values.
GFramework.Core.Abstractions/registries/IRegistry.cs
Implement the expanded registry API in KeyValueRegistryBase using the internal map and read-only collection wrappers.
  • Add System.Collections.ObjectModel using to support read-only collection types.
  • Implement Unregister by delegating to the underlying Map.Remove(key).
  • Implement GetAll to return Map cast as IReadOnlyDictionary when possible, otherwise wrap Map in a ReadOnlyDictionary.
  • Implement Values to expose Map.Values as an IReadOnlyCollection, with a fallback ReadOnlyCollection wrapper (currently constructed with an empty collection).
  • Expose Keys property as Map.Keys and Count property as Map.Count.
GFramework.Core.Abstractions/registries/KeyValueRegistryBase.cs

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • In KeyValueRegistryBase.Values(), the fallback branch creates new ReadOnlyCollection<TValue>([]), which always returns an empty collection instead of wrapping Map.Values; you likely want to materialize from Map.Values (e.g., new ReadOnlyCollection<TValue>(Map.Values.ToList())) so the method actually reflects the registry contents.
  • GetAll() currently returns a ReadOnlyDictionary wrapper over the underlying Map, which exposes a live view rather than a snapshot; consider clarifying this behavior in the method name or returning a copy (e.g., new ReadOnlyDictionary<TKey, TValue>(new Dictionary<TKey, TValue>(Map))) if callers should not see future mutations.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `KeyValueRegistryBase.Values()`, the fallback branch creates `new ReadOnlyCollection<TValue>([])`, which always returns an empty collection instead of wrapping `Map.Values`; you likely want to materialize from `Map.Values` (e.g., `new ReadOnlyCollection<TValue>(Map.Values.ToList())`) so the method actually reflects the registry contents.
- `GetAll()` currently returns a `ReadOnlyDictionary` wrapper over the underlying `Map`, which exposes a live view rather than a snapshot; consider clarifying this behavior in the method name or returning a copy (e.g., `new ReadOnlyDictionary<TKey, TValue>(new Dictionary<TKey, TValue>(Map))`) if callers should not see future mutations.

## Individual Comments

### Comment 1
<location> `GFramework.Core.Abstractions/registries/KeyValueRegistryBase.cs:99-101` </location>
<code_context>
+    ///     获取注册表中所有的值
+    /// </summary>
+    /// <returns>包含所有注册值的只读集合</returns>
+    public IReadOnlyCollection<TValue> Values()
+    {
+        return Map.Values as IReadOnlyCollection<TValue> ?? new ReadOnlyCollection<TValue>([]);
+    }
+
</code_context>

<issue_to_address>
**issue (bug_risk):** The fallback branch returns an empty collection instead of the actual registry values.

`new ReadOnlyCollection<TValue>([])` always returns an empty collection, discarding the contents of `Map`. If the cast fails, you likely want a read-only view over the actual values, e.g. `new ReadOnlyCollection<TValue>(Map.Values.ToList())`, so the result still reflects `Map`’s current values.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread GFramework.Core.Abstractions/registries/KeyValueRegistryBase.cs Outdated
- 修复了当 Map.Values 无法转换为 IReadOnlyCollection<TValue> 时返回空集合的问题
- 现在正确地将 Map.Values 转换为 List 后再创建 ReadOnlyCollection
- 确保 Values 方法始终返回包含实际数据的集合而不是空集合
@GeWuYou GeWuYou merged commit 7015ba1 into main Feb 20, 2026
5 checks passed
@GeWuYou GeWuYou deleted the feat/registry-interface-extension branch February 20, 2026 14:52
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