Remove database singleton.

This commit is contained in:
2020-09-05 02:49:41 +01:00
parent 9818cd43c6
commit 49c69b9524
8 changed files with 118 additions and 133 deletions

View File

@@ -61,6 +61,8 @@ namespace RomRepoMgr.Core.Workers
{ {
try try
{ {
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath);
SetIndeterminateProgress?.Invoke(this, System.EventArgs.Empty); SetIndeterminateProgress?.Invoke(this, System.EventArgs.Empty);
SetMessage?.Invoke(this, new MessageEventArgs SetMessage?.Invoke(this, new MessageEventArgs
@@ -117,8 +119,8 @@ namespace RomRepoMgr.Core.Workers
Category = _category Category = _category
}; };
Context.Singleton.RomSets.Add(romSet); ctx.RomSets.Add(romSet);
Context.Singleton.SaveChanges(); ctx.SaveChanges();
SetMessage?.Invoke(this, new MessageEventArgs SetMessage?.Invoke(this, new MessageEventArgs
{ {
@@ -170,7 +172,7 @@ namespace RomRepoMgr.Core.Workers
machines[name] = machine; machines[name] = machine;
Context.Singleton.Machines.Add(machine); ctx.Machines.Add(machine);
position++; position++;
} }
@@ -181,7 +183,7 @@ namespace RomRepoMgr.Core.Workers
SetIndeterminateProgress?.Invoke(this, System.EventArgs.Empty); SetIndeterminateProgress?.Invoke(this, System.EventArgs.Empty);
Context.Singleton.SaveChanges(); ctx.SaveChanges();
SetMessage?.Invoke(this, new MessageEventArgs SetMessage?.Invoke(this, new MessageEventArgs
{ {
@@ -339,8 +341,7 @@ namespace RomRepoMgr.Core.Workers
file = pendingFiles.FirstOrDefault(f => f.Crc32 == rom.CRC && f.Size == uSize); file = pendingFiles.FirstOrDefault(f => f.Crc32 == rom.CRC && f.Size == uSize);
} }
file ??= file ??= ctx.Files.FirstOrDefault(f => ((rom.SHA512 != null && f.Sha512 == rom.SHA512) ||
Context.Singleton.Files.FirstOrDefault(f => ((rom.SHA512 != null && f.Sha512 == rom.SHA512) ||
(rom.SHA384 != null && f.Sha384 == rom.SHA384) || (rom.SHA384 != null && f.Sha384 == rom.SHA384) ||
(rom.SHA256 != null && f.Sha256 == rom.SHA256) || (rom.SHA256 != null && f.Sha256 == rom.SHA256) ||
(rom.SHA1 != null && f.Sha1 == rom.SHA1) || (rom.SHA1 != null && f.Sha1 == rom.SHA1) ||
@@ -440,10 +441,10 @@ namespace RomRepoMgr.Core.Workers
SetIndeterminateProgress?.Invoke(this, System.EventArgs.Empty); SetIndeterminateProgress?.Invoke(this, System.EventArgs.Empty);
Context.Singleton.Files.AddRange(newFiles); ctx.Files.AddRange(newFiles);
Context.Singleton.FilesByMachines.AddRange(newFilesByMachine); ctx.FilesByMachines.AddRange(newFilesByMachine);
Context.Singleton.SaveChanges(); ctx.SaveChanges();
pendingFilesBySha512.Clear(); pendingFilesBySha512.Clear();
pendingFilesBySha384.Clear(); pendingFilesBySha384.Clear();
@@ -508,7 +509,7 @@ namespace RomRepoMgr.Core.Workers
dbDisk == null) dbDisk == null)
pendingDisksByMd5.TryGetValue(disk.MD5, out dbDisk); pendingDisksByMd5.TryGetValue(disk.MD5, out dbDisk);
dbDisk ??= Context.Singleton.Disks.FirstOrDefault(f => (disk.SHA1 != null && f.Sha1 == disk.SHA1) || dbDisk ??= ctx.Disks.FirstOrDefault(f => (disk.SHA1 != null && f.Sha1 == disk.SHA1) ||
(disk.MD5 != null && f.Md5 == disk.MD5)); (disk.MD5 != null && f.Md5 == disk.MD5));
if(dbDisk == null) if(dbDisk == null)
@@ -561,10 +562,10 @@ namespace RomRepoMgr.Core.Workers
SetIndeterminateProgress?.Invoke(this, System.EventArgs.Empty); SetIndeterminateProgress?.Invoke(this, System.EventArgs.Empty);
Context.Singleton.Disks.AddRange(newDisks); ctx.Disks.AddRange(newDisks);
Context.Singleton.DisksByMachines.AddRange(newDisksByMachine); ctx.DisksByMachines.AddRange(newDisksByMachine);
Context.Singleton.SaveChanges(); ctx.SaveChanges();
pendingDisksBySha1.Clear(); pendingDisksBySha1.Clear();
pendingDisksByMd5.Clear(); pendingDisksByMd5.Clear();
@@ -630,10 +631,7 @@ namespace RomRepoMgr.Core.Workers
dbMedia == null) dbMedia == null)
pendingMediasByMd5.TryGetValue(media.MD5, out dbMedia); pendingMediasByMd5.TryGetValue(media.MD5, out dbMedia);
dbMedia ??= dbMedia ??= ctx.Medias.FirstOrDefault(f => (media.SHA256 != null && f.Sha256 == media.SHA256) ||
Context.Singleton.Medias.FirstOrDefault(f =>
(media.SHA256 != null &&
f.Sha256 == media.SHA256) ||
(media.SHA1 != null && f.Sha1 == media.SHA1) || (media.SHA1 != null && f.Sha1 == media.SHA1) ||
(media.MD5 != null && f.Md5 == media.MD5)); (media.MD5 != null && f.Md5 == media.MD5));
@@ -699,10 +697,10 @@ namespace RomRepoMgr.Core.Workers
SetIndeterminateProgress?.Invoke(this, System.EventArgs.Empty); SetIndeterminateProgress?.Invoke(this, System.EventArgs.Empty);
Context.Singleton.Medias.AddRange(newMedias); ctx.Medias.AddRange(newMedias);
Context.Singleton.MediasByMachines.AddRange(newMediasByMachine); ctx.MediasByMachines.AddRange(newMediasByMachine);
Context.Singleton.SaveChanges(); ctx.SaveChanges();
pendingMediasBySha256.Clear(); pendingMediasBySha256.Clear();
pendingMediasBySha1.Clear(); pendingMediasBySha1.Clear();
@@ -712,7 +710,7 @@ namespace RomRepoMgr.Core.Workers
WorkFinished?.Invoke(this, System.EventArgs.Empty); WorkFinished?.Invoke(this, System.EventArgs.Empty);
romSet = Context.Singleton.RomSets.Find(romSet.Id); romSet = ctx.RomSets.Find(romSet.Id);
RomSetAdded?.Invoke(this, new RomSetEventArgs RomSetAdded?.Invoke(this, new RomSetEventArgs
{ {

View File

@@ -50,7 +50,9 @@ namespace RomRepoMgr.Core.Workers
Message = Localization.RetrievingRomSetFromDatabase Message = Localization.RetrievingRomSetFromDatabase
}); });
RomSet romSet = Context.Singleton.RomSets.Find(_romSetId); using var ctx = Context.Create(Settings.Settings.Current.DatabasePath);
RomSet romSet = ctx.RomSets.Find(_romSetId);
if(romSet == null) if(romSet == null)
{ {
@@ -69,7 +71,7 @@ namespace RomRepoMgr.Core.Workers
Message = Localization.ExportingRoms Message = Localization.ExportingRoms
}); });
_machines = Context.Singleton.Machines.Where(m => m.RomSet.Id == _romSetId).ToArray(); _machines = ctx.Machines.Where(m => m.RomSet.Id == _romSetId).ToArray();
SetProgressBounds?.Invoke(this, new ProgressBoundsEventArgs SetProgressBounds?.Invoke(this, new ProgressBoundsEventArgs
{ {
@@ -107,9 +109,11 @@ namespace RomRepoMgr.Core.Workers
Message = machine.Name Message = machine.Name
}); });
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath);
string machineName = machine.Name; string machineName = machine.Name;
Dictionary<string, MediaByMachine> mediasByMachine = Context.Singleton.MediasByMachines. Dictionary<string, MediaByMachine> mediasByMachine = ctx.MediasByMachines.
Where(f => f.Machine.Id == machine.Id && Where(f => f.Machine.Id == machine.Id &&
f.Media.IsInRepo). f.Media.IsInRepo).
ToDictionary(f => f.Name); ToDictionary(f => f.Name);
@@ -312,7 +316,7 @@ namespace RomRepoMgr.Core.Workers
} }
} }
Dictionary<string, DiskByMachine> disksByMachine = Context.Singleton.DisksByMachines. Dictionary<string, DiskByMachine> disksByMachine = ctx.DisksByMachines.
Where(f => f.Machine.Id == machine.Id && Where(f => f.Machine.Id == machine.Id &&
f.Disk.IsInRepo). f.Disk.IsInRepo).
ToDictionary(f => f.Name); ToDictionary(f => f.Name);
@@ -476,8 +480,7 @@ namespace RomRepoMgr.Core.Workers
} }
} }
_filesByMachine = Context.Singleton.FilesByMachines. _filesByMachine = ctx.FilesByMachines.Where(f => f.Machine.Id == machine.Id && f.File.IsInRepo).
Where(f => f.Machine.Id == machine.Id && f.File.IsInRepo).
ToDictionary(f => f.Name); ToDictionary(f => f.Name);
if(_filesByMachine.Count == 0) if(_filesByMachine.Count == 0)

