Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

WHY Blazor component parameters should be the auto properties?

$
0
0

From the official Microsoft documentation:

Component parameters should be declared as auto-properties, meaningthat they shouldn't contain custom logic in their get or setaccessors. For example, the following StartData property is anauto-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();  }}

Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>