mirror of
https://github.com/claunia/romrepomgr.git
synced 2025-12-16 19:24:51 +00:00
Remove database singleton.
This commit is contained in:
@@ -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,8 +341,7 @@ 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) ||
|
||||
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) ||
|
||||
@@ -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,7 +509,7 @@ 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) ||
|
||||
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,10 +631,7 @@ 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) ||
|
||||
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));
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -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,9 +109,11 @@ namespace RomRepoMgr.Core.Workers
|
||||
Message = machine.Name
|
||||
});
|
||||
|
||||
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath);
|
||||
|
||||
string machineName = machine.Name;
|
||||
|
||||
Dictionary<string, MediaByMachine> mediasByMachine = Context.Singleton.MediasByMachines.
|
||||
Dictionary<string, MediaByMachine> mediasByMachine = ctx.MediasByMachines.
|
||||
Where(f => f.Machine.Id == machine.Id &&
|
||||
f.Media.IsInRepo).
|
||||
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 &&
|
||||
f.Disk.IsInRepo).
|
||||
ToDictionary(f => f.Name);
|
||||
@@ -476,8 +480,7 @@ namespace RomRepoMgr.Core.Workers
|
||||
}
|
||||
}
|
||||
|
||||
_filesByMachine = Context.Singleton.FilesByMachines.
|
||||
Where(f => f.Machine.Id == machine.Id && f.File.IsInRepo).
|
||||
_filesByMachine = ctx.FilesByMachines.Where(f => f.Machine.Id == machine.Id && f.File.IsInRepo).
|
||||
ToDictionary(f => f.Name);
|
||||
|
||||
if(_filesByMachine.Count == 0)
|
||||
|
||||
@@ -19,6 +19,7 @@ namespace RomRepoMgr.Core.Workers
|
||||
public class FileImporter
|
||||
{
|
||||
const long BUFFER_SIZE = 131072;
|
||||
readonly Context _ctx;
|
||||
readonly bool _deleteAfterImport;
|
||||
readonly List<DbDisk> _newDisks;
|
||||
readonly List<DbFile> _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,7 +357,7 @@ 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] ||
|
||||
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] ||
|
||||
@@ -634,7 +636,7 @@ namespace RomRepoMgr.Core.Workers
|
||||
md5 != null)
|
||||
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));
|
||||
|
||||
if(dbDisk == null)
|
||||
@@ -937,7 +939,7 @@ namespace RomRepoMgr.Core.Workers
|
||||
md5 != null)
|
||||
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.Md5 != null && d.Md5 == sha1));
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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<DbFile> Files { get; set; }
|
||||
public DbSet<RomSet> RomSets { get; set; }
|
||||
public DbSet<Machine> Machines { get; set; }
|
||||
@@ -63,8 +42,6 @@ namespace RomRepoMgr.Database
|
||||
public DbSet<DbMedia> Medias { get; set; }
|
||||
public DbSet<MediaByMachine> MediasByMachines { get; set; }
|
||||
|
||||
public static void ReplaceSingleton(string dbPath) => _singleton = Create(dbPath);
|
||||
|
||||
public static Context Create(string dbPath)
|
||||
{
|
||||
var optionsBuilder = new DbContextOptionsBuilder();
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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,11 +313,13 @@ 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
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user