Skip to content

Commit 995b646

Browse files
authored
An update to the WinUI sample. (#74)
1 parent a71f713 commit 995b646

File tree

12 files changed

+127
-216
lines changed

12 files changed

+127
-216
lines changed

samples/todoapp/Samples.TodoApp.sln

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TodoApp.WinUI3", "TodoApp.W
99
EndProject
1010
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CommunityToolkit.Datasync.Client", "..\..\src\CommunityToolkit.Datasync.Client\CommunityToolkit.Datasync.Client.csproj", "{2AC73FBE-9E76-4702-B551-B5884383CC68}"
1111
EndProject
12-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CommunityToolkit.Datasync.Server.Abstractions", "..\..\src\CommunityToolkit.Datasync.Server.Abstractions\CommunityToolkit.Datasync.Server.Abstractions.csproj", "{C99C342C-FDAC-46DC-89B2-A06CB008348E}"
13-
EndProject
1412
Global
1513
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1614
Debug|Any CPU = Debug|Any CPU
@@ -63,29 +61,12 @@ Global
6361
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Release|x64.Build.0 = Release|Any CPU
6462
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Release|x86.ActiveCfg = Release|Any CPU
6563
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Release|x86.Build.0 = Release|Any CPU
66-
{C99C342C-FDAC-46DC-89B2-A06CB008348E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
67-
{C99C342C-FDAC-46DC-89B2-A06CB008348E}.Debug|Any CPU.Build.0 = Debug|Any CPU
68-
{C99C342C-FDAC-46DC-89B2-A06CB008348E}.Debug|ARM64.ActiveCfg = Debug|Any CPU
69-
{C99C342C-FDAC-46DC-89B2-A06CB008348E}.Debug|ARM64.Build.0 = Debug|Any CPU
70-
{C99C342C-FDAC-46DC-89B2-A06CB008348E}.Debug|x64.ActiveCfg = Debug|Any CPU
71-
{C99C342C-FDAC-46DC-89B2-A06CB008348E}.Debug|x64.Build.0 = Debug|Any CPU
72-
{C99C342C-FDAC-46DC-89B2-A06CB008348E}.Debug|x86.ActiveCfg = Debug|Any CPU
73-
{C99C342C-FDAC-46DC-89B2-A06CB008348E}.Debug|x86.Build.0 = Debug|Any CPU
74-
{C99C342C-FDAC-46DC-89B2-A06CB008348E}.Release|Any CPU.ActiveCfg = Release|Any CPU
75-
{C99C342C-FDAC-46DC-89B2-A06CB008348E}.Release|Any CPU.Build.0 = Release|Any CPU
76-
{C99C342C-FDAC-46DC-89B2-A06CB008348E}.Release|ARM64.ActiveCfg = Release|Any CPU
77-
{C99C342C-FDAC-46DC-89B2-A06CB008348E}.Release|ARM64.Build.0 = Release|Any CPU
78-
{C99C342C-FDAC-46DC-89B2-A06CB008348E}.Release|x64.ActiveCfg = Release|Any CPU
79-
{C99C342C-FDAC-46DC-89B2-A06CB008348E}.Release|x64.Build.0 = Release|Any CPU
80-
{C99C342C-FDAC-46DC-89B2-A06CB008348E}.Release|x86.ActiveCfg = Release|Any CPU
81-
{C99C342C-FDAC-46DC-89B2-A06CB008348E}.Release|x86.Build.0 = Release|Any CPU
8264
EndGlobalSection
8365
GlobalSection(SolutionProperties) = preSolution
8466
HideSolutionNode = FALSE
8567
EndGlobalSection
8668
GlobalSection(NestedProjects) = preSolution
8769
{2AC73FBE-9E76-4702-B551-B5884383CC68} = {7183ECEC-9F44-48CE-BB97-AA2445170D5E}
88-
{C99C342C-FDAC-46DC-89B2-A06CB008348E} = {7183ECEC-9F44-48CE-BB97-AA2445170D5E}
8970
EndGlobalSection
9071
GlobalSection(ExtensibilityGlobals) = postSolution
9172
SolutionGuid = {91B9DE2A-8B79-4DC4-8235-216CD07F1CB2}

samples/todoapp/TodoApp.WinUI3/App.xaml.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using Microsoft.UI.Xaml.Controls;
1111
using System;
1212
using TodoApp.WinUI3.Database;
13-
using TodoApp.WinUI3.Services;
1413
using TodoApp.WinUI3.ViewModels;
1514
using TodoApp.WinUI3.Views;
1615

@@ -30,11 +29,18 @@ public App()
3029
this.dbConnection.Open();
3130

3231
IServiceCollection services = new ServiceCollection()
33-
.AddSingleton<ITodoService, LocalTodoService>()
3432
.AddTransient<TodoListViewModel>()
33+
.AddScoped<IDbInitializer, DbContextInitializer>()
3534
.AddDbContext<AppDbContext>(options => options.UseSqlite(this.dbConnection));
3635

3736
Ioc.Default.ConfigureServices(services.BuildServiceProvider());
37+
38+
// Initialize the database
39+
using (IServiceScope scope = Ioc.Default.CreateScope())
40+
{
41+
IDbInitializer initializer = scope.ServiceProvider.GetRequiredService<IDbInitializer>();
42+
initializer.Initialize();
43+
}
3844
}
3945

4046
protected override void OnLaunched(LaunchActivatedEventArgs args)

samples/todoapp/TodoApp.WinUI3/Database/AppDbContext.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,37 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using CommunityToolkit.Datasync.Client.Offline;
56
using Microsoft.EntityFrameworkCore;
7+
using System;
8+
using System.Threading;
9+
using System.Threading.Tasks;
610

711
namespace TodoApp.WinUI3.Database;
812

913
public class AppDbContext(DbContextOptions<AppDbContext> options) : DbContext(options)
1014
{
1115
public DbSet<TodoItem> TodoItems => Set<TodoItem>();
16+
17+
//protected override void OnDatasyncInitialization(DatasyncOfflineOptionsBuilder optionsBuilder)
18+
//{
19+
// _ = optionsBuilder.UseEndpoint(new Uri("https://myservice.azurewebsites.net"));
20+
//}
21+
}
22+
23+
/// <summary>
24+
/// Use this class to initialize the database. In this sample, we just create
25+
/// the database using <see cref="DatabaseFacade.EnsureCreated"/>. However, you
26+
/// may want to use migrations.
27+
/// </summary>
28+
/// <param name="context">The context for the database.</param>
29+
public class DbContextInitializer(AppDbContext context) : IDbInitializer
30+
{
31+
/// <inheritdoc />
32+
public void Initialize()
33+
=> context.Database.EnsureCreated();
34+
35+
/// <inheritdoc />
36+
public Task InitializeAsync(CancellationToken cancellationToken = default)
37+
=> context.Database.EnsureCreatedAsync(cancellationToken);
1238
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
8+
namespace TodoApp.WinUI3.Database;
9+
10+
/// <summary>
11+
/// An interface to initialize a database.
12+
/// </summary>
13+
public interface IDbInitializer
14+
{
15+
/// <summary>
16+
/// Synchronously initialize the database.
17+
/// </summary>
18+
void Initialize();
19+
20+
/// <summary>
21+
/// Asynchronously initialize the database.
22+
/// </summary>
23+
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
24+
/// <returns>A task that resolves when complete.</returns>
25+
Task InitializeAsync(CancellationToken cancellationToken = default);
26+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.ComponentModel.DataAnnotations;
7+
8+
namespace TodoApp.WinUI3.Database;
9+
10+
/// <summary>
11+
/// An abstract class for working with offline entities.
12+
/// </summary>
13+
public abstract class OfflineClientEntity
14+
{
15+
[Key]
16+
public string Id { get; set; }
17+
public DateTimeOffset? UpdatedAt { get; set; }
18+
public string Version { get; set; }
19+
public bool Deleted { get; set; }
20+
}

samples/todoapp/TodoApp.WinUI3/Database/TodoItem.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using CommunityToolkit.Datasync.Client;
65
using System.Text.Json;
76

8-
namespace TodoApp.WinUI3;
7+
namespace TodoApp.WinUI3.Database;
98

109
public class TodoItem : OfflineClientEntity
1110
{

samples/todoapp/TodoApp.WinUI3/Services/IDataService.cs

Lines changed: 0 additions & 56 deletions
This file was deleted.

samples/todoapp/TodoApp.WinUI3/Services/ITodoService.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.

samples/todoapp/TodoApp.WinUI3/Services/LocalDataService.cs

Lines changed: 0 additions & 100 deletions
This file was deleted.

samples/todoapp/TodoApp.WinUI3/Services/LocalTodoService.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)