[dev-v5] Extend DefaultValues with option to set default value on a base class#4999
[dev-v5] Extend DefaultValues with option to set default value on a base class#4999vnbaaij wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR enhances the DefaultValues infrastructure used by Fluent UI Blazor components to support default-value configuration on base classes (with derived types able to override), and adds extra validation when registering defaults to improve robustness.
Changes:
- Added validation when registering defaults to reject read-only component properties and incompatible default values.
- Updated default-value retrieval to merge configured defaults across a component’s inheritance chain (base → derived), enabling base-class defaults with derived overrides.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/Core/Infrastructure/DefaultValuesComponentBuilder.cs | Adds validation for writable properties and basic type/null compatibility when registering defaults. |
| src/Core/Infrastructure/DefaultValues.cs | Updates retrieval logic to merge defaults across base types to support base-class default registrations. |
|
✅ All tests passed successfully Details on your Workflow / Core Tests page. |
Summary - Unit Tests Code CoverageSummary
CoverageMicrosoft.FluentUI.AspNetCore.Components - 98.9%
Microsoft.FluentUI.AspNetCore.Components.Charts - 100%
|
| throw new ArgumentException($"The property '{propertyName}' on component '{typeof(TComponent)}' is read-only and cannot be used for default values.", nameof(parameterSelector)); | ||
| } | ||
|
|
||
| ValidateValueCompatibility(propertyInfo, value, propertyName); |
There was a problem hiding this comment.
Why do we need to check the value's type? It takes time, especially when using reflection, and I don't really see the advantage: without this check, the code will crash anyway, right? Keep in mind that these methods are executed thousands of times since they're in the constructor of every component; they therefore need to be very efficie
| if (!_componentCache.TryGetValue(componentType, out var properties)) | ||
| var inheritanceChain = new List<Type>(); | ||
|
|
||
| for (var currentType = componentType; currentType is not null && currentType != typeof(object); currentType = currentType.BaseType) |
There was a problem hiding this comment.
There's going to be a performance issue here: you can't keep recalculating the BaseType parents all the time. You need to cache the type just once and then retrieve it without having to run through that loop again.
The logic must be different
This pull request expands and improves the robustness and correctness of setting and retrieving default values for component properties.
The main changes include
Of course, setting a value on a component itself still takes precedence.
DefaultValuesclass.Default value assignment improvements: