AccountingNext/Schaad.Accounting.Db/Repositories/BaseRepository.cs
Claudio Schaad 8961bb234c PR C: mechanical cleanup (typos, packages, formatting, culture)
- Consolidate decimal.ToFormattedString into Schaad.Accounting.Formatting
- Rename EnsureFileExisits -> EnsureFileExists and
  SplitPredefiniton* -> SplitPredefinition* (class, interface, files, DI reg)
- AccountRepository: single-save year-rollover start-balance seed
- Drop legacy .NET Standard packages redundant on net9.0 and the stale
  Interfaces\Extensions\ folder entry
- Set culture via RequestLocalizationOptions middleware
- Plan: item 9 (unit of work) split off into a dedicated PR D

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-07-02 20:46:55 +02:00

104 lines
No EOL
3.3 KiB
C#

using System;
using System.Collections.Concurrent;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using Schaad.Accounting.Interfaces;
namespace Schaad.Accounting.Repositories
{
public abstract class BaseRepository
{
// One lock per absolute file path so concurrent Save+Save and Save+Load are serialized
// and can't observe a half-written file.
private static readonly ConcurrentDictionary<string, object> FileLocks =
new(StringComparer.OrdinalIgnoreCase);
protected readonly ISettingsService settingsService;
protected BaseRepository(ISettingsService settingsService)
{
this.settingsService = settingsService;
}
protected void EnsureFileExists(string fileName)
{
string filePath = Path.Combine(settingsService.GetDbPath(), fileName);
lock (GetLock(filePath))
{
if (File.Exists(filePath) == false)
{
var lastYearFile = Path.Combine(settingsService.GetLastYearDbPath(), fileName);
if (File.Exists(lastYearFile))
{
File.Copy(lastYearFile, filePath);
}
}
}
}
/// <summary>
/// Save an object to an xml file. Writes to a .tmp sibling and then atomically
/// renames it, so a crash mid-write leaves the previous file intact.
/// </summary>
protected void Save<T>(T obj, string fileName)
{
var filePath = Path.Combine(settingsService.GetDbPath(), fileName);
var tmpPath = filePath + ".tmp";
lock (GetLock(filePath))
{
var settings = new XmlWriterSettings
{
Encoding = Encoding.UTF8,
Indent = true
};
try
{
using (var writer = XmlWriter.Create(tmpPath, settings))
{
var serializer = new XmlSerializer(typeof(T));
serializer.Serialize(writer, obj);
}
File.Move(tmpPath, filePath, overwrite: true);
}
catch
{
if (File.Exists(tmpPath))
{
try { File.Delete(tmpPath); } catch { /* best effort */ }
}
throw;
}
}
}
/// <summary>
/// Load an object from an xml file
/// </summary>
protected T Load<T>(string fileName)
{
var filePath = Path.Combine(settingsService.GetDbPath(), fileName);
lock (GetLock(filePath))
{
if (File.Exists(filePath) == false)
{
return default(T);
}
using (XmlReader reader = XmlReader.Create(filePath))
{
var serializer = new XmlSerializer(typeof(T));
return (T)serializer.Deserialize(reader);
}
}
}
private static object GetLock(string filePath)
=> FileLocks.GetOrAdd(filePath, _ => new object());
}
}