0

I was basically playing with WPF before and just started with Blazor. Can somebody help me to clear some basic "clean code" topics to understand how to proceed.

First initialization: I would like to initialize variables. How I should do it? From Constructor or OnInitializedAsync or OnInitialized or some other way?

I mean in constructor:

private IEnumerable<Customer> CustomerList { get; set; }

public Projects()
{
  this.CustomerList = List<Customer>();
  this.ProjectModel.ProjectStartDate = DateTime.Today;
  this.ProjectModel.ProjectEndDate = DateTime.Today;
}

OnInitializedAsync or OnInitialized:

private IEnumerable<Customer> CustomerList { get; set; }

protected async override Task OnInitializedAsync()
{
  this.CustomerList = List<Customer>();
  this.ProjectModel.ProjectStartDate = DateTime.Today;
  this.ProjectModel.ProjectEndDate = DateTime.Today;
}

Yet another way (However I personally prefer to have them all in one place):

private IEnumerable<Customer> CustomerList { get; set; } = List<Customer>();

I have noticed that constructor way does not work and only after moving these into OnInitializedAsync or OnInitialized actually works. Why so?


Second: variables.

I can define this way:

private string projectManager;
private IEnumerable<Customer> customerList;

and this:

private IEnumerable<Customer> CustomerList { get; set; }
private string ProjectManager { get; set; }

Which one is correct? What is the difference?

10101
  • 2,232
  • 3
  • 26
  • 66
  • Please also take a look at the [component lifecycle](https://learn.microsoft.com/en-us/aspnet/core/blazor/components/lifecycle?view=aspnetcore-6.0). – Grizzlly Jan 10 '22 at 15:17

1 Answers1

3

To answer your first question, I would say use the 3e option it just makes it more readable in my opinion. Unless you're fetching data from an API or repositories, then it might be better to set your variables in the OnInitializedAsync or OnAfterRenderAsync.

Regarding the constructor, as far I know this can only be used when using the code-behind approach. However, I would not suggest to use the constructor because this will stop the entire page or component from rendering. Instead, use the OnInitializedAsync, OnInitialized, OnAfterRender or OnAfterRenderAsync.This way the user will not see a empty page without body for the time the constructor takes to load data or execute logic.

OnInitialized => when it is being rendered.

OnAfterRender => when the page is full rendered.

As for your second question. See this question: C# Get Set vs Without Get Set

Properties allow you to further encapsulate logic around getting or setting a variable, for example adding simple validation logic. You could throw an exception if someone sets your value to less than zero for example. You could also add further logic in the getter/setter to for example synchronize a specific field.

A few other differences:

Properties can be used for data binding easily in most .NET UI frameworks. Reflection works differently. Differing access levels for get/set vs. for example your instance variable you can choose between readonly, private, protected, static, etc. as a whole. There is more overhead accessing a property. This is usually unimportant in most use cases other than games and highly performance sensitive situations.

Hope this makes thing a bit more clear and gives you a general direction with what terms you can find more.

Jeremie de Vos
  • 174
  • 1
  • 11