Darryl Wagoner WA1GON
Darryl Wagoner WA1GON

Reputation: 1029

Where is the best place to load initial data on Blazor WASM start up

I am building a Blazor app and need to load some initial data when the app starts so that I can put the data into a CascadingPerameter to be used by other components.

I am wondering where the best place to call a service to read this data?

Upvotes: 4

Views: 2710

Answers (2)

MrC aka Shaun Curtis
MrC aka Shaun Curtis

Reputation: 30046

In general you should use Services for data. A service is simply an instance of a class accessed through Dependency Injection. Any UI component can access the same instance of that class. The type of service depends on the scope of the data. If it's application wide and fixed during the SPA session, create a scoped or singleton service (depending on whether you're running WASM or Server) and share it though Dependency Injection. If it's "page" scoped, use a Transient Service.

Think UI - Components, Data - Services.

The MS documentation on Services and DI is here

Upvotes: 1

enet
enet

Reputation: 45626

I'd suggest you to inject your service into the MainLayout component, and read the data in the OnInitialized(Async) methods. But if the service can read the data in that phase of the initialization of your app, why can't you inject the service into the components that need it, instead of cascading the data ?

Upvotes: 1

Related Questions