AccountingNext/Schaad.Accounting.Db/Repositories/BookingRuleRepository.cs
Claudio Schaad 1c7ba7f416 PR D: per-scope repository cache
Add a Scoped RepositoryCache that memoizes deserialized XML lists,
keyed by absolute file path. Each XML file is loaded at most once per
SignalR circuit; Save invalidates the corresponding entry so
subsequent reads reload from disk.

BaseRepository gains a LoadList<T>(fileName) helper; each of the seven
repositories now defines Get*List() as a one-liner over that helper
and takes RepositoryCache via the constructor.

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

61 lines
No EOL
1.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Schaad.Accounting.Interfaces;
using Schaad.Accounting.Models;
namespace Schaad.Accounting.Repositories
{
public class BookingRuleRepository : BaseRepository, IBookingRuleRepository
{
private readonly string BOOKING_RULES = "BookingRules.xml";
public BookingRuleRepository(ISettingsService settingsService, RepositoryCache cache)
: base(settingsService, cache)
{
EnsureFileExists(BOOKING_RULES);
}
/// <summary>
/// Load booking rules
/// </summary>
public List<BookingRule> GetBookingRuleList() => LoadList<BookingRule>(BOOKING_RULES);
/// <summary>
/// Save a booking rule (insert/update)
/// </summary>
public void SaveBookingRule(BookingRule bookingRule)
{
var bookingRules = GetBookingRuleList();
var existingRule = bookingRules.FirstOrDefault(a => a.Id == bookingRule.Id);
if (existingRule == null)
{
existingRule = new BookingRule();
bookingRules.Add(existingRule);
bookingRule.Id = Guid.NewGuid().ToString();
}
bookingRule.Copy(existingRule);
Save(bookingRules, BOOKING_RULES);
}
/// <summary>
/// Get booking rule
/// </summary>
public BookingRule GetBookingRule(string id)
{
var bookingRules = GetBookingRuleList();
return bookingRules.FirstOrDefault(t => t.Id == id);
}
/// <summary>
/// Delete booking rule
/// </summary>
public void DeleteBookingRule(string id)
{
var bookingRules = GetBookingRuleList();
bookingRules.RemoveAll(t => t.Id == id);
Save(bookingRules, BOOKING_RULES);
}
}
}