Skip to content

Commit 5f52dc8

Browse files
authored
Database is no longer static. (#596)
1 parent 63f73bf commit 5f52dc8

File tree

5 files changed

+21
-23
lines changed

5 files changed

+21
-23
lines changed

9.0/Data/TodoSQLite/TodoSQLite/Data/TodoItemDatabase.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,56 +5,54 @@ namespace TodoSQLite.Data;
55

66
public class TodoItemDatabase
77
{
8-
SQLiteAsyncConnection Database;
9-
public TodoItemDatabase()
10-
{
11-
}
8+
SQLiteAsyncConnection database;
9+
1210
async Task Init()
1311
{
14-
if (Database is not null)
12+
if (database is not null)
1513
return;
1614

17-
Database = new SQLiteAsyncConnection(Constants.DatabasePath, Constants.Flags);
18-
var result = await Database.CreateTableAsync<TodoItem>();
15+
database = new SQLiteAsyncConnection(Constants.DatabasePath, Constants.Flags);
16+
var result = await database.CreateTableAsync<TodoItem>();
1917
}
2018

2119
public async Task<List<TodoItem>> GetItemsAsync()
2220
{
2321
await Init();
24-
return await Database.Table<TodoItem>().ToListAsync();
22+
return await database.Table<TodoItem>().ToListAsync();
2523
}
2624

2725
public async Task<List<TodoItem>> GetItemsNotDoneAsync()
2826
{
2927
await Init();
30-
return await Database.Table<TodoItem>().Where(t => t.Done).ToListAsync();
28+
return await database.Table<TodoItem>().Where(t => t.Done).ToListAsync();
3129

3230
// SQL queries are also possible
33-
//return await Database.QueryAsync<TodoItem>("SELECT * FROM [TodoItem] WHERE [Done] = 0");
31+
//return await database.QueryAsync<TodoItem>("SELECT * FROM [TodoItem] WHERE [Done] = 0");
3432
}
3533

3634
public async Task<TodoItem> GetItemAsync(int id)
3735
{
3836
await Init();
39-
return await Database.Table<TodoItem>().Where(i => i.ID == id).FirstOrDefaultAsync();
37+
return await database.Table<TodoItem>().Where(i => i.ID == id).FirstOrDefaultAsync();
4038
}
4139

4240
public async Task<int> SaveItemAsync(TodoItem item)
4341
{
4442
await Init();
4543
if (item.ID != 0)
4644
{
47-
return await Database.UpdateAsync(item);
45+
return await database.UpdateAsync(item);
4846
}
4947
else
5048
{
51-
return await Database.InsertAsync(item);
49+
return await database.InsertAsync(item);
5250
}
5351
}
5452

5553
public async Task<int> DeleteItemAsync(TodoItem item)
5654
{
5755
await Init();
58-
return await Database.DeleteAsync(item);
56+
return await database.DeleteAsync(item);
5957
}
6058
}

9.0/Data/TodoSQLite/TodoSQLite/MauiProgram.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Microsoft.Extensions.DependencyInjection.Extensions;
2-
using TodoSQLite.Data;
1+
using TodoSQLite.Data;
32
using TodoSQLite.Views;
43

54
namespace TodoSQLite;
@@ -19,7 +18,6 @@ public static MauiApp CreateMauiApp()
1918

2019
builder.Services.AddSingleton<TodoListPage>();
2120
builder.Services.AddTransient<TodoItemPage>();
22-
2321
builder.Services.AddSingleton<TodoItemDatabase>();
2422

2523
return builder.Build();

9.0/Data/TodoSQLite/TodoSQLite/TodoSQLite.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
4040
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
4141
</PropertyGroup>
42+
4243
<ItemGroup>
4344
<!-- App Icon -->
4445
<MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" />
@@ -62,8 +63,7 @@
6263
</ItemGroup>
6364

6465
<ItemGroup>
65-
<PackageReference Include="sqlite-net-pcl" Version="1.8.116" />
66-
<PackageReference Include="SQLitePCLRaw.bundle_green" Version="2.1.2" />
66+
<PackageReference Include="sqlite-net-pcl" Version="1.9.172" />
6767
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
6868
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="9.0.0" />
6969
</ItemGroup>

9.0/Data/TodoSQLite/TodoSQLite/Views/TodoItemPage.xaml.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ namespace TodoSQLite.Views;
66
[QueryProperty("Item", "Item")]
77
public partial class TodoItemPage : ContentPage
88
{
9-
TodoItem item;
10-
public TodoItem Item
9+
TodoItemDatabase database;
10+
11+
public TodoItem Item
1112
{
1213
get => BindingContext as TodoItem;
1314
set => BindingContext = value;
1415
}
15-
TodoItemDatabase database;
16+
1617
public TodoItemPage(TodoItemDatabase todoItemDatabase)
1718
{
1819
InitializeComponent();

9.0/Data/TodoSQLite/TodoSQLite/Views/TodoListPage.xaml.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ namespace TodoSQLite.Views;
77
public partial class TodoListPage : ContentPage
88
{
99
TodoItemDatabase database;
10+
1011
public ObservableCollection<TodoItem> Items { get; set; } = new();
12+
1113
public TodoListPage(TodoItemDatabase todoItemDatabase)
1214
{
1315
InitializeComponent();
1416
database = todoItemDatabase;
1517
BindingContext = this;
1618
}
1719

18-
1920
protected override async void OnNavigatedTo(NavigatedToEventArgs args)
2021
{
2122
base.OnNavigatedTo(args);

0 commit comments

Comments
 (0)