EntityConstructorDescriptor.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using Ant.Common;
  2. using Ant.Mapper;
  3. using Ant.Utility;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Reflection;
  8. namespace Ant.Descriptors
  9. {
  10. public class EntityConstructorDescriptor
  11. {
  12. EntityMemberMapper _mapper = null;
  13. EntityConstructor _entityConstructor = null;
  14. EntityConstructorDescriptor(ConstructorInfo constructorInfo)
  15. {
  16. this.ConstructorInfo = constructorInfo;
  17. this.Init();
  18. }
  19. void Init()
  20. {
  21. ConstructorInfo constructor = this.ConstructorInfo;
  22. Type type = constructor.DeclaringType;
  23. if (AntUtils.IsAnonymousType(type))
  24. {
  25. ParameterInfo[] parameters = constructor.GetParameters();
  26. this.MemberParameterMap = new Dictionary<MemberInfo, ParameterInfo>(parameters.Length);
  27. foreach (ParameterInfo parameter in parameters)
  28. {
  29. PropertyInfo prop = type.GetProperty(parameter.Name);
  30. this.MemberParameterMap.Add(prop, parameter);
  31. }
  32. }
  33. else
  34. this.MemberParameterMap = new Dictionary<MemberInfo, ParameterInfo>(0);
  35. }
  36. public ConstructorInfo ConstructorInfo { get; private set; }
  37. public Dictionary<MemberInfo, ParameterInfo> MemberParameterMap { get; private set; }
  38. public Func<IDataReader, ReaderOrdinalEnumerator, ObjectActivatorEnumerator, object> GetInstanceCreator()
  39. {
  40. EntityConstructor entityConstructor = null;
  41. if (null == this._entityConstructor)
  42. {
  43. this._entityConstructor = EntityConstructor.GetInstance(this.ConstructorInfo);
  44. }
  45. entityConstructor = this._entityConstructor;
  46. return entityConstructor.InstanceCreator;
  47. }
  48. /// <summary>
  49. /// 获取实体映射关系
  50. /// </summary>
  51. /// <returns></returns>
  52. public EntityMemberMapper GetEntityMemberMapper()
  53. {
  54. EntityMemberMapper mapper = null;
  55. if (null == this._mapper)
  56. {
  57. this._mapper = EntityMemberMapper.GetInstance(this.ConstructorInfo.DeclaringType);
  58. }
  59. mapper = this._mapper;
  60. return mapper;
  61. }
  62. static readonly System.Collections.Concurrent.ConcurrentDictionary<ConstructorInfo, EntityConstructorDescriptor> InstanceCache = new System.Collections.Concurrent.ConcurrentDictionary<ConstructorInfo, EntityConstructorDescriptor>();
  63. /// <summary>
  64. ///
  65. /// </summary>
  66. /// <param name="constructorInfo"></param>
  67. /// <returns></returns>
  68. public static EntityConstructorDescriptor GetInstance(ConstructorInfo constructorInfo)
  69. {
  70. EntityConstructorDescriptor instance;
  71. if (!InstanceCache.TryGetValue(constructorInfo, out instance))
  72. {
  73. lock (constructorInfo)
  74. {
  75. if (!InstanceCache.TryGetValue(constructorInfo, out instance))
  76. {
  77. instance = new EntityConstructorDescriptor(constructorInfo);
  78. InstanceCache.GetOrAdd(constructorInfo, instance);
  79. }
  80. }
  81. }
  82. return instance;
  83. }
  84. }
  85. }