From the official Microsoft documentation:
Component parameters should be declared as auto-properties, meaning
that they shouldn’t contain custom logic in their get or set
accessors. For example, the following StartData property is an
auto-property:csharp [Parameter] public DateTime StartData { get; set; }
https://learn.microsoft.com/en-us/aspnet/core/blazor/components/?view=aspnetcore-9.0
Well, this is the dogma because I don’t see the justification.
I agree that there must not be the complicated logic, but there is the important case when the simple logic must be – it is the validation.
Example
The value of theme
parameter of AdmonitionBlock
component must be either the element of StandardThemes
enumeration or the element of CustomThemes
enumeration which must be defined via defineThemes
method before use of AdmonitionBlock
component:
public partial class AdmonitionBlock : Microsoft.AspNetCore.Components.ComponentBase
{
public static string CSS_NAMESPACE = "AdmonitionBlock";
public enum StandardThemes { regular }
protected internal static Type? CustomThemes;
public static void defineThemes(Type CustomThemes)
{
YDF_ComponentsHelper.ValidateCustomTheme(CustomThemes);
AdmonitionBlock.CustomThemes = CustomThemes;
}
protected string _theme = AdmonitionBlock.StandardThemes.regular.ToString();
[Microsoft.AspNetCore.Components.Parameter]
public object theme
{
get => this._theme;
set => YDF_ComponentsHelper.
AssignThemeIfItIsValid<AdmonitionBlock.StandardThemes>(value, AdmonitionBlock.CustomThemes, ref this._theme);
}
protected internal static bool mustConsiderThemesCSS_ClassesAsCommon = YDF_ComponentsHelper.areThemesCSS_ClassesCommon;
public static void considerThemesAsCommon()
{
AdmonitionBlock.mustConsiderThemesCSS_ClassesAsCommon = true;
}
[Microsoft.AspNetCore.Components.Parameter]
public bool areThemesCSS_ClassesCommon { get; set; } =
YDF_ComponentsHelper.areThemesCSS_ClassesCommon || AdmonitionBlock.mustConsiderThemesCSS_ClassesAsCommon;
}
The validation is being executed by the ComponentsHelper
class (not only for AdmonitionBlock
component):
public abstract class ComponentsHelper
{
public static bool areThemesCSS_ClassesCommon = false;
public static void ValidateCustomTheme(Type CustomThemes)
{
if (!CustomThemes.IsEnum)
{
throw new CustomYDF_ThemeIsNotEnumerationException();
}
}
public static void AssignThemeIfItIsValid<TStandardThemes>(object value, Type? customThemes, ref string _theme)
{
if (value is TStandardThemes standardTheme)
{
_theme = $"{ standardTheme }";
return;
}
string stringifiedThemeValue = value.ToString() ?? "";
if (customThemes is not null)
{
Type customThemesType = customThemes;
if (customThemesType.IsEnum && Enum.GetNames(customThemesType).Contains(stringifiedThemeValue))
{
_theme = stringifiedThemeValue;
return;
}
}
throw new InvalidThemeParameterForYDF_ComponentException();
}
}
6
I have to politely disagree, though it’s a reasonable initial conclusion to draw when starting out in Blazor. Components are far more complex than initial impressions.
It isn’t dogma, as explained in https://github.com/dotnet/aspnetcore/issues/26230, Jon Skeet’s comment, and other sources.
It’s sound advice, because, in almost all circumstances, people don’t understand the consequences of what they’re doing. There have been many questions on here on the topic.
Parameters get applied to the component in SetParametersAsync
. If you want to apply setting logic, apply it here. It can be sync or async and it will run before any component rendering.
Here’s the basic override template:
public override Task SetParametersAsync(ParameterView parameters)
{
// First apply the incoming parameters to the component properties
parameters.SetParameterProperties(this);
// put your logic here such as detecting the change in a parameter
// from a cached local copy
// Call base to run the rest of the OnInitialized and SetParameters methods
return base.SetParametersAsync(ParameterView.Empty);
}
You can implement your own setting logic if you wish as detailed here https://learn.microsoft.com/en-us/aspnet/core/blazor/performance?view=aspnetcore-9.0#implement-setparametersasync-manually