From 58a7806fef67f2d2b15010adfb87c18726d0b8c3 Mon Sep 17 00:00:00 2001 From: JPVenson Date: Fri, 29 Apr 2022 11:09:08 +0200 Subject: [PATCH] #21 Fixed currency handler could try to enumerate duplicated currencies --- .../Predefined/Accounting/CurrencyHandler.cs | 60 ++++++++++++------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/Morestachio/Formatter/Predefined/Accounting/CurrencyHandler.cs b/Morestachio/Formatter/Predefined/Accounting/CurrencyHandler.cs index 42ea4e63..6b84a66f 100644 --- a/Morestachio/Formatter/Predefined/Accounting/CurrencyHandler.cs +++ b/Morestachio/Formatter/Predefined/Accounting/CurrencyHandler.cs @@ -3,6 +3,7 @@ using System.ComponentModel; using System.Globalization; using System.Linq; +using System.Threading; using Morestachio.Formatter.Framework.Attributes; namespace Morestachio.Formatter.Predefined.Accounting; @@ -14,32 +15,42 @@ public class CurrencyHandler { static CurrencyHandler() { - DefaultHandler = new CurrencyHandler(); - DefaultHandler.Currencies = CultureInfo.GetCultures(CultureTypes.SpecificCultures) - .GroupBy(e => e.LCID) - .Select(f => - { - //this has to be tried because in some systems we can get invalid regions - try - { - return new RegionInfo(f.Key); - } - catch (Exception e) + IDictionary GetSystemCurrencies() + { + return CultureInfo.GetCultures(CultureTypes.SpecificCultures) + .GroupBy(e => e.LCID) + .Select(f => { - return null; - } - }) - .Where(e => e != null) - .GroupBy(e => e.CurrencyEnglishName) - .Select(f => f.First()).ToDictionary(e => e.ISOCurrencySymbol, e => new Currency(e.CurrencySymbol, e.ISOCurrencySymbol)); + //this has to be tried because in some systems we can get invalid regions + try + { + return new RegionInfo(f.Key); + } + catch (Exception e) + { + return null; + } + }) + .Where(e => e != null) + .GroupBy(e => e.ISOCurrencySymbol) + .Select(f => f.First()) + .ToDictionary(e => e.ISOCurrencySymbol, e => new Currency(e.CurrencySymbol, e.ISOCurrencySymbol)); + } + + DefaultHandler = new CurrencyHandler(GetSystemCurrencies); } /// /// /// - public CurrencyHandler() + public CurrencyHandler() + : this(() => new Dictionary()) + { + } + + private CurrencyHandler(Func> currencyFactory) { - Currencies = new Dictionary(); + _currencies = new Lazy>(currencyFactory); ConversionFactors = new HashSet(); } @@ -49,11 +60,16 @@ public CurrencyHandler() [Description("The Default handler that contains all system wide known currencies")] public static CurrencyHandler DefaultHandler { get; } + private Lazy> _currencies; + /// /// The list of all known currencies /// [Description("The list of all known currencies")] - public IDictionary Currencies { get; private set; } + public IDictionary Currencies + { + get { return _currencies.Value; } + } /// /// A list of known conversions for Currencies @@ -67,9 +83,9 @@ public CurrencyHandler() /// public CurrencyHandler Clone() { - return new CurrencyHandler() + var currencies = new Dictionary(Currencies); + return new CurrencyHandler(() => currencies) { - Currencies = new Dictionary(Currencies), ConversionFactors = new HashSet(ConversionFactors) }; }