Added a data context factory to abstract the database creation process

This commit is contained in:
Thorsten Sommer 2022-08-17 21:00:37 +02:00
parent 12eecf9fe0
commit d53966501a
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
5 changed files with 34 additions and 2 deletions

View File

@ -2,7 +2,7 @@
namespace DataModel.Database.Common; namespace DataModel.Database.Common;
public sealed class DataContext : DbContext public sealed class DataContext : DbContext, IDataContext
{ {
public DbSet<Setting> Settings { get; set; } public DbSet<Setting> Settings { get; set; }

View File

@ -0,0 +1,11 @@
namespace DataModel.Database.Common;
public sealed class DataContextFactory
{
public IDataContext DataContext { get; private set; }
public void CreateDataContext(string path2Database)
{
this.DataContext = Setup.CreateDatabaseInstance(path2Database, true);
}
}

View File

@ -0,0 +1,14 @@
using Microsoft.EntityFrameworkCore;
namespace DataModel.Database.Common;
public interface IDataContext
{
public DbSet<Setting> Settings { get; set; }
public DbSet<Section> Sections { get; set; }
public DbSet<TextElement> TextElements { get; set; }
public DbSet<Translation> Translations { get; set; }
}

View File

@ -1,4 +1,5 @@
using Microsoft.Maui.LifecycleEvents; using DataModel.Database.Common;
using Microsoft.Maui.LifecycleEvents;
#if WINDOWS #if WINDOWS
using Microsoft.UI; using Microsoft.UI;
@ -39,6 +40,7 @@ public static class MauiProgram
#endif #endif
builder.Services.AddMauiBlazorWebView(); builder.Services.AddMauiBlazorWebView();
builder.Services.AddSingleton<DataContextFactory>();
#if DEBUG #if DEBUG
builder.Services.AddBlazorWebViewDeveloperTools(); builder.Services.AddBlazorWebViewDeveloperTools();

View File

@ -1,5 +1,7 @@
using System.Text; using System.Text;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using DataModel.Database.Common;
using Microsoft.AspNetCore.Components;
namespace UI_MAUI.Pages; namespace UI_MAUI.Pages;
@ -7,6 +9,9 @@ public partial class LoadProject
{ {
private readonly List<RecentProject> recentProjects = new(); private readonly List<RecentProject> recentProjects = new();
[Inject]
public DataContextFactory DataContextFactory { get; set; }
private string newProjectName = string.Empty; private string newProjectName = string.Empty;
private string newProjectDestination = string.Empty; private string newProjectDestination = string.Empty;