Setting.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. public class Setting : QlbrcEntityTenant, IStructSetting
  10. {
  11. #region *** Properties ***
  12. public long? ParentId { get; set; }
  13. public int SequenceNo { get; set; }
  14. public string ModuleCode { get; set; } = null!;
  15. public string Code { get; set; } = null!;
  16. public string? Description { get; set; }
  17. public string ValueTypeQualified { get; set; } = null!;
  18. public string? Value { get; set; }
  19. public bool IsEnabled { get; set; }
  20. #endregion
  21. #region *** Private fields ***
  22. #endregion
  23. public TValue? GetTypedValue<TValue>()
  24. {
  25. return (TValue?)GetTypedValue();
  26. }
  27. public object? GetTypedValue()
  28. {
  29. if (string.IsNullOrEmpty(ValueTypeQualified))
  30. throw new InvalidDataException(
  31. $"Cannot get typed value of setting '{Code}' (module '{ModuleCode}') because ValueTypeQualified is not set.");
  32. if (string.IsNullOrEmpty(Value))
  33. return null;
  34. var type = TypeUtils.GetRemoteType(ValueTypeQualified);
  35. var converter = TypeDescriptor.GetConverter(type);
  36. if (converter.CanConvertFrom(typeof(string)))
  37. {
  38. return converter.ConvertFrom(Value);
  39. }
  40. throw new InvalidCastException($"Cannot convert value of setting '{Code}' (module '{ModuleCode}') from type '{typeof(string).Name}' to '{ValueTypeQualified}'.");
  41. }
  42. }