Skip to content

Blazor form model expression formatting#47754

Merged
MackinnonBuck merged 12 commits intomainfrom
mbuck/form-expression-formatting
Apr 20, 2023
Merged

Blazor form model expression formatting#47754
MackinnonBuck merged 12 commits intomainfrom
mbuck/form-expression-formatting

Conversation

@MackinnonBuck
Copy link
Copy Markdown
Member

@MackinnonBuck MackinnonBuck commented Apr 17, 2023

Blazor form model expression formatting

With Blazor server-side rendering, we need to have the ability to map form request entries to their corresponding properties on the related model. This PR enables Input* components to render a name attribute that makes this mapping possible.

Fixes #47780

@MackinnonBuck MackinnonBuck requested a review from javiercn April 17, 2023 23:50
@ghost ghost added the area-blazor Includes: Blazor, Razor Components label Apr 17, 2023
Comment thread src/Components/Web/src/Forms/ExpressionFormatting/ExpressionFormatter.cs Outdated
Comment thread src/Components/Web/src/Forms/InputBase.cs Outdated
Comment thread src/Components/Web/src/Forms/ExpressionFormatting/ReverseStringBuilder.cs Outdated
CachedResult = result,
};
}
// TODO: Top-level caching.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm thinking top-level caching might not be a great optimization right now. Comparing this to MVC for a moment, the ExpressionHelper class maps cached LambdaExpression instances to strings. That strategy won't be useful in Blazor because we'll be getting a new Expression instance on each render. It will likely be good enough to make the assumption that the ValueExpression parameter won't change throughout a form input component's lifetime. That way, we only have to generate the string once per component instance.

If we did want to somehow cache on the top level, it would require us to implement our own sort of value equality checking for Expressions. And in order to check if a Expression has an entry in that cache, it would require evaluating each sub expression, meaning we would have to explore the full expression twice per string generation (once to check the cache and once to generate the string if the expression isn't in the cache). In the vast majority of cases, all that work would be to save a single allocation (the final string), which just doesn't seem worth it IMO. The existing caching mechanisms are still valid and really help optimize the case where we've seen an identical expression before.

@MackinnonBuck MackinnonBuck marked this pull request as ready for review April 18, 2023 23:18
@MackinnonBuck MackinnonBuck requested a review from a team as a code owner April 18, 2023 23:18
Comment thread src/Components/Forms/src/EditContext.cs Outdated
@MackinnonBuck
Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 3 pipeline(s).

Comment on lines +47 to +61
if (!string.IsNullOrEmpty(Name))
{
// Prefer the explicitly-specified group name over anything else.
_context.GroupName = Name;
}
else if (!string.IsNullOrEmpty(NameAttributeValue))
{
// If the user specifies a "name" attribute, or we're using "name" as a form field identifier, use that.
_context.GroupName = NameAttributeValue;
}
else
{
// Otherwise, just use a GUID to disambiguate this group's radio inputs from any others on the page.
_context.GroupName = _defaultGroupName;
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's put a pin on this one, we don't have to deal with it in this PR, but it might be challenging if providing your own name breaks binding.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's go with this for now, and we'll revisit in preview5

Comment thread src/Components/Web/src/Forms/ExpressionFormatting/ExpressionFormatter.cs Outdated
Copy link
Copy Markdown
Member

@javiercn javiercn left a comment

Choose a reason for hiding this comment

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

Looks great! Only a minor piece of feedback

@MackinnonBuck MackinnonBuck merged commit 8fe5e6d into main Apr 20, 2023
@MackinnonBuck MackinnonBuck deleted the mbuck/form-expression-formatting branch April 20, 2023 21:50
@ghost ghost added this to the 8.0-preview4 milestone Apr 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Includes: Blazor, Razor Components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generate form field identifiers for Blazor server-side rendering

2 participants