FormField.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Web.UI.WebControls;
  6. using DymWebForm.Common.Attributes;
  7. using DymWebForm.Common.Linq;
  8. using DymWebForm.Common.Reflection;
  9. namespace DymWebForm
  10. {
  11. /// <summary>
  12. /// Description of MyClass.
  13. /// </summary>
  14. public class FormField
  15. {
  16. //inst
  17. public PropertyInfo PropertyInfo { get; private set; }
  18. //prop
  19. public Func<object, object> GetValue { get; private set; }
  20. public Action<object, object> SetValue { get; private set; }
  21. //feature
  22. public bool IsPK { get; private set; }
  23. public bool IsFK { get; private set; }
  24. public Type FKParentType { get; private set; }
  25. public bool IsFKDisplay { get; private set; }
  26. public int DisplayOrder { get; private set; }
  27. public string FieldName { get; private set; }
  28. public string DisplayName { get; private set; }
  29. public Type DataType { get; private set; }
  30. public bool AllowNull { get; private set; }
  31. public string ControlID { get; private set; }
  32. public bool HasDefaultValueGenerator { get; private set; }
  33. public Func<object> GenerateDefaultValue { get; private set; }
  34. public Type ValidationType { get; private set; }
  35. //ctor
  36. public FormField(PropertyInfo pi)
  37. {
  38. PropertyInfo = pi;
  39. var pia = pi;
  40. if (pi.DeclaringType.IsDefined(typeof (DWFMetaAttribute), true))
  41. {
  42. var mt =
  43. ((DWFMetaAttribute) pi.DeclaringType.GetCustomAttributes(typeof (DWFMetaAttribute), true)[0])
  44. .Type;
  45. var mp = mt.GetProperty(pi.Name);
  46. if (mp != null)
  47. pia = mp;
  48. }
  49. GetValue = PropertyInfo.GenerateGetterFunc();
  50. SetValue = PropertyInfo.GenerateSetterAction();
  51. DataType = pi.PropertyType;
  52. IsPK = pia.HasAttribute<DWFPKAttribute>();
  53. IsFK = pia.HasAttribute<DWFFKAttribute>();
  54. IsFKDisplay = pia.HasAttribute<DWFFKDisplayAttribute>();
  55. HasDefaultValueGenerator = pia.HasAttribute<DWFDefaultValueGeneratorAttribute>(true);
  56. AllowNull = DataType.IsGenericType && DataType.GetGenericTypeDefinition() == typeof (Nullable<>)
  57. || pia.HasAttribute<DWFAllowNullAttribute>();
  58. ValidationType =
  59. pia.HasAttribute<DWFValidationTypeAttribute>()
  60. ? pia.GetCustomAttributes(typeof (DWFValidationTypeAttribute), true)
  61. .Select(p => ((DWFValidationTypeAttribute) p).ValidationType)
  62. .First()
  63. : null;
  64. if (IsFK)
  65. FKParentType = pia
  66. .GetCustomAttributes(typeof (DWFFKAttribute), true)
  67. .Select(p => ((DWFFKAttribute) p).ParentType)
  68. .FirstOrDefault();
  69. if (HasDefaultValueGenerator)
  70. GenerateDefaultValue =
  71. ((DWFDefaultValueGeneratorAttribute)
  72. pia.GetCustomAttributes(typeof (DWFDefaultValueGeneratorAttribute), true).First())
  73. .GenerateValue;
  74. DisplayOrder = pia.GetCustomAttributes(typeof (DWFDisplayOrderAttribute), true)
  75. .Select(p => (int?) ((DWFDisplayOrderAttribute) p).DisplayOrder)
  76. .FirstOrDefault() ?? int.MaxValue;
  77. FieldName = pi.Name;
  78. DisplayName = pia.GetDescriptionByAttribute(defaultResult: pi.Name);
  79. ControlID = pi.DeclaringType.Name + FieldName;
  80. }
  81. public WebControl CreateControl()
  82. {
  83. return DymWebFormControlMappnig.GetInst(DataType, ValidationType).CreateControl();
  84. }
  85. public DymWebFormValidation Validation
  86. {
  87. get
  88. {
  89. return DymWebFormControlMappnig.GetInst(DataType, ValidationType).Validation;
  90. }
  91. }
  92. }
  93. internal class FormFieldFactory
  94. {
  95. private readonly FormField[] ArrCache;
  96. public FormFieldFactory(Type t)
  97. {
  98. var mt = t
  99. .GetCustomAttributes(typeof(DWFMetaAttribute), true)
  100. .Select(p => ((DWFMetaAttribute)p).Type)
  101. .FirstOrDefault();
  102. var mp = mt == null
  103. ? null
  104. : mt.GetProperties().ToDictionary(p => p.Name, p => p);
  105. ArrCache = t.GetProperties()
  106. .Where(p =>
  107. //using meta
  108. (mp != null && mp.ContainsKey(p.Name) && mp[p.Name].IsDefined(typeof(DWFIgnoreAttribute), true) == false)
  109. || //not using meta
  110. (mp == null && p.IsDefined(typeof(DWFIgnoreAttribute), true) == false)
  111. )
  112. .SelectArray(p => new FormField(p))
  113. .OrderBy(p => p.DisplayOrder)
  114. .ToArray();
  115. }
  116. public FormField[] GetFormFields()
  117. {
  118. return ArrCache;
  119. }
  120. public object GetPKValue(object entity)
  121. {
  122. return GetFormFields()
  123. .Where(p => p.IsPK)
  124. .Select(p => p.GetValue(entity))
  125. .First();
  126. }
  127. public object GetFKValue(object entity)
  128. {
  129. return GetFormFields()
  130. .Where(p => p.IsFKDisplay)
  131. .Select(p => p.GetValue(entity))
  132. .First();
  133. }
  134. }
  135. }