Best Practices for Implementing Search and Other Operations in a Blazor Server App

Yusuf 791 Reputation points
2024-07-18T05:48:37.93+00:00

Hi,

I'm a beginner working on a Blazor Server application using Blazor 8. I have used scaffolding to generate basic CRUD operations for a model, and the auto-generated code works well for these operations.

I would like to expand my application by adding more functionality, such as search and other operations. Specifically, I want to understand the best practices for where to write the code for these operations. For example, should the search logic and other queries be placed within the DbContext, a service class, or directly within the Blazor components?

Additionally, I would appreciate any advice on best practices for organizing code and structuring a Blazor application for maintainability and scalability. Are there specific patterns or practices recommended for beginners to follow?

Thank you!

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,612 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,596 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,011 questions
0 comments No comments
{count} votes

Accepted answer
  1. Tiny Wang-MSFT 2,731 Reputation points Microsoft Vendor
    2024-07-18T09:49:16.7666667+00:00

    Hi @Yusuf, thank you for using Blazor, we have official samples here, and hope it could help.

    In your scenario, you want to have a best practice for where to write the code for these operations. This is similar to what is the best practice for a structure which is hard to define without detailed requirement. For example, we create a tool class to covert string to specific format, we'd better to create a Tools folder and create a specific file which containing this service in it, and we could inject this service wherever we want to use this method. But if we just want to write a demo, or this method will only be used in a specific component, then we don't need to create so many files and folders and add so many configurations, we just need write a method in the component where we want to use it.

    The same for the search logic and other queries. We could create corresponding models, search logic and other methods in the component directly, or creating separate service files for them, it depends on the requirement. By the way, the best practice for placing the codes can be adjusted during the lifecycle of the application. One of the methods might be more and more complex in future, then we might divide the method into several methods to make it easier to maintain, and one day these methods might even be placed in several services one day. We can't predict for a very long time. But if it's just in the beginning or if we just need a simple sample, I'm afraid placing all the related methods together in a component might be the best option. We can certainly put the query methods in a separate service in advance when we already know that we would have several methods for a simple query in a near future.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.