Added a data context factory to abstract the database creation process

This commit is contained in:
tsommer committed 2022-08-17 21:00:37 +02:00
1 parent 12eecf9fe0
commit d53966501a
5 files changed
+34 -2

No files matched your search

@@ -2,7 +2,7 @@
namespace DataModel.Database.Common;
public sealed class DataContext : DbContext
public sealed class DataContext : DbContext, IDataContext
{
public DbSet<Setting> Settings { get; set; }
@@ -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);
}
}
@@ -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; }
}
+3 -1
View File
@@ -1,4 +1,5 @@
using Microsoft.Maui.LifecycleEvents;
using DataModel.Database.Common;
using Microsoft.Maui.LifecycleEvents;
#if WINDOWS
using Microsoft.UI;
@@ -39,6 +40,7 @@ public static class MauiProgram
#endif
builder.Services.AddMauiBlazorWebView();
builder.Services.AddSingleton<DataContextFactory>();
#if DEBUG
builder.Services.AddBlazorWebViewDeveloperTools();
@@ -1,5 +1,7 @@
using System.Text;
using System.Text.Json.Serialization;
using DataModel.Database.Common;
using Microsoft.AspNetCore.Components;
namespace UI_MAUI.Pages;
@@ -7,6 +9,9 @@ public partial class LoadProject
{
private readonly List<RecentProject> recentProjects = new();
[Inject]
public DataContextFactory DataContextFactory { get; set; }
private string newProjectName = string.Empty;
private string newProjectDestination = string.Empty;