Setting.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. public string? Value { get; set; }
  32. [Required]
  33. [DefaultValue(false)]
  34. public bool IsEnabled { get; set; }
  35. #endregion
  36. #region *** Private fields ***
  37. #endregion
  38. public TValue? GetTypedValue<TValue>()
  39. {
  40. return (TValue?)GetTypedValue();
  41. }
  42. public object? GetTypedValue()
  43. {
  44. if (string.IsNullOrEmpty(ValueTypeQualified))
  45. throw new InvalidDataException(
  46. $"Cannot get typed value of setting '{Code}' (module '{ModuleCode}') because ValueTypeQualified is not set.");
  47. if (string.IsNullOrEmpty(Value))
  48. return null;
  49. var type = TypeUtils.GetRemoteType(ValueTypeQualified);
  50. var converter = TypeDescriptor.GetConverter(type);
  51. if (converter.CanConvertFrom(typeof(string)))
  52. {
  53. return converter.ConvertFrom(Value);
  54. }
  55. throw new InvalidCastException($"Cannot convert value of setting '{Code}' (module '{ModuleCode}') from type '{typeof(string).Name}' to '{ValueTypeQualified}'.");
  56. }
  57. }