You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EntityBase.cs 3.8KB

1 anno fa
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.ComponentModel.DataAnnotations;
  3. using System.Linq;
  4. using HealthMonitor.Util.Common;
  5. using HealthMonitor.Util.Entities.Interfaces;
  6. namespace HealthMonitor.Util.Entities.Base
  7. {
  8. public abstract class EntityBase : IEntity
  9. {
  10. /// <summary>
  11. /// 获取全局(数据库内)唯一键
  12. /// </summary>
  13. /// <param name="key">需要赋予实体键的值,多个实体键赋予的值,用逗号分隔</param>
  14. /// <returns></returns>
  15. public string GetUniqueKey(string? key = null)
  16. {
  17. var type = this.GetType();
  18. if(EntityUtils.HasAutoGenerateKey(type))
  19. {
  20. return $"{type.Name}_{Guid.NewGuid()}";
  21. }
  22. if (!string.IsNullOrWhiteSpace(key)) AssignEntityKey(key);
  23. var propertyValues = type.GetProperties().Where(e => e.GetCustomAttributes(typeof(KeyAttribute), false).Length > 0)
  24. .Select(e => e.GetValue(this) + "");
  25. string str = $"{type.Name}_{string.Join("_", propertyValues)}";
  26. return str;
  27. }
  28. /// <summary>
  29. /// 获取全局(数据库内)唯一键(通过实体的主键创建,而不会创建随机键)
  30. /// </summary>
  31. /// <returns></returns>
  32. public string GetRequiredUniqueKey()
  33. {
  34. var type = this.GetType();
  35. var propertyValues = type.GetProperties().Where(e => e.GetCustomAttributes(typeof(KeyAttribute), false).Length > 0)
  36. .Select(e => e.GetValue(this) + "").ToList();
  37. if (propertyValues.Count() == 0 || (propertyValues[0] + "") == "0") throw new ArgumentException($"实体{type.Name}主键无效");
  38. string str = $"{type.Name}_{string.Join("_", propertyValues)}";
  39. return str;
  40. }
  41. /// <summary>
  42. /// 获取主键(数据表内唯一)
  43. /// </summary>
  44. /// <returns></returns>
  45. public string GetPrimaryKey()
  46. {
  47. var type = this.GetType();
  48. var propertyValues = type.GetProperties().Where(e => e.GetCustomAttributes(typeof(KeyAttribute), false).Length > 0)
  49. .Select(e => e.GetValue(this) + "");
  50. string str = string.Join("_", propertyValues);
  51. return str;
  52. }
  53. /// <summary>
  54. /// 为实体的键(标记了KeyAttribute)属性赋值
  55. /// </summary>
  56. /// <param name="key"></param>
  57. public virtual void AssignEntityKey(string key)
  58. {
  59. var type = this.GetType();
  60. try
  61. {
  62. var properties = type.GetProperties().Where(e => e.GetCustomAttributes(typeof(KeyAttribute), false).Length > 0);
  63. if (!string.IsNullOrWhiteSpace(key))
  64. {
  65. //为字段赋值
  66. var values = key.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  67. if (properties.Count() == 1 || properties.Count() == values.Count())
  68. {
  69. int i = 0;
  70. foreach (var p in properties) p.SetValue(this, values[i]);
  71. }
  72. }
  73. }
  74. catch (Exception) { }
  75. }
  76. /// <summary>
  77. /// 断言实体各个字段值符合要求
  78. /// </summary>
  79. public void AssertValidate()
  80. {
  81. var type = this.GetType();
  82. var properties = type.GetProperties();
  83. var requiredProperties = properties.Where(e => e.GetCustomAttributes(typeof(RequiredAttribute), false).Length > 0).ToList();
  84. requiredProperties.ForEach(e =>
  85. {
  86. var value = e.GetValue(this);
  87. if (value == null)
  88. {
  89. //throw new ArgumentException($"字段{e.Name}值能为空");
  90. //赋默认值
  91. object? v = e.PropertyType.IsValueType ? Activator.CreateInstance(e.PropertyType) : string.Empty;
  92. e.SetValue(this, v);
  93. }
  94. });
  95. var limitedProperties = properties.Where(e => e.GetCustomAttributes(typeof(StringLengthAttribute), false).Length > 0).ToList();
  96. limitedProperties.ForEach(e =>
  97. {
  98. var attr = (e.GetCustomAttributes(typeof(StringLengthAttribute), false) as StringLengthAttribute[])![0];
  99. int max = attr.MaximumLength;
  100. int min = attr.MinimumLength;
  101. var strValue = e.GetValue(this) + "";
  102. if (strValue.Length > max || strValue.Length < min) throw new ArgumentException($"字段{e.Name}值长度大于{max},或者小于{min}");
  103. });
  104. }
  105. }
  106. }