Abstracting a service for a Blazor component
Currently, the Blazor component directly calls the Northwind database context to fetch the customers. This works fine in Blazor Server since the component executes on the server. But this component would not work when hosted in Blazor WebAssembly.
We will now create a local dependency service to enable better reuse of the components:
- In the
Northwind.BlazorServer
project, in theData
folder, add a new file namedINorthwindService.cs
. (The Visual Studio project item template is named Interface.) - Modify its contents to define a contract for a local service that abstracts CRUD operations, as shown in the following code:
namespace Packt.Shared; public interface INorthwindService { Task<List<Customer>> GetCustomersAsync(); Task<List<Customer>> GetCustomersAsync(string country); Task<Customer?> GetCustomerAsync(string id); Task<Customer> CreateCustomerAsync(Customer c);...