From 49c69b95247ec6261290f5b67da8884e29e5abcf Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Sat, 5 Sep 2020 02:49:41 +0100 Subject: [PATCH] Remove database singleton. --- RomRepoMgr.Core/Workers/DatImporter.cs | 58 ++++++------ RomRepoMgr.Core/Workers/FileExporter.cs | 29 +++--- RomRepoMgr.Core/Workers/FileImporter.cs | 32 ++++--- RomRepoMgr.Database/Context.cs | 23 ----- RomRepoMgr/ViewModels/EditDatViewModel.cs | 6 +- RomRepoMgr/ViewModels/RemoveDatViewModel.cs | 8 +- RomRepoMgr/ViewModels/SettingsViewModel.cs | 3 - .../ViewModels/SplashWindowViewModel.cs | 92 ++++++++++--------- 8 files changed, 118 insertions(+), 133 deletions(-) diff --git a/RomRepoMgr.Core/Workers/DatImporter.cs b/RomRepoMgr.Core/Workers/DatImporter.cs index 7c5b39f..e42cd63 100644 --- a/RomRepoMgr.Core/Workers/DatImporter.cs +++ b/RomRepoMgr.Core/Workers/DatImporter.cs @@ -61,6 +61,8 @@ namespace RomRepoMgr.Core.Workers { try { + using var ctx = Context.Create(Settings.Settings.Current.DatabasePath); + SetIndeterminateProgress?.Invoke(this, System.EventArgs.Empty); SetMessage?.Invoke(this, new MessageEventArgs @@ -117,8 +119,8 @@ namespace RomRepoMgr.Core.Workers Category = _category }; - Context.Singleton.RomSets.Add(romSet); - Context.Singleton.SaveChanges(); + ctx.RomSets.Add(romSet); + ctx.SaveChanges(); SetMessage?.Invoke(this, new MessageEventArgs { @@ -170,7 +172,7 @@ namespace RomRepoMgr.Core.Workers machines[name] = machine; - Context.Singleton.Machines.Add(machine); + ctx.Machines.Add(machine); position++; } @@ -181,7 +183,7 @@ namespace RomRepoMgr.Core.Workers SetIndeterminateProgress?.Invoke(this, System.EventArgs.Empty); - Context.Singleton.SaveChanges(); + ctx.SaveChanges(); SetMessage?.Invoke(this, new MessageEventArgs { @@ -339,14 +341,13 @@ namespace RomRepoMgr.Core.Workers file = pendingFiles.FirstOrDefault(f => f.Crc32 == rom.CRC && f.Size == uSize); } - file ??= - Context.Singleton.Files.FirstOrDefault(f => ((rom.SHA512 != null && f.Sha512 == rom.SHA512) || - (rom.SHA384 != null && f.Sha384 == rom.SHA384) || - (rom.SHA256 != null && f.Sha256 == rom.SHA256) || - (rom.SHA1 != null && f.Sha1 == rom.SHA1) || - (rom.MD5 != null && f.Md5 == rom.MD5) || - (rom.CRC != null && f.Crc32 == rom.CRC)) && - f.Size == uSize); + file ??= ctx.Files.FirstOrDefault(f => ((rom.SHA512 != null && f.Sha512 == rom.SHA512) || + (rom.SHA384 != null && f.Sha384 == rom.SHA384) || + (rom.SHA256 != null && f.Sha256 == rom.SHA256) || + (rom.SHA1 != null && f.Sha1 == rom.SHA1) || + (rom.MD5 != null && f.Md5 == rom.MD5) || + (rom.CRC != null && f.Crc32 == rom.CRC)) && + f.Size == uSize); if(file == null) { @@ -440,10 +441,10 @@ namespace RomRepoMgr.Core.Workers SetIndeterminateProgress?.Invoke(this, System.EventArgs.Empty); - Context.Singleton.Files.AddRange(newFiles); - Context.Singleton.FilesByMachines.AddRange(newFilesByMachine); + ctx.Files.AddRange(newFiles); + ctx.FilesByMachines.AddRange(newFilesByMachine); - Context.Singleton.SaveChanges(); + ctx.SaveChanges(); pendingFilesBySha512.Clear(); pendingFilesBySha384.Clear(); @@ -508,8 +509,8 @@ namespace RomRepoMgr.Core.Workers dbDisk == null) pendingDisksByMd5.TryGetValue(disk.MD5, out dbDisk); - dbDisk ??= Context.Singleton.Disks.FirstOrDefault(f => (disk.SHA1 != null && f.Sha1 == disk.SHA1) || - (disk.MD5 != null && f.Md5 == disk.MD5)); + dbDisk ??= ctx.Disks.FirstOrDefault(f => (disk.SHA1 != null && f.Sha1 == disk.SHA1) || + (disk.MD5 != null && f.Md5 == disk.MD5)); if(dbDisk == null) { @@ -561,10 +562,10 @@ namespace RomRepoMgr.Core.Workers SetIndeterminateProgress?.Invoke(this, System.EventArgs.Empty); - Context.Singleton.Disks.AddRange(newDisks); - Context.Singleton.DisksByMachines.AddRange(newDisksByMachine); + ctx.Disks.AddRange(newDisks); + ctx.DisksByMachines.AddRange(newDisksByMachine); - Context.Singleton.SaveChanges(); + ctx.SaveChanges(); pendingDisksBySha1.Clear(); pendingDisksByMd5.Clear(); @@ -630,12 +631,9 @@ namespace RomRepoMgr.Core.Workers dbMedia == null) pendingMediasByMd5.TryGetValue(media.MD5, out dbMedia); - dbMedia ??= - Context.Singleton.Medias.FirstOrDefault(f => - (media.SHA256 != null && - f.Sha256 == media.SHA256) || - (media.SHA1 != null && f.Sha1 == media.SHA1) || - (media.MD5 != null && f.Md5 == media.MD5)); + dbMedia ??= ctx.Medias.FirstOrDefault(f => (media.SHA256 != null && f.Sha256 == media.SHA256) || + (media.SHA1 != null && f.Sha1 == media.SHA1) || + (media.MD5 != null && f.Md5 == media.MD5)); // TODO: SpamSum if(dbMedia == null) @@ -699,10 +697,10 @@ namespace RomRepoMgr.Core.Workers SetIndeterminateProgress?.Invoke(this, System.EventArgs.Empty); - Context.Singleton.Medias.AddRange(newMedias); - Context.Singleton.MediasByMachines.AddRange(newMediasByMachine); + ctx.Medias.AddRange(newMedias); + ctx.MediasByMachines.AddRange(newMediasByMachine); - Context.Singleton.SaveChanges(); + ctx.SaveChanges(); pendingMediasBySha256.Clear(); pendingMediasBySha1.Clear(); @@ -712,7 +710,7 @@ namespace RomRepoMgr.Core.Workers WorkFinished?.Invoke(this, System.EventArgs.Empty); - romSet = Context.Singleton.RomSets.Find(romSet.Id); + romSet = ctx.RomSets.Find(romSet.Id); RomSetAdded?.Invoke(this, new RomSetEventArgs { diff --git a/RomRepoMgr.Core/Workers/FileExporter.cs b/RomRepoMgr.Core/Workers/FileExporter.cs index 566c5b7..31852fe 100644 --- a/RomRepoMgr.Core/Workers/FileExporter.cs +++ b/RomRepoMgr.Core/Workers/FileExporter.cs @@ -50,7 +50,9 @@ namespace RomRepoMgr.Core.Workers 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) { @@ -69,7 +71,7 @@ namespace RomRepoMgr.Core.Workers 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 { @@ -107,12 +109,14 @@ namespace RomRepoMgr.Core.Workers Message = machine.Name }); + using var ctx = Context.Create(Settings.Settings.Current.DatabasePath); + string machineName = machine.Name; - Dictionary mediasByMachine = Context.Singleton.MediasByMachines. - Where(f => f.Machine.Id == machine.Id && - f.Media.IsInRepo). - ToDictionary(f => f.Name); + Dictionary mediasByMachine = ctx.MediasByMachines. + Where(f => f.Machine.Id == machine.Id && + f.Media.IsInRepo). + ToDictionary(f => f.Name); if(mediasByMachine.Count > 0) { @@ -312,10 +316,10 @@ namespace RomRepoMgr.Core.Workers } } - Dictionary disksByMachine = Context.Singleton.DisksByMachines. - Where(f => f.Machine.Id == machine.Id && - f.Disk.IsInRepo). - ToDictionary(f => f.Name); + Dictionary disksByMachine = ctx.DisksByMachines. + Where(f => f.Machine.Id == machine.Id && + f.Disk.IsInRepo). + ToDictionary(f => f.Name); if(disksByMachine.Count > 0) { @@ -476,9 +480,8 @@ namespace RomRepoMgr.Core.Workers } } - _filesByMachine = Context.Singleton.FilesByMachines. - Where(f => f.Machine.Id == machine.Id && f.File.IsInRepo). - ToDictionary(f => f.Name); + _filesByMachine = ctx.FilesByMachines.Where(f => f.Machine.Id == machine.Id && f.File.IsInRepo). + ToDictionary(f => f.Name); if(_filesByMachine.Count == 0) { diff --git a/RomRepoMgr.Core/Workers/FileImporter.cs b/RomRepoMgr.Core/Workers/FileImporter.cs index d952583..db9a187 100644 --- a/RomRepoMgr.Core/Workers/FileImporter.cs +++ b/RomRepoMgr.Core/Workers/FileImporter.cs @@ -19,6 +19,7 @@ namespace RomRepoMgr.Core.Workers public class FileImporter { const long BUFFER_SIZE = 131072; + readonly Context _ctx; readonly bool _deleteAfterImport; readonly List _newDisks; readonly List _newFiles; @@ -48,6 +49,7 @@ namespace RomRepoMgr.Core.Workers _onlyKnown = onlyKnown; _deleteAfterImport = deleteAfterImport; _position = 0; + _ctx = Context.Create(Settings.Settings.Current.DatabasePath); } public event EventHandler SetIndeterminateProgress2; @@ -355,13 +357,13 @@ namespace RomRepoMgr.Core.Workers bool knownFile = _pendingFiles.TryGetValue(checksums[ChecksumType.Sha512], out DbFile dbFile); - dbFile ??= Context.Singleton.Files.FirstOrDefault(f => (f.Sha512 == checksums[ChecksumType.Sha512] || - f.Sha384 == checksums[ChecksumType.Sha384] || - f.Sha256 == checksums[ChecksumType.Sha256] || - f.Sha1 == checksums[ChecksumType.Sha1] || - f.Md5 == checksums[ChecksumType.Md5] || - f.Crc32 == checksums[ChecksumType.Crc32]) && - f.Size == uSize); + dbFile ??= _ctx.Files.FirstOrDefault(f => (f.Sha512 == checksums[ChecksumType.Sha512] || + f.Sha384 == checksums[ChecksumType.Sha384] || + f.Sha256 == checksums[ChecksumType.Sha256] || + f.Sha1 == checksums[ChecksumType.Sha1] || + f.Md5 == checksums[ChecksumType.Md5] || + f.Crc32 == checksums[ChecksumType.Crc32]) && + f.Size == uSize); if(dbFile == null) { @@ -634,8 +636,8 @@ namespace RomRepoMgr.Core.Workers md5 != null) knownDisk = _pendingDisksByMd5.TryGetValue(md5, out dbDisk); - dbDisk ??= Context.Singleton.Disks.FirstOrDefault(d => (d.Sha1 != null && d.Sha1 == sha1) || - (d.Md5 != null && d.Md5 == sha1)); + dbDisk ??= _ctx.Disks.FirstOrDefault(d => (d.Sha1 != null && d.Sha1 == sha1) || + (d.Md5 != null && d.Md5 == sha1)); if(dbDisk == null) { @@ -937,9 +939,9 @@ namespace RomRepoMgr.Core.Workers md5 != null) knownMedia = _pendingMediasByMd5.TryGetValue(md5, out dbMedia); - dbMedia ??= Context.Singleton.Medias.FirstOrDefault(d => (d.Sha256 != null && d.Sha256 == sha256) || - (d.Sha1 != null && d.Sha1 == sha1) || - (d.Md5 != null && d.Md5 == sha1)); + dbMedia ??= _ctx.Medias.FirstOrDefault(d => (d.Sha256 != null && d.Sha256 == sha256) || + (d.Sha1 != null && d.Sha1 == sha1) || + (d.Md5 != null && d.Md5 == sha1)); if(dbMedia == null) { @@ -1179,9 +1181,9 @@ namespace RomRepoMgr.Core.Workers Message = Localization.SavingChangesToDatabase }); - Context.Singleton.Files.AddRange(_newFiles); - Context.Singleton.Disks.AddRange(_newDisks); - Context.Singleton.SaveChanges(); + _ctx.Files.AddRange(_newFiles); + _ctx.Disks.AddRange(_newDisks); + _ctx.SaveChanges(); _newFiles.Clear(); _newDisks.Clear(); diff --git a/RomRepoMgr.Database/Context.cs b/RomRepoMgr.Database/Context.cs index 345f7d2..f1b5c3b 100644 --- a/RomRepoMgr.Database/Context.cs +++ b/RomRepoMgr.Database/Context.cs @@ -23,37 +23,16 @@ // Copyright © 2020 Natalia Portillo *******************************************************************************/ -using System; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using RomRepoMgr.Database.Models; -using RomRepoMgr.Database.Resources; namespace RomRepoMgr.Database { public sealed class Context : DbContext { - static Context _singleton; - 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 Files { get; set; } public DbSet RomSets { get; set; } public DbSet Machines { get; set; } @@ -63,8 +42,6 @@ namespace RomRepoMgr.Database public DbSet Medias { get; set; } public DbSet MediasByMachines { get; set; } - public static void ReplaceSingleton(string dbPath) => _singleton = Create(dbPath); - public static Context Create(string dbPath) { var optionsBuilder = new DbContextOptionsBuilder(); diff --git a/RomRepoMgr/ViewModels/EditDatViewModel.cs b/RomRepoMgr/ViewModels/EditDatViewModel.cs index 9ea8835..57e7131 100644 --- a/RomRepoMgr/ViewModels/EditDatViewModel.cs +++ b/RomRepoMgr/ViewModels/EditDatViewModel.cs @@ -203,7 +203,9 @@ namespace RomRepoMgr.ViewModels 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) return; @@ -218,7 +220,7 @@ namespace RomRepoMgr.ViewModels romSetDb.Version = Version; romSetDb.UpdatedOn = DateTime.UtcNow; - await Context.Singleton.SaveChangesAsync(); + await ctx.SaveChangesAsync(); RomSetModified?.Invoke(this, new RomSetEventArgs { diff --git a/RomRepoMgr/ViewModels/RemoveDatViewModel.cs b/RomRepoMgr/ViewModels/RemoveDatViewModel.cs index a1b9a82..9708a04 100644 --- a/RomRepoMgr/ViewModels/RemoveDatViewModel.cs +++ b/RomRepoMgr/ViewModels/RemoveDatViewModel.cs @@ -59,20 +59,22 @@ namespace RomRepoMgr.ViewModels internal void OnOpened() => Task.Run(() => { + using var ctx = Context.Create(Settings.Settings.Current.DatabasePath); + Dispatcher.UIThread.Post(() => StatusMessage = Localization.RetrievingRomSetFromDatabase); - RomSet romSet = Context.Singleton.RomSets.Find(_romSetId); + RomSet romSet = ctx.RomSets.Find(_romSetId); if(romSet == null) return; Dispatcher.UIThread.Post(() => StatusMessage = Localization.RemovingRomSetFromDatabase); - Context.Singleton.RomSets.Remove(romSet); + ctx.RomSets.Remove(romSet); Dispatcher.UIThread.Post(() => StatusMessage = Localization.SavingChangesToDatabase); - Context.Singleton.SaveChanges(); + ctx.SaveChanges(); Dispatcher.UIThread.Post(() => StatusMessage = Localization.RemovingDatFileFromRepo); diff --git a/RomRepoMgr/ViewModels/SettingsViewModel.cs b/RomRepoMgr/ViewModels/SettingsViewModel.cs index bd76d9d..ff0606f 100644 --- a/RomRepoMgr/ViewModels/SettingsViewModel.cs +++ b/RomRepoMgr/ViewModels/SettingsViewModel.cs @@ -321,10 +321,7 @@ namespace RomRepoMgr.ViewModels void ExecuteSaveCommand() { if(_databaseChanged) - { Settings.Settings.Current.DatabasePath = DatabasePath; - Context.ReplaceSingleton(DatabasePath); - } if(_repositoryChanged) Settings.Settings.Current.RepositoryPath = RepositoryPath; diff --git a/RomRepoMgr/ViewModels/SplashWindowViewModel.cs b/RomRepoMgr/ViewModels/SplashWindowViewModel.cs index 6b9c84d..e5cb4b4 100644 --- a/RomRepoMgr/ViewModels/SplashWindowViewModel.cs +++ b/RomRepoMgr/ViewModels/SplashWindowViewModel.cs @@ -255,7 +255,7 @@ namespace RomRepoMgr.ViewModels if(!Directory.Exists(dbPathFolder)) Directory.CreateDirectory(dbPathFolder); - _ = Context.Singleton; + using var ctx = Context.Create(Settings.Settings.Current.DatabasePath); Dispatcher.UIThread.Post(MigrateDatabase); } @@ -283,7 +283,9 @@ namespace RomRepoMgr.ViewModels { try { - Context.Singleton.Database.Migrate(); + using var ctx = Context.Create(Settings.Settings.Current.DatabasePath); + + ctx.Database.Migrate(); Dispatcher.UIThread.Post(LoadRomSets); } @@ -311,50 +313,52 @@ namespace RomRepoMgr.ViewModels { try { + using var ctx = Context.Create(Settings.Settings.Current.DatabasePath); + GotRomSets?.Invoke(this, new RomSetsEventArgs { - RomSets = Context.Singleton.RomSets.OrderBy(r => r.Name).ThenBy(r => r.Version). - ThenBy(r => r.Date).ThenBy(r => r.Description).ThenBy(r => r.Comment). - ThenBy(r => r.Filename).Select(r => new RomSetModel - { - Id = r.Id, - Author = r.Author, - Comment = r.Comment, - Date = r.Date, - Description = r.Description, - Filename = r.Filename, - Homepage = r.Homepage, - Name = r.Name, - Sha384 = r.Sha384, - Version = r.Version, - TotalMachines = r.Machines.Count, - CompleteMachines = - r.Machines.Count(m => m.Files.Count > 0 && m.Disks.Count == 0 && - m.Files.All(f => f.File.IsInRepo)) + - r.Machines.Count(m => m.Disks.Count > 0 && m.Files.Count == 0 && - m.Disks.All(f => f.Disk.IsInRepo)) + - r.Machines.Count(m => m.Files.Count > 0 && m.Disks.Count > 0 && - m.Files.All(f => f.File.IsInRepo) && - m.Disks.All(f => f.Disk.IsInRepo)), - IncompleteMachines = - r.Machines.Count(m => m.Files.Count > 0 && m.Disks.Count == 0 && - m.Files.Any(f => !f.File.IsInRepo)) + - r.Machines.Count(m => m.Disks.Count > 0 && m.Files.Count == 0 && - m.Disks.Any(f => !f.Disk.IsInRepo)) + - r.Machines.Count(m => m.Files.Count > 0 && m.Disks.Count > 0 && - (m.Files.Any(f => !f.File.IsInRepo) || - m.Disks.Any(f => !f.Disk.IsInRepo))), - TotalRoms = r.Machines.Sum(m => m.Files.Count) + - r.Machines.Sum(m => m.Disks.Count) + - r.Machines.Sum(m => m.Medias.Count), - HaveRoms = r.Machines.Sum(m => m.Files.Count(f => f.File.IsInRepo)) + - r.Machines.Sum(m => m.Disks.Count(f => f.Disk.IsInRepo)) + - r.Machines.Sum(m => m.Medias.Count(f => f.Media.IsInRepo)), - MissRoms = r.Machines.Sum(m => m.Files.Count(f => !f.File.IsInRepo)) + - r.Machines.Sum(m => m.Disks.Count(f => !f.Disk.IsInRepo)) + - r.Machines.Sum(m => m.Medias.Count(f => !f.Media.IsInRepo)), - Category = r.Category - }).ToList() + RomSets = ctx.RomSets.OrderBy(r => r.Name).ThenBy(r => r.Version).ThenBy(r => r.Date). + ThenBy(r => r.Description).ThenBy(r => r.Comment).ThenBy(r => r.Filename). + Select(r => new RomSetModel + { + Id = r.Id, + Author = r.Author, + Comment = r.Comment, + Date = r.Date, + Description = r.Description, + Filename = r.Filename, + Homepage = r.Homepage, + Name = r.Name, + Sha384 = r.Sha384, + Version = r.Version, + TotalMachines = r.Machines.Count, + CompleteMachines = + r.Machines.Count(m => m.Files.Count > 0 && m.Disks.Count == 0 && + m.Files.All(f => f.File.IsInRepo)) + + r.Machines.Count(m => m.Disks.Count > 0 && m.Files.Count == 0 && + m.Disks.All(f => f.Disk.IsInRepo)) + + r.Machines.Count(m => m.Files.Count > 0 && m.Disks.Count > 0 && + m.Files.All(f => f.File.IsInRepo) && + m.Disks.All(f => f.Disk.IsInRepo)), + IncompleteMachines = + r.Machines.Count(m => m.Files.Count > 0 && m.Disks.Count == 0 && + m.Files.Any(f => !f.File.IsInRepo)) + + r.Machines.Count(m => m.Disks.Count > 0 && m.Files.Count == 0 && + m.Disks.Any(f => !f.Disk.IsInRepo)) + + r.Machines.Count(m => m.Files.Count > 0 && m.Disks.Count > 0 && + (m.Files.Any(f => !f.File.IsInRepo) || + m.Disks.Any(f => !f.Disk.IsInRepo))), + TotalRoms = r.Machines.Sum(m => m.Files.Count) + + r.Machines.Sum(m => m.Disks.Count) + + r.Machines.Sum(m => m.Medias.Count), + HaveRoms = r.Machines.Sum(m => m.Files.Count(f => f.File.IsInRepo)) + + r.Machines.Sum(m => m.Disks.Count(f => f.Disk.IsInRepo)) + + r.Machines.Sum(m => m.Medias.Count(f => f.Media.IsInRepo)), + MissRoms = r.Machines.Sum(m => m.Files.Count(f => !f.File.IsInRepo)) + + r.Machines.Sum(m => m.Disks.Count(f => !f.Disk.IsInRepo)) + + r.Machines.Sum(m => m.Medias.Count(f => !f.Media.IsInRepo)), + Category = r.Category + }).ToList() }); Dispatcher.UIThread.Post(LoadMainWindow);