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

Chapter 9 Local Data

var board = connection.Query<Board>("SELECT B.* FROM Board B JOIN BoardWidget BW ON BW.BoardId = B.BoardId WHERE B.BoardId = ?", boardId);

Note that the above query is purely aimed at showing how joins work, it does not provide you with any particularly useful in the context of your application.

Deleting from an SQLite Database

While I haven’t focused on providing this functionality just yet, it is a very common use case.

public void DeleteBoard(Board board)

{

connection.Delete(board);

}

Updating an Entity in an SQLite Database

While I haven’t focused on providing this functionality just yet, it is a very common use case.

public void UpdateBoard(Board board)

{

connection.Update(board);

}

LiteDB

LiteDB is a simple, fast, and lightweight embedded .NET document database. LiteDB was inspired by the MongoDB database and its API is very similar to the official MongoDB .NET API.

278

Chapter 9 Local Data

Installing LiteDB

In order to install and use LiteDB, you need to install the NuGet package called LiteDB. Don’t worry; it is perfectly fine to install both the LiteDB and SQLite packages side by side into your project. In fact, that is precisely what you will do here.

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 LiteDB.

\4.\ Select the LiteDB package and select Add Package.

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

Using LiteDB

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

using LiteDB;

using WidgetBoard.Models;

namespace WidgetBoard.Data;

public class LiteDBBoardRepository : IBoardRepository

{

public void CreateBoard(Board board)

{

throw new NotImplementedException();

}

279

Chapter 9 Local Data

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();

}

}

You also need to register your implementation with the app builder in MauiProgram.cs. You can add the following line. Just make sure that you have removed or commented out the line to register the

SqliteBoardRepository implementation.

builder.Services.AddTransient<IBoardRepository,

LiteDBBoardRepository>();

280

Chapter 9 Local Data

Connecting to a LiteDB database

LiteDB stores all its data in a single file on disk, so your first task is to specify where this file exists so that you can create and open the file for users within your application. For this part, you will borrow a concept from a little further ahead in this chapter (the “File System” section).

Edit your repository class to support opening a connection to your database.

Add a field to hold the database access details.

private readonly LiteDatabase database;

Add a constructor to open the connection.

public LiteDBBoardRepository(IFileSystem fileSystem)

{

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

database = new LiteDatabase(dbPath);

}

The above should look very similar to the Sqlite way of accessing the database. Here you make use of the IFileSystem implementation you registered in the previous section. Then you make use of that 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.

Mapping Your Models

First, you need to add a field to hold a collection of boards and one for the collection of board widgets

private readonly ILiteCollection<Board> boardCollection;

281