public T GetValueOrDefault <T>(Fältkod fk, T defaultValue = default(T)) { if (_dict.TryGetValue(fk, out var f)) { return((T)f.Value); } return(defaultValue); }
public static bool Contains(this List <Fält> source, Fältkod fk) { foreach (var f in source) { if (f.Key == fk) { return(true); } } return(false); }
public static void Validate(Fältkod fk, object v) { var attr = GetFälttypAttribute(fk); switch (attr.Typ) { case Fälttyp.Belopp: { var min = 0; var max = Math.Pow(10, attr.MaxLength) - 1; var num = Convert.ToInt64(v, CultureInfo.InvariantCulture); if (!(min <= num && num <= max)) { throw new ArgumentException($"{FKId(fk)}: {min} - {max}"); } break; } case Fälttyp.Text: { var s = Convert.ToString(v, CultureInfo.InvariantCulture) ?? string.Empty; if (!(s.Length <= attr.MaxLength)) { throw new ArgumentException($"{FKId(fk)}: Max {attr.MaxLength} tecken."); } break; } case Fälttyp.Identitet: { if (!IsIdentitetValid(v)) { throw new ArgumentException($"{FKId(fk)}: SSÅÅMMDDNNNK: Ej korrekt person-/organisations-/samordningsnummer, dvs börjar inte på 16, 18, 19 eller 20 eller innehålla inte en korrekt kontrollsiffra."); } break; } case Fälttyp.SpecifikationsNummer: { var num = Convert.ToInt64(v, CultureInfo.InvariantCulture); if (!(1 <= num && num <= 9999999999)) { throw new ArgumentException($"{FKId(fk)}: 1 - 9999999999"); } break; } } }
public void AddOrUpdateValue(Fältkod fk, object v) { if (v is string) { v = Text.Normalize((string)v); } if (v != null) { FältHelper.Validate(fk, v); } if (_dict.ContainsKey(fk)) { _dict[fk] = new Fält(fk, v); } else { _dict.Add(fk, new Fält(fk, v)); } }
public Fält(Fältkod k, object v = null) { this.Key = k; this.Value = v; }
private static string FKId(Fältkod fk) { return($"FK{(int)fk:000}"); }
public static FälttypAttribute GetFälttypAttribute(Fältkod fk) { return(Metadata[fk]); }
public void Remove(Fältkod key) { _dict.Remove(key); }
public bool Contains(Fältkod key) { return(_dict.ContainsKey(key)); }
public Fält this[Fältkod fk] { get { return(_dict[fk]); } set { _dict[fk] = value; } }