View File

@@ -19,6 +19,7 @@ namespace RomRepoMgr.Core.Workers
public class FileImporter public class FileImporter
{ {
const long BUFFER_SIZE = 131072; const long BUFFER_SIZE = 131072;
readonly Context _ctx;
readonly bool _deleteAfterImport; readonly bool _deleteAfterImport;
readonly List<DbDisk> _newDisks; readonly List<DbDisk> _newDisks;
readonly List<DbFile> _newFiles; readonly List<DbFile> _newFiles;
@@ -48,6 +49,7 @@ namespace RomRepoMgr.Core.Workers
_onlyKnown = onlyKnown; _onlyKnown = onlyKnown;
_deleteAfterImport = deleteAfterImport; _deleteAfterImport = deleteAfterImport;
_position = 0; _position = 0;
_ctx = Context.Create(Settings.Settings.Current.DatabasePath);
} }
public event EventHandler SetIndeterminateProgress2; public event EventHandler SetIndeterminateProgress2;
@@ -355,7 +357,7 @@ namespace RomRepoMgr.Core.Workers
bool knownFile = _pendingFiles.TryGetValue(checksums[ChecksumType.Sha512], out DbFile dbFile); bool knownFile = _pendingFiles.TryGetValue(checksums[ChecksumType.Sha512], out DbFile dbFile);
dbFile ??= Context.Singleton.Files.FirstOrDefault(f => (f.Sha512 == checksums[ChecksumType.Sha512] || dbFile ??= _ctx.Files.FirstOrDefault(f => (f.Sha512 == checksums[ChecksumType.Sha512] ||
f.Sha384 == checksums[ChecksumType.Sha384] || f.Sha384 == checksums[ChecksumType.Sha384] ||
f.Sha256 == checksums[ChecksumType.Sha256] || f.Sha256 == checksums[ChecksumType.Sha256] ||
f.Sha1 == checksums[ChecksumType.Sha1] || f.Sha1 == checksums[ChecksumType.Sha1] ||
@@ -634,7 +636,7 @@ namespace RomRepoMgr.Core.Workers
md5 != null) md5 != null)
knownDisk = _pendingDisksByMd5.TryGetValue(md5, out dbDisk); knownDisk = _pendingDisksByMd5.TryGetValue(md5, out dbDisk);
dbDisk ??= Context.Singleton.Disks.FirstOrDefault(d => (d.Sha1 != null && d.Sha1 == sha1) || dbDisk ??= _ctx.Disks.FirstOrDefault(d => (d.Sha1 != null && d.Sha1 == sha1) ||
(d.Md5 != null && d.Md5 == sha1)); (d.Md5 != null && d.Md5 == sha1));
if(dbDisk == null) if(dbDisk == null)
@@ -937,7 +939,7 @@ namespace RomRepoMgr.Core.Workers
md5 != null) md5 != null)
knownMedia = _pendingMediasByMd5.TryGetValue(md5, out dbMedia); knownMedia = _pendingMediasByMd5.TryGetValue(md5, out dbMedia);
dbMedia ??= Context.Singleton.Medias.FirstOrDefault(d => (d.Sha256 != null && d.Sha256 == sha256) || dbMedia ??= _ctx.Medias.FirstOrDefault(d => (d.Sha256 != null && d.Sha256 == sha256) ||
(d.Sha1 != null && d.Sha1 == sha1) || (d.Sha1 != null && d.Sha1 == sha1) ||
(d.Md5 != null && d.Md5 == sha1)); (d.Md5 != null && d.Md5 == sha1));
@@ -1179,9 +1181,9 @@ namespace RomRepoMgr.Core.Workers
Message = Localization.SavingChangesToDatabase Message = Localization.SavingChangesToDatabase
}); });
Context.Singleton.Files.AddRange(_newFiles); _ctx.Files.AddRange(_newFiles);
Context.Singleton.Disks.AddRange(_newDisks); _ctx.Disks.AddRange(_newDisks);
Context.Singleton.SaveChanges(); _ctx.SaveChanges();
_newFiles.Clear(); _newFiles.Clear();
_newDisks.Clear(); _newDisks.Clear();

