Skip to content

revert(EmitHelper): revert use cache logic#7815

Merged
ArgoZhang merged 8 commits intomainfrom
fix-dynamic
Mar 29, 2026
Merged

revert(EmitHelper): revert use cache logic#7815
ArgoZhang merged 8 commits intomainfrom
fix-dynamic

Conversation

@ArgoZhang
Copy link
Copy Markdown
Member

@ArgoZhang ArgoZhang commented Mar 29, 2026

Link issues

fixes #7814

Summary By Copilot

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • Merge the latest code from the main branch

Summary by Sourcery

Simplify dynamic type generation for data table contexts by reverting custom cache manager usage and centralizing caching within the emit helper.

Enhancements:

  • Update DataTableDynamicContext to generate dynamic types directly via EmitHelper instead of using CacheManager-based type caching.
  • Add internal type reuse logic to EmitHelper using a shared dynamic module and type lookup by name.
  • Remove the now-unnecessary dynamic-type caching helper from CacheManager to reduce duplication and complexity.

Copilot AI review requested due to automatic review settings March 29, 2026 04:03
@bb-auto bb-auto bot added the enhancement New feature or request label Mar 29, 2026
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai bot commented Mar 29, 2026

Reviewer's Guide

Reverts the previous caching-based dynamic type creation for DataTable dynamic contexts and centralizes dynamic type reuse inside EmitHelper instead of CacheManager.

Sequence diagram for dynamic type creation in DataTableDynamicContext

sequenceDiagram
    participant DataTableDynamicContext
    participant EmitHelper
    participant ModuleBuilder

    DataTableDynamicContext->>DataTableDynamicContext: InternalGetColumns()
    DataTableDynamicContext->>EmitHelper: CreateTypeByName(typeName, cols, parent, creatingCallback)
    EmitHelper->>EmitHelper: _moduleBuilderLazy.Value
    EmitHelper->>ModuleBuilder: GetType(typeName)
    alt type_exists
        ModuleBuilder-->>EmitHelper: existing Type
        EmitHelper-->>DataTableDynamicContext: existing Type
    else type_not_exists
        ModuleBuilder-->>EmitHelper: null
        EmitHelper->>ModuleBuilder: DefineType(typeName, attributes, parent)
        alt creatingCallback_not_null
            loop for_each_col
                EmitHelper->>ModuleBuilder: CreateProperty(col, attributeBuilds)
            end
        end
        ModuleBuilder-->>EmitHelper: new Type
        EmitHelper-->>DataTableDynamicContext: new Type
    end
    DataTableDynamicContext->>DataTableDynamicContext: Columns = Utility.GetTableColumns(DynamicObjectType, cols)
    DataTableDynamicContext->>DataTableDynamicContext: OnValueChanged = OnCellValueChanged
Loading

Updated class diagram for dynamic type creation and caching

classDiagram
    class DataTableDynamicContext {
        +DataTable DataTable
        +Action~DataTableDynamicContext,ITableColumn~ AddAttributesCallback
        +Type DynamicObjectType
        +IEnumerable~ITableColumn~ Columns
        +EventHandler OnValueChanged
        +DataTableDynamicContext(DataTable table, Action~DataTableDynamicContext,ITableColumn~ addAttributesCallback, IEnumerable~string~ invisibleColumns, IEnumerable~string~ shownColumns, IEnumerable~string~ hiddenColumns)
        -IEnumerable~ITableColumn~ InternalGetColumns()
        -IEnumerable~CustomAttributeBuilder~ OnColumnCreating(ITableColumn col)
        -void OnCellValueChanged(object sender, EventArgs e)
        -Type CreateType()
    }

    class EmitHelper {
        <<static>>
        -Lazy~ModuleBuilder~ _moduleBuilderLazy
        +Type CreateTypeByName(string typeName, IEnumerable~ITableColumn~ cols, Type parent, Func~ITableColumn,IEnumerable~CustomAttributeBuilder~~ creatingCallback)
    }

    class CacheManager {
        <<static>>
        // other caching members
        -Type GetDynamicObjectTypeByName(string key, IEnumerable~ITableColumn~ cols, Func~ITableColumn,IEnumerable~CustomAttributeBuilder~~ creatingCallback, out bool cached) *removed*
    }

    class ModuleBuilder {
        +Type GetType(string className)
        +TypeBuilder DefineType(string name, TypeAttributes attr, Type parent)
    }

    class DataTableDynamicObject {
    }

    class ITableColumn {
        <<interface>>
        +string GetFieldName()
        +Type PropertyType
    }

    DataTableDynamicContext ..> ITableColumn : uses
    DataTableDynamicContext ..> DataTableDynamicObject : dynamic parent type
    DataTableDynamicContext ..> EmitHelper : calls
    EmitHelper ..> ModuleBuilder : uses
    EmitHelper ..> ITableColumn : creates properties
    CacheManager .. EmitHelper : no longer mediates dynamic type caching
