using System.ComponentModel; using System.ComponentModel.DataAnnotations; using Quadarax.Application.QLiberace.Base.Entities.Interfaces; using Quadarax.Application.QLiberace.Common.Entities.Base; using System.ComponentModel.DataAnnotations.Schema; using Microsoft.EntityFrameworkCore; using Quadarax.Foundation.Core.Reflection; namespace Quadarax.Application.QLiberace.Base.Entities; [Table(Constants.Modules.Base.TblSetting,Schema = Constants.Modules.Base.Schema)] [Index("ModuleCode","Code", Name="IDX_CODE_MODULECODE", IsUnique = true )] public class Setting : QlbrcEntityTrackedWithoutId, IStructSetting { #region *** Properties *** [ForeignKey("FK_Setting_Parent_Id")] public long? ParentId { get; set; } [Required] [DefaultValue(0)] public int SequenceNo { get; set; } [MaxLength(10)] [Required(AllowEmptyStrings = false)] public string ModuleCode { get; set; } = null!; [MaxLength(100)] [Required(AllowEmptyStrings = false)] public string Code { get; set; } = null!; [MaxLength(500)] public string? Description { get; set; } [MaxLength(500)] [Required(AllowEmptyStrings = false)] public string ValueTypeQualified { get; set; } = null!; [MaxLength(5000)] [Required] public string? Value { get; set; } [Required] [DefaultValue(false)] public bool IsEnabled { get; set; } #endregion #region *** Private fields *** #endregion public TValue? GetTypedValue() { return (TValue?)GetTypedValue(); } public object? GetTypedValue() { if (string.IsNullOrEmpty(ValueTypeQualified)) throw new InvalidDataException( $"Cannot get typed value of setting '{Code}' (module '{ModuleCode}') because ValueTypeQualified is not set."); if (string.IsNullOrEmpty(Value)) return null; var type = TypeUtils.GetRemoteType(ValueTypeQualified); var converter = TypeDescriptor.GetConverter(type); if (converter.CanConvertFrom(typeof(string))) { return converter.ConvertFrom(Value); } throw new InvalidCastException($"Cannot convert value of setting '{Code}' (module '{ModuleCode}') from type '{typeof(string).Name}' to '{ValueTypeQualified}'."); } }