View File

@@ -23,37 +23,16 @@
// Copyright © 2020 Natalia Portillo // Copyright © 2020 Natalia Portillo
*******************************************************************************/ *******************************************************************************/
using System;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using RomRepoMgr.Database.Models; using RomRepoMgr.Database.Models;
using RomRepoMgr.Database.Resources;
namespace RomRepoMgr.Database namespace RomRepoMgr.Database
{ {
public sealed class Context : DbContext public sealed class Context : DbContext
{ {
static Context _singleton;
public Context(DbContextOptions options) : base(options) {} public Context(DbContextOptions options) : base(options) {}
public static Context Singleton
{
get
{
if(_singleton != null)
return _singleton;
if(Settings.Settings.Current?.DatabasePath is null)
throw new ArgumentNullException(nameof(Settings.Settings.Current.DatabasePath),
Localization.Settings_not_initialized);
_singleton = Create(Settings.Settings.Current.DatabasePath);
return _singleton;
}
}
public DbSet<DbFile> Files { get; set; } public DbSet<DbFile> Files { get; set; }
public DbSet<RomSet> RomSets { get; set; } public DbSet<RomSet> RomSets { get; set; }
public DbSet<Machine> Machines { get; set; } public DbSet<Machine> Machines { get; set; }
@@ -63,8 +42,6 @@ namespace RomRepoMgr.Database
public DbSet<DbMedia> Medias { get; set; } public DbSet<DbMedia> Medias { get; set; }
public DbSet<MediaByMachine> MediasByMachines { get; set; } public DbSet<MediaByMachine> MediasByMachines { get; set; }
public static void ReplaceSingleton(string dbPath) => _singleton = Create(dbPath);
public static Context Create(string dbPath) public static Context Create(string dbPath)
{ {
var optionsBuilder = new DbContextOptionsBuilder(); var optionsBuilder = new DbContextOptionsBuilder();

View File

@@ -203,7 +203,9 @@ namespace RomRepoMgr.ViewModels
async void ExecuteSaveCommand() async void ExecuteSaveCommand()
{ {
RomSet romSetDb = await Context.Singleton.RomSets.FindAsync(_romSet.Id); using var ctx = Context.Create(Settings.Settings.Current.DatabasePath);
RomSet romSetDb = await ctx.RomSets.FindAsync(_romSet.Id);
if(romSetDb == null) if(romSetDb == null)
return; return;
@@ -218,7 +220,7 @@ namespace RomRepoMgr.ViewModels
romSetDb.Version = Version; romSetDb.Version = Version;
romSetDb.UpdatedOn = DateTime.UtcNow; romSetDb.UpdatedOn = DateTime.UtcNow;
await Context.Singleton.SaveChangesAsync(); await ctx.SaveChangesAsync();
RomSetModified?.Invoke(this, new RomSetEventArgs RomSetModified?.Invoke(this, new RomSetEventArgs
{ {

View File

@@ -59,20 +59,22 @@ namespace RomRepoMgr.ViewModels
internal void OnOpened() => Task.Run(() => internal void OnOpened() => Task.Run(() =>
{ {
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath);
Dispatcher.UIThread.Post(() => StatusMessage = Localization.RetrievingRomSetFromDatabase); Dispatcher.UIThread.Post(() => StatusMessage = Localization.RetrievingRomSetFromDatabase);
RomSet romSet = Context.Singleton.RomSets.Find(_romSetId); RomSet romSet = ctx.RomSets.Find(_romSetId);
if(romSet == null) if(romSet == null)
return; return;
Dispatcher.UIThread.Post(() => StatusMessage = Localization.RemovingRomSetFromDatabase); Dispatcher.UIThread.Post(() => StatusMessage = Localization.RemovingRomSetFromDatabase);
Context.Singleton.RomSets.Remove(romSet); ctx.RomSets.Remove(romSet);
Dispatcher.UIThread.Post(() => StatusMessage = Localization.SavingChangesToDatabase); Dispatcher.UIThread.Post(() => StatusMessage = Localization.SavingChangesToDatabase);
Context.Singleton.SaveChanges(); ctx.SaveChanges();
Dispatcher.UIThread.Post(() => StatusMessage = Localization.RemovingDatFileFromRepo); Dispatcher.UIThread.Post(() => StatusMessage = Localization.RemovingDatFileFromRepo);

View File

@@ -321,10 +321,7 @@ namespace RomRepoMgr.ViewModels
void ExecuteSaveCommand() void ExecuteSaveCommand()
{ {
if(_databaseChanged) if(_databaseChanged)
{
Settings.Settings.Current.DatabasePath = DatabasePath; Settings.Settings.Current.DatabasePath = DatabasePath;
Context.ReplaceSingleton(DatabasePath);
}
if(_repositoryChanged) if(_repositoryChanged)
Settings.Settings.Current.RepositoryPath = RepositoryPath; Settings.Settings.Current.RepositoryPath = RepositoryPath;

View File

@@ -255,7 +255,7 @@ namespace RomRepoMgr.ViewModels
if(!Directory.Exists(dbPathFolder)) if(!Directory.Exists(dbPathFolder))
Directory.CreateDirectory(dbPathFolder); Directory.CreateDirectory(dbPathFolder);
_ = Context.Singleton; using var ctx = Context.Create(Settings.Settings.Current.DatabasePath);
Dispatcher.UIThread.Post(MigrateDatabase); Dispatcher.UIThread.Post(MigrateDatabase);
} }
@@ -283,7 +283,9 @@ namespace RomRepoMgr.ViewModels
{ {
try try
{ {
Context.Singleton.Database.Migrate(); using var ctx = Context.Create(Settings.Settings.Current.DatabasePath);
ctx.Database.Migrate();
Dispatcher.UIThread.Post(LoadRomSets); Dispatcher.UIThread.Post(LoadRomSets);
} }
@@ -311,11 +313,13 @@ namespace RomRepoMgr.ViewModels
{ {
try try
{ {
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath);
GotRomSets?.Invoke(this, new RomSetsEventArgs GotRomSets?.Invoke(this, new RomSetsEventArgs
{ {
RomSets = Context.Singleton.RomSets.OrderBy(r => r.Name).ThenBy(r => r.Version). RomSets = ctx.RomSets.OrderBy(r => r.Name).ThenBy(r => r.Version).ThenBy(r => r.Date).
ThenBy(r => r.Date).ThenBy(r => r.Description).ThenBy(r => r.Comment). ThenBy(r => r.Description).ThenBy(r => r.Comment).ThenBy(r => r.Filename).
ThenBy(r => r.Filename).Select(r => new RomSetModel Select(r => new RomSetModel
{ {
Id = r.Id, Id = r.Id,
Author = r.Author, Author = r.Author,