Loading

File-Level Changes

Change Details Files
Rework DataTableDynamicContext dynamic type creation to stop using CacheManager and rely on EmitHelper directly.
  • Initialize OnValueChanged earlier in the constructor.
  • Remove use of CacheManager.GetDynamicObjectTypeByName and related cache key computation.
  • Restore local CreateType() to call EmitHelper.CreateTypeByName using a per-instance type name based on GetHashCode().
  • Adjust when AddAttributesCallback is invoked so it is no longer tied to cache hit logic.
src/BootstrapBlazor/Dynamic/DataTableDynamicContext.cs
Change EmitHelper to internally cache dynamic types by name using a single lazily created ModuleBuilder instead of creating a new dynamic assembly per type.
  • Add a Lazy field to hold the dynamic module.
  • Update CreateTypeByName to check the module for an existing type name and return it if found.
  • Update CreateTypeByName to define the type directly on the stored ModuleBuilder instead of via a helper.
  • Remove the now-unused CreateTypeBuilderByName helper.
src/BootstrapBlazor/Utils/EmitHelper.cs
Remove CacheManager-based API for dynamic object type caching.
  • Delete GetDynamicObjectTypeByName method that wrapped EmitHelper and used the general CacheManager instance for type caching.
  • Drop reflection-emit using directive that is no longer needed in CacheManager.
src/BootstrapBlazor/Services/CacheManager.cs

Assessment against linked issues

Issue Objective Addressed Explanation
#7814 Revert the previously introduced cache-based logic for dynamic type creation in EmitHelper/DataTableDynamicContext (i.e., stop using CacheManager-based caching and restore direct EmitHelper-based type creation behavior).

Possibly linked issues


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

@bb-auto bb-auto bot added this to the v10.4.0 milestone Mar 29, 2026
Copy link
Copy Markdown
Contributor

@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 2 issues, and left some high level feedback:

  • The constructor in DataTableDynamicContext now calls CacheManager.GetOrCreateDynamicObjectTypeByName and GetOrCreateType(), but GetOrCreateDynamicObjectTypeByName has been removed from CacheManager and GetOrCreateType does not exist, so this code will not compile and needs to be aligned with the new EmitHelper-based logic.
  • In EmitHelper.CreateTypeByName, properties are only created when creatingCallback is non-null; this means types created without a callback will have no properties at all, whereas previously properties were always created with optional attributes—consider restoring property creation regardless of callback presence.
  • The caching comment and logic around cached in DataTableDynamicContext are inconsistent (using if (!cached && ...) instead of the prior if (cached && ...) semantics); once the new cache approach is finalized, ensure the condition and comments correctly reflect when attribute callbacks should run for cached vs. newly created types.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The constructor in `DataTableDynamicContext` now calls `CacheManager.GetOrCreateDynamicObjectTypeByName` and `GetOrCreateType()`, but `GetOrCreateDynamicObjectTypeByName` has been removed from `CacheManager` and `GetOrCreateType` does not exist, so this code will not compile and needs to be aligned with the new `EmitHelper`-based logic.
- In `EmitHelper.CreateTypeByName`, properties are only created when `creatingCallback` is non-null; this means types created without a callback will have no properties at all, whereas previously properties were always created with optional attributes—consider restoring property creation regardless of callback presence.
- The caching comment and logic around `cached` in `DataTableDynamicContext` are inconsistent (using `if (!cached && ...)` instead of the prior `if (cached && ...)` semantics); once the new cache approach is finalized, ensure the condition and comments correctly reflect when attribute callbacks should run for cached vs. newly created types.

## Individual Comments

### Comment 1
<location path="src/BootstrapBlazor/Dynamic/DataTableDynamicContext.cs" line_range="104" />
<code_context>
-                }
-            }
-
+            var dynamicType = EmitHelper.CreateTypeByName($"BootstrapBlazor_{nameof(DataTableDynamicContext)}_{GetHashCode()}", cols, typeof(DataTableDynamicObject), OnColumnCreating);
             return dynamicType ?? throw new InvalidOperationException();
         }
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Using `GetHashCode()` in the dynamic type name can lead to collisions and non-deterministic naming.

Because this type is emitted into a shared dynamic module, using `GetHashCode()` for the name makes it non-deterministic across runs and vulnerable to collisions, which will cause `DefineType` to fail when two instances share the same hash. Consider deriving the name from a stable key (e.g., the existing `cacheKey` based on column names) or a GUID, or otherwise relying on the cache you’ve added rather than per-instance hash codes.

