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

Chapter 9 Local Data

private readonly ILiteCollection<BoardWidget> boardWidgetCollection;

Then you need to get access to that collection in order to allow you to perform your operations against it.

boardCollection = database.GetCollection<Board>("Boards"); boardCollection = database.GetCollection<BoardWidget>("Board Widgets");

The final part of your mapping setup is to define indexing information about your model. For this you use the EnsureIndex method.

boardCollection.EnsureIndex(b => b.Id, true);

In LiteDB, any property that you wish to be unique or want to query against needs to have a definition provided through the

EnsureIndex method.

Creating Your Tables

You don’t actually need to do anything to create your tables here. The key difference between LiteDB and other databases that you might use is that the schema of the data is held with the data.

Inserting into a LiteDB Database

You can now add in the ability to insert a board into your database by supplying the following implementation into the CreateBoard method:

public void CreateBoard(Board board)

{

boardCollection.Insert(board);

}

282

Chapter 9 Local Data

Reading a Collection from a LiteDB Database

You only need to return a list of the boards your user created in the application.

public IReadOnlyList<Board> ListBoards()

{

return boardCollection.Query()

.ToList();

}

Perhaps you should consider sorting these boards alphabetically. LiteDB offers a similar set of functionality that you looked at with Sqlite-­ net. LINQ-based expressions can be used to order your boards, which gives you the following (the addition is in bold):

public IReadOnlyList<Board> ListBoards()

{

return connection.Table<Board>()

.OrderBy(b => b.Name)

.ToList();

}

You also need to add the following line to your constructor to make sure querying is possible:

boardCollection.EnsureIndex(b => b.Name, false);

Reading a Single Entity from a LiteDB Database

When reading a Board from the database, you also need to load any BoardWidgets that relate to it. For this you can write the following:

public Board LoadBoard(int boardId)

{

283

Chapter 9 Local Data

var board = boardCollection.FindById(boardId); var boardWidgets = boardWidgetCollection.Find(w => w.BoardId == boardId).ToList();

board.BoardWidgets = boardWidgets;

return board;

}

The first line calls FindById, which allows you to find an entity with the supplied primary key value. This retrieves the Board. Next, you need to retrieve the collection of BoardWidgets. This is performed in a very similar manner to loading your collection of Boards. Finally, you assign the widgets you loaded into the board before returning it to the caller.

Deleting from a LiteDB Database

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

public void DeleteBoard(Board board)

{

boardCollection.Delete(board.Id);

}

Updating an Entity in a LiteDB Database

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

public void UpdateBoard(Board board)

{

boardCollection.Update(board);

}

284