API proposal for Declarative Model for Persistent Component and Services State #61138
Labels
api-suggestion
Early API idea and discussion, it is NOT ready for implementation
area-blazor
Includes: Blazor, Razor Components
Milestone
Overview and Motivation
Currently, Blazor's persistent component state feature requires imperative API calls to store and retrieve state during prerendering. This proposal introduces a declarative model using attributes to simplify state persistence, allowing developers to annotate properties in components and services that should be persisted during prerendering and restored when the application becomes interactive.
Proposed API
Usage Examples
Component State Persistence
Multiple Components of the Same Type
Service State Persistence
Alternative Designs
The existing imperative API for state persistence offers more flexibility for complex scenarios, but is more verbose and error-prone. The declarative model provides a simpler approach for common scenarios while still allowing fallback to the imperative API when needed.
Risk Considerations
Serialization Limitations: By default, System.Text.Json is used for serialization which is not trimmer-safe. Developers must ensure that types being serialized are preserved through other means.
Key Computation Constraints: The key computation algorithm only considers a subset of the component hierarchy, limiting its use in deeply nested or recursive component hierarchies.
Service Persistence Scope: Only scoped services are supported for persistence, and implementations must expose the same properties for consistent serialization.
Type Compatibility: Developers must ensure that types marked for persistence can be properly serialized and deserialized by the default serializer.
The text was updated successfully, but these errors were encountered: