WHY Blazor component parameters should be the auto properties?

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật