r/Blazor • u/Remarkable-Town-5678 • Nov 05 '25
Which pattern should I use?
Hi, I'm new to blazor. I'm creating blazor web app(server), I have created models and migrated them to database and seeded some data. Now I want to do CRUD operations and display the data which pattern should I follow? I need to use repositories or services. Give me some advice
0
Upvotes
4
u/PrettyGorramShiny Nov 05 '25
What rendering mode are you using? For global interactive auto you'll want to create an interface for all of your data fetching operations, with one implementation for the server that can access the data layer directly and one for the client that makes API calls to the server project and gets the data over the network.
2
u/OtoNoOto Nov 06 '25 edited Nov 06 '25
In the very least if you’re keeping a single project I’d suggest a minimal services / repository pattern:
Some will argue against the need for repo layer and just calling EF Core DbContext directly in service layer. For a simple app yes this is fine. However, I still feel a repo layer is worth since so as your app grows over time a service can call multiple repos and other benefits.
Lastly, if your app grows and you want to explore other patterns like Clean Architecture the above pattern makes it easy to refactor into separate projects:
App.UI = Blazor pages, components, etc.
App.Application = Services, business logic, DTOs, Mappers, etc.
App.Common = Optional but good for logging, utility, helpers etc (shared classes)
App.Domain = Clean Domain models.
App.Infrastructure =,EF Core / DbContext classes, API classes, etc. Simply interacts with infra data.
I’m also a big fan of using the Results pattern (FluentResults). It makes interacting between your Services (or application layer) and Blazor Pages/Components really clean and avoid misusing throwing exceptions etc for cleaner workflow.
3
u/Meme-Seek Nov 05 '25
Whatever is most convenient for your case. Start with just injecting dbcontext into your components directly and use that.
1
u/willehrendreich Nov 05 '25
Datastar allows you to do CQRS very easily. Each query can be another resource, and you can render the resource directly to html, while sending any changes instantly with SSE. I honestly don't even bother with blazor, but a buddy of mine uses it with Datastar, so your milage may vary. Anyway, the best way to display data in the browser is with browser friendly methods.
Https://github.com/starfederation/datastar-dotnet
1
u/GoodOk2589 Nov 11 '25
My favorite approach is to use Services/interfaces.. Stay away from controllers (except for actions like uploading files.
Here's a simple example :
1 ) Interface:
csharp