Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
lawrence_shaun_introducing_net_maui_build_and_deploy_crosspl.pdf
Скачиваний:
46
Добавлен:
26.06.2023
Размер:
5.15 Mб
Скачать

Chapter 9 Local Data

{

return;

}

var widgetViewModel = widgetFactory.CreateWidgetViewModel(S electedWidget);

widgetViewModel.Position = addingPosition;

Widgets.Add(widgetViewModel);

SaveWidget(widgetViewModel);

IsAddingWidget = false;

}

You can’t run your code yet because you don’t have an implementation of your IBoardRepository interface so let’s look at two different database options that will allow you to provide an implementation for your

IBoardRepository.

SQLite

SQLite is a lightweight cross-platform database that has become the go-to option for providing database support in mobile applications. The database is stored locally in a single file on the device's file system.

SQLite is supported natively by Android and iOS; however, they require access via C++. There are several C# wrappers around the native SQLite engine that .NET developers can use. The most popular choice is the C# wrapper called SQLite-net.

270

Chapter 9 Local Data

Installing SQLite-net

In order to install and use Sqlite-net, you need to install the NuGet package called Sqlite-net-pcl. You may notice the extra -pcl suffix in the NuGet package name and find this confusing. This is an artifact of an old piece

of technology used in Xamarin.Forms applications. The name has been retained but don’t worry; this is the correct package for adding to a .NET MAUI project.

You can do this by following these steps.

\1.\ Right-click the WidgetBoard project.

\2.\ Click Manage NuGet Packages.

\3.\ In the Search field, enter Sqlite-net-pcl.

\4.\ Select the Sqlite-net-pcl package and select Add Package.

\5.\ A confirmation dialog will show. Review and accept the license details if you are happy.

\6.\ Repeat the above steps for the following packages:

\a.\ SQLitePCLRaw.bundle_green

\b.\ SQLitePCLRaw.provider.dynamic_cdecl

\c.\ SQLitePCLRaw.provider.sqlite3

Using Sqlite-net

The first step is to create your IBoardRepository implementation. Add a new class file called SqliteBoardRepository in your Data folder, and make it implement your IBoardRepository interface.

using SQLite;

using WidgetBoard.Models;

271

Chapter 9 Local Data

namespace WidgetBoard.Data;

public class SqliteBoardRepository : IBoardRepository

{

public void CreateBoard(Board board)

{

throw new NotImplementedException();

}

public void CreateBoardWidget(BoardWidget boardWidget)

{

throw new NotImplementedException();

}

public void DeleteBoard(Board board)

{

throw new NotImplementedException();

}

public IReadOnlyList<Board> ListBoards()

{

throw new NotImplementedException();

}

public Board LoadBoard(int boardId)

{

throw new NotImplementedException();

}

public void UpdateBoard(Board board)

{

throw new NotImplementedException();

}

}

272

Chapter 9 Local Data

You also need to register your implementation with the app builder in MauiProgram.cs. You can add the following line

builder.Services.AddTransient<IBoardRepository,

SqliteBoardRepository>();

Connecting to an SQLite database

As mentioned, an SQLite database is contained within a single file, so when connecting to the database you need to provide the path to that file. You can do this through the SqliteConnection class. Note that if you wish to make use of async/await, you can use the SqliteAsyncConnection class.

Let’s edit your repository class to support opening a connection to your database.

Add a field for the database connection.

private readonly SQLiteConnection database;

Add a constructor to open the connection.

public SqliteBoardRepository(IFileSystem fileSystem)

{

var dbPath = Path.Combine(fileSystem.AppDataDirectory, "widgetboard_sqlite.db");

database = new SQLiteConnection(dbPath);

}

Here you make use of the IFileSystem implementation you registered in the previous section. Then you make use of it to determine where to store your database file. Finally, you open a connection using the path to your database file. Note that if the file does not exist, one will be created for you.

273