ModuleSetting.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using Quadarax.Foundation.Core.Data.Interface.Entity;
  2. using Quadarax.Foundation.Core.Object.Extensions;
  3. using Quadarax.Foundation.Core.Reflection.Extensions;
  4. namespace Quadarax.Application.QLiberace.Common.Settings
  5. {
  6. public abstract class ModuleSetting : IDto
  7. {
  8. public void SetData(IEnumerable<Tuple<string, string, object?>> data)
  9. {
  10. if (data == null)
  11. throw new ArgumentNullException(nameof(data));
  12. var properties = this.GetType().GetAllProperties();
  13. foreach (var item in data)
  14. {
  15. var property = properties.FirstOrDefault(x => string.Equals(x.Name, item.Item1));
  16. if (property == null)
  17. throw ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.SettingsHasNoPropertyCode,
  18. item.Item1, this.GetType().Name);
  19. property.SetValue(this, item.Item3);
  20. }
  21. }
  22. public IEnumerable<Tuple<string, string, object?>> GetData()
  23. {
  24. var properties = this.GetType().GetAllProperties();
  25. var result = this.GetAllPropertyValues().
  26. Select(x=> new Tuple<string, string, object?>(x.Key, properties.First(y=>y.Name == x.Key).GetFinalPropertyType().FullName ?? string.Empty, properties.First(y=>y.Name == x.Key).GetValue(this))).ToArray();
  27. return result;
  28. }
  29. }
  30. }