andlekbra
andlekbra

Reputation: 76

Project structure for Blazor project with Rest Api

The questions are related to a school project.

We have among others the following requirements:

  1. "The application must have a web front-end for users to interact with the application using a browser"
  2. A REST API exposing (a suitable subset of) the business logic must be provided

We have free choice of technology and I would like to use this as an oppurtunity to learn more about Blazor. What would be the recommended way of organizing a project in Blazor with both front end and API (contaiing only a subset of the business logic)?

I guess the API and front end can be implemented in the same project using Blazor server? But maybe it is better to have separate projects, and have the front-end (Blazor wasm?) use the api?

All input highly appreciated!

Upvotes: 2

Views: 2700

Answers (2)

instanceMaster
instanceMaster

Reputation: 501

I prefer using WASM project. After creating a Blazor WASM project, I typically restructure it for better organization by dividing the client project into three distinct parts:

  1. Client/UI: This is the original Blazor WASM project that contains components, pages, and other related elements.
  2. Services: This is a Class Library containing all the logic for processing server requests and responses, which includes caching, state management, and data presentation. The Services library functions as a regular Class Library.
  3. Network: Another Class Library dedicated to handling REST calls.

The Client Blazor app depends on the Services library, while the Services library relies on the Network library. This arrangement establishes a clear hierarchy among the components.

Please check the ASP.NET + Blazor starting app I maintain on GitHub: https://github.com/devInstance/DevCoreApp/tree/blazor-app. It can provide you with some ideas how to structure Server project and Data Access Layer as well.

Check my article about steps by step designing APIS: https://blog.devinstance.net/a-practical-guide-to-aspnet-applications-designing-robust-crud-webapis-part2

Additionally, you may find my latest article on using Mocks in Blazor WASM project during the UI building phase interesting: https://blog.devinstance.net/implementing-a-mock-driven-approach-in-blazor-applications

Upvotes: 1

andlekbra
andlekbra

Reputation: 76

Answering my own question in case someone else are in the same situation. I started out with Blazor WASM hosted (dotnet new blazorwasm --hosted) as suggested by iamrafael. This was a good match for my requirements. If you need authentication/authorization I would include that in the scaffolding from the beginning.

I later found and switched to the template from Blazor Hero, which seems like an excellent starting point for a clean architecture using Blazor wasm and asp.net

Upvotes: 3

Related Questions