Separating your API calls from the components
Back in the Dependency Injection in Blazor WebAssembly section in Chapter 1, we created the IBooksService
interface and an implementation for it called LocalBooksService
. The goal was to make the components rely on the IBooksService
interface and not the implementation, which can be changed. The code to call the web API endpoints is somewhat long and repeatable, in addition to making the components depend on an instance of HttpClient
, which makes the components hard to test and have longer code.
The goal in this section is to migrate the calls we made in the previous two sections to IBooksService
and create a new implementation for it that communicates with the web API. Finally, we will write some code to fetch some book details from the API using book IDs.
So, let’s get started:
- In the
IBooksService
interface in theServices
folder, we need to add a new method calledAddBookAsync
that takes aSubmitBook
object as a...