Suggested implementation:

```csharp
            // Use a deterministic name based on the cache key instead of the instance hash code.
            // This avoids collisions and non-deterministic naming across runs in the shared dynamic module.
            var typeName = $"BootstrapBlazor_{nameof(DataTableDynamicContext)}_{cacheKey}";
            var dynamicType = EmitHelper.CreateTypeByName(typeName, cols, typeof(DataTableDynamicObject), OnColumnCreating);

```

This change assumes that a `cacheKey` string is already computed in the same scope where the dynamic type is created (e.g., in `GetOrCreateType()` or its surrounding method), as suggested by your review comment. If `cacheKey` is not yet available in this scope, you will need to:
1. Ensure that the logic that builds the cache key (based on column names) is moved or exposed so it can be used here.
2. Pass `cacheKey` into this method or local function (if `CreateType` is a local function) so it is in scope for constructing `typeName`.
If the cache key can contain characters invalid in type names, you may also want to sanitize `cacheKey` (e.g., replace non-alphanumeric characters with `_`) before building `typeName`.
</issue_to_address>

### Comment 2
<location path="src/BootstrapBlazor/Utils/EmitHelper.cs" line_range="36-43" />
<code_context>
     public static Type? CreateTypeByName(string typeName, IEnumerable<ITableColumn> cols, Type? parent = null, Func<ITableColumn, IEnumerable<CustomAttributeBuilder>>? creatingCallback = null)
     {
-        var typeBuilder = CreateTypeBuilderByName(typeName, parent);
+        var moduleBuilder = _moduleBuilderLazy.Value;
+        var type = moduleBuilder.GetType(typeName);
+        if (type != null)
+        {
+            return type;
+        }

-        foreach (var col in cols)
+        var typeBuilder = moduleBuilder.DefineType(typeName, TypeAttributes.Public, parent);
+        if (creatingCallback != null)
         {
</code_context>
<issue_to_address>
**issue (bug_risk):** `CreateTypeByName` no longer creates properties when `creatingCallback` is null, which may be surprising.

Previously, properties were always created for `cols`, and a null `creatingCallback` just meant no attributes. Now the `foreach` only runs when `creatingCallback != null`, so a null callback results in no properties at all. If callers ever pass `creatingCallback == null` but still expect properties matching the columns, this is a breaking behavior change. Consider always defining the properties and only guarding the attribute application behind the callback check.
</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 src/BootstrapBlazor/Dynamic/DataTableDynamicContext.cs Outdated
Comment thread src/BootstrapBlazor/Utils/EmitHelper.cs Outdated
Copy link
Copy Markdown
Contributor

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 targets the dynamic-type emission/caching path used by DataTableDynamicContext, aiming to adjust (and partially revert) prior “use cache” logic related to emitted dynamic types.

Changes:

  • Refactors EmitHelper.CreateTypeByName to reuse a single ModuleBuilder and return an already-created type when the same typeName is requested again.
  • Removes the dynamic-type caching helper method previously located in CacheManager.
  • Modifies DataTableDynamicContext constructor to build a cache key and attempt to retrieve a cached emitted type.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.

File Description
src/BootstrapBlazor/Utils/EmitHelper.cs Switches dynamic type emission to a shared module and adds a type lookup “cache” by type name.
src/BootstrapBlazor/Services/CacheManager.cs Removes the internal dynamic-type caching helper and related Emit using.
src/BootstrapBlazor/Dynamic/DataTableDynamicContext.cs Attempts to use a cache key + CacheManager API for dynamic type reuse; adjusts callback timing.

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

Comment thread src/BootstrapBlazor/Dynamic/DataTableDynamicContext.cs Outdated
Comment thread src/BootstrapBlazor/Dynamic/DataTableDynamicContext.cs Outdated
Comment thread src/BootstrapBlazor/Utils/EmitHelper.cs Outdated
Comment thread src/BootstrapBlazor/Utils/EmitHelper.cs
Comment thread src/BootstrapBlazor/Utils/EmitHelper.cs Outdated
Comment thread src/BootstrapBlazor/Dynamic/DataTableDynamicContext.cs Outdated
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 29, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (41abea9) to head (0066185).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #7815   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          764       764           
  Lines        34128     34117   -11     
  Branches      4697      4697           
=========================================
- Hits         34128     34117   -11     
Flag Coverage Δ
BB 100.00% <100.00%> (?)

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.

@ArgoZhang ArgoZhang merged commit 548634f into main Mar 29, 2026
6 checks passed
@ArgoZhang ArgoZhang deleted the fix-dynamic branch March 29, 2026 04:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

revert(EmitHelper): revert use cache logic

2 participants