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.

lib.Automapper.asp 6.2KB

8 kuukautta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <%
  2. '=======================================================================================================================
  3. ' AUTOMAPPER CLASS
  4. '=======================================================================================================================
  5. 'Side Effects: Since src and target are passed ByRef to reduce unnecessary copying, if src is a recordset then the
  6. ' current record pointer is modified using src.MoveFirst and src.MoveNext. The end result is the current
  7. ' record pointer ends the operation at src.EOF.
  8. Class Automapper_Class
  9. Private m_src
  10. Private m_target
  11. Private m_statements
  12. Private m_statements_count
  13. Private Property Get Src : set Src = m_src : End Property
  14. Private Property Get Target : set Target = m_target : End Property
  15. Private Sub Class_Initialize
  16. m_statements = Array()
  17. m_statements_count = 0
  18. redim m_statements(m_statements_count)
  19. End Sub
  20. Private Sub ResetState
  21. m_statements_count = 0
  22. redim m_statements(m_statements_count)
  23. set m_src = Nothing
  24. set m_target = Nothing
  25. End Sub
  26. 'Maps all rs or object fields to corresponding fields in the specified class.
  27. Public Function AutoMap(src_obj, target_obj)
  28. Set AutoMap = FlexMap(src_obj, target_obj, empty)
  29. End Function
  30. 'Only maps fields specified in the field_names array (array of strings).
  31. 'If field_names is empty, attempts to map all fields from the passed rs or object.
  32. Public Function FlexMap(src_obj, target_obj, field_names)
  33. Set FlexMap = DynMap(src_obj, target_obj, field_names, empty)
  34. End Function
  35. 'Only maps fields specified in the field_names array (array of strings).
  36. 'If field_names is empty then src MUST be a recordset as it attempts to map all fields from the recordset.
  37. 'Since there is no reflection in vbscript, there is no way around this short of pseudo-reflection.
  38. Public Function DynMap(src_obj, target_obj, field_names, exprs)
  39. SetSource src_obj
  40. SetTarget target_obj
  41. dim field_name
  42. dim field_idx 'loop counter
  43. if IsEmpty(field_names) then 'map everything
  44. if typename(src_obj) = "Recordset" then
  45. for field_idx = 0 to src_obj.Fields.Count - 1
  46. field_name = src_obj.Fields.Item(field_idx).Name
  47. 'AddStatement field_name
  48. AddStatement BuildStatement(field_name)
  49. next
  50. elseif InStr(typename(src_obj), "Dictionary") > 0 then 'enables Scripting.Dictionary and IRequestDictionary for Request.Querystring and Request.Form
  51. for each field_name in src_obj
  52. AddStatement BuildStatement(field_name)
  53. next
  54. elseif not IsEmpty(src_obj.Class_Get_Properties) then
  55. dim props : props = src_obj.Class_Get_Properties
  56. for field_idx = 0 to ubound(props)
  57. field_name = props(field_idx)
  58. 'AddStatement field_name
  59. AddStatement BuildStatement(field_name)
  60. next
  61. else 'some invalid type of object
  62. Err.Raise 9, "Automapper.DynMap", "Cannot automatically map this source object. Expected recordset or object implementing Class_Get_Properties reflection, got: " & typename(src_obj)
  63. end if
  64. else 'map only specified fields
  65. for field_idx = lbound(field_names) to ubound(field_names)
  66. field_name = field_names(field_idx)
  67. 'AddStatement field_name
  68. AddStatement BuildStatement(field_name)
  69. next
  70. end if
  71. dim exprs_idx
  72. if not IsEmpty(exprs) then
  73. if typename(exprs) = "Variant()" then
  74. for exprs_idx = lbound(exprs) to ubound(exprs)
  75. 'field_name = exprs(exprs_idx)
  76. 'AddStatement field_name
  77. AddStatement exprs(exprs_idx)
  78. next
  79. else 'assume string or string-like default value
  80. AddStatement exprs
  81. end if
  82. end if
  83. 'Can't pre-join the statements because if one fails the rest of them fail too... :(
  84. 'dim joined_statements : joined_statements = Join(m_statements, " : ")
  85. 'put joined_statements
  86. 'suspend errors to prevent failing when attempting to map a field that does not exist in the class
  87. on error resume next
  88. dim stmt_idx
  89. for stmt_idx = 0 to ubound(m_statements)
  90. Execute m_statements(stmt_idx)
  91. next
  92. on error goto 0
  93. set DynMap = m_target
  94. ResetState
  95. End Function
  96. Private Sub SetSource(ByVal src_obj)
  97. set m_src = src_obj
  98. End Sub
  99. Private Sub SetTarget(ByVal target_obj)
  100. if typename(target_obj) = "String" then
  101. set m_target = eval("new " & target_obj)
  102. else
  103. set m_target = target_obj
  104. end if
  105. End Sub
  106. 'Builds a statement and adds it to the internal statements array
  107. Private Sub AddStatement(ByVal stmt)
  108. redim preserve m_statements(m_statements_count + 1)
  109. m_statements(m_statements_count) = stmt
  110. m_statements_count = m_statements_count + 1
  111. End Sub
  112. Private Function BuildStatement(ByVal field_name)
  113. dim result
  114. if typename(m_src) = "Recordset" or InStr(typename(m_src), "Dictionary") > 0 then
  115. result = "m_target." & field_name & " = m_src(""" & field_name & """)"
  116. else
  117. 'Funky magic...
  118. 'If src.field_name is an object, ensure the set statement is used
  119. if IsObject(eval("m_src." & field_name)) then
  120. result = "set "
  121. else
  122. 'result = "m_target." & field_name & " = m_src." & field_name
  123. end if
  124. result = result & " m_target." & field_name & " = m_src." & field_name
  125. end if
  126. BuildStatement = result
  127. End Function
  128. End Class
  129. Function Automapper()
  130. Set Automapper = new Automapper_Class
  131. End Function
  132. %>

Powered by TurnKey Linux.