Setting.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System.ComponentModel;
  2. using Quadarax.Application.QLiberace.Base.Entities.Interfaces;
  3. using Quadarax.Application.QLiberace.Common.Entities.Base;
  4. using System.ComponentModel.DataAnnotations.Schema;
  5. using Quadarax.Foundation.Core.Reflection;
  6. namespace Quadarax.Application.QLiberace.Base.Entities;
  7. [Table(Constants.Modules.Base.TblSetting,Schema = Constants.Modules.Base.Schema)]
  8. public class Setting : QlbrcEntityTrackedWithoutId, IStructSetting
  9. {
  10. #region *** Properties ***
  11. public long ParentId { get; set; }
  12. public int SequenceNo { get; set; }
  13. public string ModuleCode { get; set; }
  14. public string Code { get; set; }
  15. public string? Description { get; set; }
  16. public string ValueTypeQualified { get; set; }
  17. public string? Value { get; set; }
  18. public bool IsEnabled { get; set; }
  19. #endregion
  20. #region *** Private fields ***
  21. #endregion
  22. public TValue? GetTypedValue<TValue>()
  23. {
  24. return (TValue?)GetTypedValue();
  25. }
  26. public object? GetTypedValue()
  27. {
  28. if (string.IsNullOrEmpty(ValueTypeQualified))
  29. throw new InvalidDataException(
  30. $"Cannot get typed value of setting '{Code}' (module '{ModuleCode}') because ValueTypeQualified is not set.");
  31. if (string.IsNullOrEmpty(Value))
  32. return null;
  33. var type = TypeUtils.GetRemoteType(ValueTypeQualified);
  34. var converter = TypeDescriptor.GetConverter(type);
  35. if (converter.CanConvertFrom(typeof(string)))
  36. {
  37. return converter.ConvertFrom(Value);
  38. }
  39. throw new InvalidCastException($"Cannot convert value of setting '{Code}' (module '{ModuleCode}') from type '{typeof(string).Name}' to '{ValueTypeQualified}'.");
  40. }
  41. }