Nov 17 2021 11:46 PM - edited Nov 17 2021 11:48 PM
I am in process to start develop an ERP Application in Blazor server. I am new in Blazor technology. I want suggestion in DI. I have a class called Setting. This class is used for store application’s setting at the time of user login and consume it in at every component level. And this class have reference type multiple properties. I want suggestion that what is best practice to handle these properties. There is multiple ways to handle these property. please suggest me which is best way or any new way is also welcome.
Base Class
public class CompanyBase { public int ID { get; set; } public string? Name { get; set; } } public class OptionBase { public int Option1 { get; set; } public string? Option2 { get; set; } }
Way No 1
public class Setting { public CompanyBase User { get; } = new (); public OptionBase Option { get; } = new(); }
Program.cs
builder.Services.AddTransient<Setting>();
FetchData.razor
@inject Setting setting
Way No 2
public class Setting { public CompanyBase User { get; init; } public OptionBase Option { get; init; } public Setting(CompanyBase user, OptionBase option) { User = user; Option = option; } }
Program.cs
builder.Services.AddTransient<CompanyBase>(); builder.Services.AddTransient<OptionBase>(); builder.Services.AddTransient<Setting>();
FetchData.razor
@inject Setting setting
Way No 3
No Setting Class
Program.cs
builder.Services.AddTransient<CompanyBase>(); builder.Services.AddTransient<OptionBase>();
FetchData.razor
@inject CompanyBase Company @inject CompanyBase Option
Nov 18 2021 01:01 PM
Settings that are tied to login are usually handled by the login provider and returned as user claims and persisted on the client in a token. The service lifetime for a service that reads the user claims would be a singleton in Blazor WASM.
I recommend visiting the Blazor authentication and authorization documentation for ideas.