Caleb Weeks
Data fetching in components?
I'm using Blazor Server.
I have set up a partial class called DataContextComponent that uses DBContextFactory to create an instance of dbContext for the lifetime of that component. I thought this would be enough to keep components isolated, but I'm finding that if there is a parent and child component that both inherit from DataContextComponent and fetch data using EF Core, I get an error saying "a second operation was started on this context instance before a previous operation completed." Am I doing something wrong?
I'd like to make reusable components that fetch their own data that I can use throughout the application. Is this an anti pattern?
Thanks for the advice!
13 replies
Using Blazor WebAssembly for client interactions in Blazor Server
Hello all!
I'm building an admin dashboard sort of application that heavily interacts with the server, so I'm using InteractiveServer rendering by default. For small client side interactions such as a collapsible sidebar, it seems like there are a few options:
- Use JavaScript (with or without a library such as Jquery or Alpine)
- Use HTML + CSS "hacks" such as the checkbox method that ships with the default Blazor template
- Use Blazor WebAssembly
I love the idea of doing as much as possible directly in Blazor, but I'm running into an issue with getting a collapsible sidebar to work. I want the functionality of the collapsing sidebar to live in Blazor WebAssembly so that it isn't talking to the server just to toggle a single piece of state. But the contents of the sidebar should be a Blazor Server component so I can directly access the database (through a service), and don't need to expose an API. I tried creating a Blazor component with RenderFragments, but I'm getting that issue where they can't be serialized.
So, my question is, in an environment that is primarily tightly coupled to the server, what's the best way of sprinkling in some client side interactivity? Is it possible to just use WebAssembly for this, or should I just be using JavaScript?
Thanks!
7 replies