using System.ComponentModel; using Quadarax.Application.QLiberace.Base.Entities.Interfaces; using Quadarax.Application.QLiberace.Common.Entities.Base; using System.ComponentModel.DataAnnotations.Schema; using Quadarax.Foundation.Core.Reflection; namespace Quadarax.Application.QLiberace.Base.Entities; [Table(Constants.Modules.Base.TblSetting,Schema = Constants.Modules.Base.Schema)] public class Setting : QlbrcEntityTrackedWithoutId, IStructSetting { #region *** Properties *** public long ParentId { get; set; } public int SequenceNo { get; set; } public string ModuleCode { get; set; } public string Code { get; set; } public string? Description { get; set; } public string ValueTypeQualified { get; set; } public string? Value { get; set; } 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}'."); } }