Creating a Books Tracker App with .NET MAUI and Supabase

Written by zadok | Published 2023/04/04
Tech Story Tags: .net | csharp | cross-platform-app-development | supabase | xaml | database | books-tracker-app | .net-maui-and-supabase

TLDRIn this guide, you'll learn how to create an MVVM .NET MAUI app (Books Tracker) that connects with a Supabase database using the Model-View-ViewModel (MVVM) architecture.via the TL;DR App

Introduction

Supabase is a Backend-as-a-Service (BaaS) solution that provides a range of services like authentication, database management, and cloud storage. With Supabase, developers can easily build and scale web and mobile applications without having to manage the infrastructure themselves.

In this guide, you'll learn how to create an MVVM .NET MAUI app (Books Tracker) that connects with a Supabase database using the Model-View-ViewModel (MVVM) architecture. Here’s a quick demo of the app:

To continue this guide, you will need the following development tools installed:

  • Visual Studio 2022 with the .NET Multi-platform App UI development workload.
  • .NET 7 SDK.

Creating a Supabase account and database

  1. To get started, you need to create an account on Supabase. You can sign up by linking your GitHub account or using your email address.

  2. Once you are signed in, click New project and select New Organization in the drop-down that appears.

  1. Create a new organization by typing a preferred organization name, selecting the type of organization and clicking Create Organization.

  1. You will be prompted with a form for creating a new project. Fill up the form and click Create new project.

  1. Once the project setup process is complete, you will be directed to a Dashboard. Navigate to Table Editor and Click Create a new table.

  1. A flyout, for creating tables and columns, appears from the right. Type the table name, uncheck “Enable Row Level Security” and add table columns.

The id column and created_at column have been added to the table automatically. The id column serves as the primary key, while the created_at column generates a timestamp for each row upon insertion.  You can add a new column by clicking Add Column at the bottom left of the flyout.

Click Save to create the new table.

  1. Navigate to Settings -> API and copy your project URL and public API key to somewhere secure.

Creating a .NET MAUI app that works with Supabase

  1. Open Visual Studio 2022 and create a new .NET MAUI App project with the following details.
    • Project name: BooksTracker.
    • Solution name: BooksTrackerApp.
    • Location: Choose your preferred project location.

After opening the project, delete the default MainPage that was created.

  1. Add the following NuGet Packages:
    • CommunityToolkit.Mvvm: .NET MVVM helper library.

    • CommunityToolkit.Maui: To use the .NET MAUI Community Toolkit, follow the instructions in the ReadMe file that appears in Visual Studio after installing it.

    • supabase-csharp: Supabase's C# library. To learn more, you can check the documentation.

  2. Create the following folders in your project:
    • Models
    • ViewModels
    • Views
    • Services

Models

  1. In Models folder, add a new class file named Book.cs with properties as shown in the following code:

https://gist.github.com/ZadokJoshua/1d04c2d3f4d2d60bd7ee4d32b891b1f4?embedable=true

This class inherits the BaseModel provided by Postgrest library, which is included in the Supabase client. The table attribute is the same as the name of the table we created in Supabase. Also, every column attribute is the same as its corresponding table column name.

  1. Add a new class file named AppConfig.cs to the Models folder.  In AppConfig.cs, define a static class with two fields to store the Supabase URL and key you copied initially.

https://gist.github.com/ZadokJoshua/14e6c9d405235104da82dfd5d53daf77?embedable=true

(For demo purposes, we are hardcoding the API key and URL in our code. However, please use more secure methods such as storing them in a configuration file to avoid sharing your credentials.)

Services

  1. Add an interface named IDataService.cs to the Services folder.

https://gist.github.com/ZadokJoshua/c8e7d025cb0c41d87e364992e2edade0?embedable=true

  1. This interface will be implemented by a new class called DataService in the Services folder. The interface methods are modified to perform CRUD operations with the books table in Supabase.

https://gist.github.com/ZadokJoshua/16e73629e2476064ab111ebd409d07f3?embedable=true

ViewModels

In the ViewModels folder,

  1. Add a class file named BooksListingViewModel.cs.

https://gist.github.com/ZadokJoshua/88271bc6522950d41f8ebed87ca7e7f7?embedable=true

  1. Add a new class file named AddBookViewModel.cs.

https://gist.github.com/ZadokJoshua/4fe0abaa320c896dfa6022c8edd1998d?embedable=true

  1. The last ViewModel to be created is UpdateBookViewModel.cs.

https://gist.github.com/ZadokJoshua/ac8caa801d0bbcf956e5b06b533a3acd?embedable=true

Views

  1. Create three content pages in the Views folder with the following names and corresponding XAML markups:
    • AddBookPage
    • BooksListingPage - This is the main view of the application. It features a CollectionView for displaying books from the database and uses two items from the .NET MAUI Community Toolkit - BoolToObjectConverter and EventToCommandBehavior.
    • UpdateBookPage

https://gist.github.com/ZadokJoshua/419bef7c2fee56333d50978387b23aa1?embedable=true

To enable data binding, inject the viewmodels into the constructors of the pages in the code-behind and set their BindingContext.

https://gist.github.com/ZadokJoshua/66f68ae75332195f9870fd2448f3bb08?embedable=true

Register Routes

  1. In AppShell.xaml, define the BooksListingPage route on a ShellContent object and register other pages in AppShell.xaml.cs.

https://gist.github.com/ZadokJoshua/3a8e1d4f132b8f2a96375a8857abdd75?embedable=true

Register Services and Dependencies in MauiProgram.cs

  1. In MauiProgram.cs, configure and register services, pages, viewmodels and Supabase Client in the Dependency Injection (DI) container.

https://gist.github.com/ZadokJoshua/0da084463d7f7a21aaa079de457692d7?embedable=true

  1. Build and run the application.

Project source code

https://github.com/ZadokJoshua/supabase-maui-demo/tree/master?embedable=true

Thank you for Reading! Hope you found this guide helpful.


Written by zadok | Passionate .NET developer with a mechanical engineering background.
Published by HackerNoon on 2023/04/04