Skip to content

feat: bind dictionary, ExpandoObject and DynamicObject rows by key (#361) - #507

Open
michaelstonis wants to merge 4 commits into
mainfrom
feat/361-dictionary-rows
Open

michaelstonis wants to merge 4 commits into
mainfrom
feat/361-dictionary-rows

Conversation

@michaelstonis

Copy link
Copy Markdown
Contributor

Closes #361.

Changes

Rows held as dictionaries, ExpandoObject or a DynamicObject property bag used to show blank cells. They now display, sort, filter, group, summarize and edit by key with no app code. This is Track B stage 1 of spec #107, plus the DynamicObject and FromDictionaries parts of stage 2.

Built-in row providers (KumikoUI.Core.Data)

  • KumikoDictionaryAccessorProvider (public, Instance singleton, internal constructor) handles three row shapes, checked in this order:
    • IDictionary<string, object?>, which covers Dictionary, ExpandoObject and the concurrent and sorted dictionaries.
    • IReadOnlyDictionary<string, object?>, read-only.
    • The non-generic IDictionary with string keys, such as Hashtable or a boxed Dictionary<string, int>.
  • How it reads and writes:
    • A key is matched exactly first, then by an OrdinalIgnoreCase scan. The scan is skipped when the dictionary's comparer already ignores case, and it allocates nothing for Dictionary<string, object?>.
    • A missing key reads as null, and writing it adds the key. A write to a key that differs only in case updates that key instead of adding a second one.
    • Accessors report object. Dotted paths read through nested dictionaries.
  • KumikoDynamicObjectAccessorProvider calls DynamicObject.TryGetMember/TrySetMember directly. It uses two small private GetMemberBinder/SetMemberBinder subclasses whose Fallback* methods throw. There is no CallSite, no Microsoft.CSharp and no dynamic code.
    • If the exact name misses, it matches GetDynamicMemberNames() ignoring case.
    • A refused write fails the edit with a message.
    • Like C# dynamic, a CLR property of the row type wins when reflection can see it.
  • KumikoAccessorRegistry resolves in this order: type providers (Register, [KumikoGridItem]), then AddProvider providers, then the dictionary provider, then the DynamicObject provider, then reflection.
    • Both built-in providers sit behind DictionaryRowsSwitchName ("KumikoUI.Core.IsDictionaryRowAccessSupported") and IsDictionaryRowAccessSupported ([FeatureSwitchDefinition]). The switch is on by default, which answers Spec: paging and DataTable / dictionary / dynamic row sources #107 Q1 as the spec proposes.
    • buildTransitive/KumikoUI.Core.targets maps the KumikoDictionaryRowAccess MSBuild property to that switch, the same way the reflection switch is wired.
    • Both providers keep working with reflection off.
    • A non-DynamicObject IDynamicMetaObjectProvider traces a one-time warning that names the alternatives.

Why DynamicObject support is on by default and not an opt-in: the framework marks the GetMemberBinder/SetMemberBinder constructors (and DynamicObject's own constructor) [RequiresDynamicCode], because a binder normally builds a call site. These binders never reach one. They only carry the member name into ordinary virtual calls, and the constructors only store the name. So the two binder constructors carry a narrowly justified [UnconditionalSuppressMessage("AOT", "IL3050")], and the library stays free of warnings.

  • This is proven at run time: eng/AotSmoke reads and writes a DynamicObject row in a Native AOT publish (TrimMode=full, warnings as errors), and the binary runs and passes.
  • Putting the provider behind an opt-in marked [RequiresDynamicCode] would have pushed a warning onto every AOT app, even though the path works.
  • The one thing left for the app: an AOT app that defines its own DynamicObject row type gets IL3050 from the framework's DynamicObject constructor in its own code. The README says so, and the smoke app shows how to suppress it.

KumikoColumn.ValueType (Type?)

  • This is the conversion target when the accessor reports object.
  • KumikoValuePath.TrySetValue has a new overload that takes it. For an object-typed slot, the target is ValueType, else the runtime type of the current value, else the value is stored raw. Null is always stored as-is.
  • KumikoSource.TrySetItemValue passes the column's ValueType.
  • EditSession.TargetType uses this order: declared type (if not object), then ValueType, then the original value's type, then the column-type default.
  • Nullable targets take empty text as null. Invalid text is refused with the converter's localized message.

KumikoColumnFactory.FromDictionaries(IEnumerable<object?> rows, int sampleSize = 100)

Other changes

  • Benchmarks: bench/KumikoUI.Benchmarks/DictionaryRowBenchmarks.cs (Spec: paging and DataTable / dictionary / dynamic row sources #107 section 8.9).
  • Docs: a "Row Sources" README section, a new tier in the README's Native AOT section, a CHANGELOG.md entry, and new rows in samples/README.md and bench/README.md.
  • Samples: a Dictionary Rows page on both apps (shared samples/Shared/DictionaryRowsData.cs). It has Dictionary and ExpandoObject rows, columns from FromDictionaries with ValueType and format adjustments, and a button that adds a key at run time.
  • No host members were added. ValueType lives on KumikoColumn, so both hosts' XAML can set it.

Public API added (PublicAPI.Unshipped.txt)

  • KumikoDictionaryAccessorProvider, .Instance, .GetAccessor(Type, string)
  • KumikoDynamicObjectAccessorProvider, .Instance, .GetAccessor(Type, string)
  • KumikoAccessorRegistry.DictionaryRowsSwitchName, KumikoAccessorRegistry.IsDictionaryRowAccessSupported
  • KumikoColumn.ValueType
  • KumikoColumnFactory.FromDictionaries(IEnumerable<object?>, int)

Decisions

Acceptance criteria

  • Dictionary, ExpandoObject, IReadOnlyDictionary, non-generic IDictionary and DynamicObject rows display, sort, filter, group, summarize and edit by key with no app provider. Covered by tests in DictionaryRowTests and by the AOT smoke app.
  • Provider precedence (type provider, AddProvider, dictionary, reflection) and the opt-out switch are tested (Precedence_*, DictionaryRowSwitchTests.SwitchOff_*).
  • ValueType converts edited text in object-typed cells, and invalid text gives the converter's localized message (ValueType_*, through EditSession with custom KumikoStrings).
  • KumikoColumnFactory.FromDictionaries infers keys, order and types from heterogeneous rows (Dictionary, ExpandoObject, DynamicObject, nulls and non-rows in one list).
  • Behavior tests are in tests/KumikoUI.Core.Tests/Data, including with reflection access off (DictionaryRowSwitchTests).
  • The eng/AotSmoke dictionary case (Dictionary, ExpandoObject, DynamicObject, FromDictionaries, sort, edit, filter) publishes with no IL warnings, and the AOT binary runs and passes.
  • Works on MAUI and Uno: the Core feature needs no host code. Both sample apps have a Dictionary Rows page built by FromDictionaries. Both sample apps build, but neither page was opened in a running app.
  • XML docs on every new public member. PublicAPI.Unshipped.txt is updated: the eng/update-public-api.sh Core code fix wrote nothing on this machine, so the RS0016 entries were added by hand and sorted the way the script sorts.
  • README "Row Sources" section and a CHANGELOG entry under Unreleased.
  • No allocations added to the per-frame render path. A dictionary cell read allocates nothing (benchmark). The Spec: paging and DataTable / dictionary / dynamic row sources #107 section 8.9 benchmarks were added and all targets are met (numbers below).

Verification

  • dotnet build src/KumikoUI.Core (Debug and Release): succeeded, 0 warnings.
  • dotnet build src/KumikoUI.SkiaSharp: succeeded.
  • dotnet build src/KumikoUI.Maui -f net10.0-maccatalyst: succeeded.
  • dotnet build src/KumikoUI.Uno -f net10.0-desktop: succeeded.
  • dotnet test tests/KumikoUI.Core.Tests: 4394 passed, 5 skipped, 0 failed. This includes 33 new tests in DictionaryRowTests and DictionaryRowSwitchTests.
  • dotnet test tests/KumikoUI.SkiaSharp.Tests: 42 passed.
  • dotnet publish eng/AotSmoke -c Release -r osx-arm64: no IL2xxx/IL3xxx warnings with TreatWarningsAsErrors. The binary printed AOT smoke OK. Linking needed LIBRARY_PATH pointing at Homebrew's openssl and brotli on this machine; that is a local toolchain setting, not part of this change.
  • dotnet run --project eng/DocsSnippets -c Release, then:
    • dotnet build tests/KumikoUI.Docs.Snippets.Uno -f net10.0-desktop -c Release: succeeded.
    • dotnet build tests/KumikoUI.Docs.Snippets.Maui -f net10.0-maccatalyst -c Release: succeeded.
  • dotnet build samples/SampleApp.Uno -f net10.0-desktop: succeeded.
  • dotnet build samples/SampleApp.Maui -f net10.0-maccatalyst -p:KumikoDevFlow=false: succeeded. The default Debug build fails restore with NU1605 (the DevFlow Microsoft.Maui.Controls override). That failure was already there and is unrelated to this change.
  • dotnet run --project eng/TypeNameCheck -c Release -- src/KumikoUI.Core/bin/Release/net10.0/KumikoUI.Core.dll: no collisions.
  • dotnet run -c Release --project bench/KumikoUI.Benchmarks -- --filter '*DictionaryRowBenchmarks*' --job short (Apple M4 Max):
Benchmark Result Target (#107 section 8.9)
Read, key present 1.9x the generated read (117 us vs 62 us per 10,000 cells), 0 B allocated within 4x, 0 allocations
Read, key missing (ignore-case scan of 20 keys) about 54 ns per cell, 0 B allocated under 400 ns, 0 allocations
Sort of 100K rows by a numeric key 1.13x the POCO sort (36 ms vs 32 ms) within 3x


Assert.True(CommitText(source, 0, "", out _));

Assert.True(row.ContainsKey("Qty"));

This branch has not been deployed

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Data: bind dictionary, ExpandoObject and DynamicObject rows by key with no app code (#107 Track B)

2 participants