Caleb Weeks
Caleb Weeks
CC#
Created by Caleb Weeks on 10/28/2024 in #help
Data fetching in components?
That's true...
13 replies
CC#
Created by Caleb Weeks on 10/28/2024 in #help
Data fetching in components?
Oh, that's an interesting idea. I could have a variation of the DataContextComponent that creates a new dbContext every time OnParametersSetAsync gets called. I think I'll look more into the Cancellation token approach. In this case, I need the value from the parameter in order to do the data fetching. Thanks for the help!
13 replies
CC#
Created by Caleb Weeks on 10/28/2024 in #help
Data fetching in components?
Alright, I spent some time this morning making a minimal reproduction of the error and have determined that it's not caused by the parent and child components both using dbContext. It has more to do with OnParametersSetAsync being called twice in rapid succession on a component with a @bind-Value directive. Here's some code that causes the error: [Parent.razor]
@page "/Parent"

<Child @bind-Value="value" />

@code {
private int value;

protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
await Task.Delay(1);
}
}
@page "/Parent"

<Child @bind-Value="value" />

@code {
private int value;

protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
await Task.Delay(1);
}
}
[Child.razor]
@using Core.Data
@inherits DataContextComponent

@code {
[Parameter] public int Value { get; set; }
[Parameter] public EventCallback<int> ValueChanged { get; set; }

protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
}

protected override async Task OnParametersSetAsync()
{
await dbContext.MaterialInstances.ToListAsync();
}
}
@using Core.Data
@inherits DataContextComponent

@code {
[Parameter] public int Value { get; set; }
[Parameter] public EventCallback<int> ValueChanged { get; set; }

protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
}

protected override async Task OnParametersSetAsync()
{
await dbContext.MaterialInstances.ToListAsync();
}
}
13 replies
CC#
Created by Caleb Weeks on 10/28/2024 in #help
Data fetching in components?
It might be important... I'm fairly new to C# and OOP in general, so I thought I needed to make it partial so that the inheriting class had access to the protected dbContext. I'll put together a minimal example tomorrow morning.
13 replies
CC#
Created by Caleb Weeks on 10/28/2024 in #help
Data fetching in components?
Here's my version (essentially the same):
public partial class DataContextComponent : ComponentBase, IDisposable
{
[Inject] public IDbContextFactory<DataContext> DbFactory { get; set; }

protected DataContext dbContext;

protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
dbContext ??= await DbFactory.CreateDbContextAsync();
}

public void Dispose() => dbContext.Dispose();
}
public partial class DataContextComponent : ComponentBase, IDisposable
{
[Inject] public IDbContextFactory<DataContext> DbFactory { get; set; }

protected DataContext dbContext;

protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
dbContext ??= await DbFactory.CreateDbContextAsync();
}

public void Dispose() => dbContext.Dispose();
}
13 replies
CC#
Created by Caleb Weeks on 10/28/2024 in #help
Data fetching in components?
Thanks for the response! Somehow I missed this message until just now. My implementation looks very similar. It's been working for the most part, but when I tried fetching data in OnParametersSetAsync in the child component, I'm getting that error. If I move the data loading to OnInitializedAsync in the child component or comment out the data fetching in the parent component, the error goes away. So it has something to do with the interaction between the two components.
13 replies
CC#
Created by Caleb Weeks on 8/28/2024 in #help
Using Blazor WebAssembly for client interactions in Blazor Server
Thanks! I'll look into that more. This repo is what gets generated when using Radzen Blazor Studio. I like how it's set up with the layout in the client side, but I just need the nav menu to be generated on the server. https://github.com/sethcalebweeks/RadzenBlazorAuto
7 replies
CC#
Created by Caleb Weeks on 8/28/2024 in #help
Using Blazor WebAssembly for client interactions in Blazor Server
Thanks! I'm familiar with the render modes. I'm looking for architectural advice on the best way of adding small client side interactions. I'd like to use Blazor WebAssembly, but it seems like nesting Server components in WebAssembly components isn't possible.
7 replies