Setting.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System.ComponentModel;
  2. using System.ComponentModel.DataAnnotations;
  3. using Quadarax.Application.QLiberace.Base.Entities.Interfaces;
  4. using Quadarax.Application.QLiberace.Common.Entities.Base;
  5. using System.ComponentModel.DataAnnotations.Schema;
  6. using Microsoft.EntityFrameworkCore;
  7. using Quadarax.Foundation.Core.Reflection;
  8. namespace Quadarax.Application.QLiberace.Base.Entities;
  9. [Table(Constants.Modules.Base.TblSetting,Schema = Constants.Modules.Base.Schema)]
  10. [Index("ModuleCode","Code", Name="IDX_CODE_MODULECODE", IsUnique = true )]
  11. public class Setting : QlbrcEntityTrackedWithoutId, IStructSetting
  12. {
  13. #region *** Properties ***
  14. [ForeignKey("FK_Setting_Parent_Id")]
  15. public long? ParentId { get; set; }
  16. [Required]
  17. [DefaultValue(0)]
  18. public int SequenceNo { get; set; }
  19. [MaxLength(10)]
  20. [Required(AllowEmptyStrings = false)]
  21. public string ModuleCode { get; set; } = null!;
  22. [MaxLength(100)]
  23. [Required(AllowEmptyStrings = false)]
  24. public string Code { get; set; } = null!;
  25. [MaxLength(500)]
  26. public string? Description { get; set; }
  27. [MaxLength(500)]
  28. [Required(AllowEmptyStrings = false)]
  29. public string ValueTypeQualified { get; set; } = null!;
  30. [MaxLength(5000)]
  31. [Required]
  32. public string? Value { get; set; }
  33. [Required]
  34. [DefaultValue(false)]
  35. public bool IsEnabled { get; set; }
  36. #endregion
  37. #region *** Private fields ***
  38. #endregion
  39. public TValue? GetTypedValue<TValue>()
  40. {
  41. return (TValue?)GetTypedValue();
  42. }
  43. public object? GetTypedValue()
  44. {
  45. if (string.IsNullOrEmpty(ValueTypeQualified))
  46. throw new InvalidDataException(
  47. $"Cannot get typed value of setting '{Code}' (module '{ModuleCode}') because ValueTypeQualified is not set.");
  48. if (string.IsNullOrEmpty(Value))
  49. return null;
  50. var type = TypeUtils.GetRemoteType(ValueTypeQualified);
  51. var converter = TypeDescriptor.GetConverter(type);
  52. if (converter.CanConvertFrom(typeof(string)))
  53. {
  54. return converter.ConvertFrom(Value);
  55. }
  56. throw new InvalidCastException($"Cannot convert value of setting '{Code}' (module '{ModuleCode}') from type '{typeof(string).Name}' to '{ValueTypeQualified}'.");
  57. }
  58. }