2
0

AutoMappingObjects.dpr 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. program AutoMappingObjects;
  2. {$APPTYPE CONSOLE}
  3. {$R *.res}
  4. uses
  5. System.SysUtils,
  6. System.Generics.Collections,
  7. Quick.Commons,
  8. Quick.Console,
  9. Quick.JSONRecord,
  10. Quick.Json.Serializer,
  11. Quick.AutoMapper;
  12. type
  13. TJob = record
  14. Name : string;
  15. DateFrom : TDateTime;
  16. DateTo : TDateTime;
  17. end;
  18. TCarType = (ctOil, ctDiesel);
  19. TAgentStatus = (stActive, stIdle, stFail);
  20. TCar = class
  21. private
  22. fModel : string;
  23. fCarType : TCarType;
  24. published
  25. property Model : string read fModel write fModel;
  26. property CarType : TCarType read fCarType write fCarType;
  27. end;
  28. TCarList = TObjectList<TCar>;
  29. TAgent = record
  30. Name : string;
  31. Status : TAgentStatus;
  32. end;
  33. TAgentList = TList<TAgent>;
  34. TUserBase = class(TJsonRecord)
  35. private
  36. fName : string;
  37. fAge : Integer;
  38. fCreationDate : TDateTime;
  39. fNumbers : TArray<Integer>;
  40. fAgent : TAgent;
  41. published
  42. property Name : string read fName write fName;
  43. property Age : Integer read fAge write fAge;
  44. property CreationDate : TDateTime read fCreationDate write fCreationDate;
  45. property Numbers : TArray<Integer> read fNumbers write fNumbers;
  46. property Agent : TAgent read fAgent write fAgent;
  47. end;
  48. TPointsList = TList<Integer>;
  49. TUser = class(TUserBase)
  50. private
  51. fId : Int64;
  52. fCash : Integer;
  53. fJob : TJob;
  54. fCar : TCar;
  55. fCarList : TCarList;
  56. fPoints : TPointsList;
  57. fAgentList : TAgentList;
  58. public
  59. constructor Create;
  60. destructor Destroy; override;
  61. published
  62. property Id : Int64 read fId write fId;
  63. property Cash : Integer read fCash write fCash;
  64. property Job : TJob read fJob write fJob;
  65. property Car : TCar read fCar write fCar;
  66. property CarList : TCarList read fCarList write fCarList;
  67. property Points : TPointsList read fPoints write fPoints;
  68. property AgentList : TAgentList read fAgentList write fAgentList;
  69. end;
  70. TUser2 = class(TUserBase)
  71. private
  72. fIdUser : Int64;
  73. fJob : TJob;
  74. fMoney : Integer;
  75. fCar : TCar;
  76. fCarList : TCarList;
  77. fPoints : TPointsList;
  78. fAgentList : TAgentList;
  79. fJobName : string;
  80. public
  81. constructor Create;
  82. destructor Destroy; override;
  83. published
  84. property IdUser : Int64 read fIdUser write fIdUser;
  85. property Money : Integer read fMoney write fMoney;
  86. property Job : TJob read fJob write fJob;
  87. property Car : TCar read fCar write fCar;
  88. property CarList : TCarList read fCarList write fCarList;
  89. property Points : TPointsList read fPoints write fPoints;
  90. property AgentList : TAgentList read fAgentList write fAgentList;
  91. property JobName : string read fJobName write fJobName;
  92. end;
  93. var
  94. User : TUser;
  95. User2 : TUser2;
  96. job : TJob;
  97. AutoMapper : TAutoMapper<TUser,TUser2>;
  98. { TUser }
  99. constructor TUser.Create;
  100. begin
  101. fCar := TCar.Create;
  102. fCarList := TCarList.Create(True);
  103. fPoints := TPointsList.Create;
  104. fAgentList := TAgentList.Create;
  105. end;
  106. destructor TUser.Destroy;
  107. begin
  108. fCar.Free;
  109. fCarList.Free;
  110. fPoints.Free;
  111. fAgentList.Free;
  112. inherited;
  113. end;
  114. { TUser2 }
  115. constructor TUser2.Create;
  116. begin
  117. fCar := TCar.Create;
  118. fCarList := TCarList.Create(True);
  119. fPoints := TPointsList.Create;
  120. fAgentList := TAgentList.Create;
  121. end;
  122. destructor TUser2.Destroy;
  123. begin
  124. fCar.Free;
  125. fCarList.Free;
  126. fPoints.Free;
  127. fAgentList.Free;
  128. inherited;
  129. end;
  130. var
  131. car : TCar;
  132. agent : TAgent;
  133. begin
  134. ReportMemoryLeaksOnShutdown := True;
  135. try
  136. User := TUser.Create;
  137. User.Id := 17;
  138. User.CreationDate := Now();
  139. User.Name := 'John Miller';
  140. User.Age := 30;
  141. User.Numbers := [1,2,3,4,5];
  142. User.Cash := 3500;
  143. job.Name := 'Designer';
  144. job.DateFrom := IncMonth(Now(),-12);
  145. job.DateTo := Now();
  146. User.Job := job;
  147. User.Car.Model := 'Ferrari';
  148. User.Car.CarType := ctOil;
  149. car := TCar.Create;
  150. car.Model := 'Ford';
  151. car.CarType := ctDiesel;
  152. User.CarList.Add(car);
  153. car := TCar.Create;
  154. car.Model := 'Nissan';
  155. car.CarType := ctDiesel;
  156. User.CarList.Add(car);
  157. User.Points.Add(77);
  158. User.Points.Add(100);
  159. User.Points.Add(30);
  160. agent.Name := 'FirstAgent';
  161. agent.Status := TAgentStatus.stIdle;
  162. User.Agent := agent;
  163. User.AgentList.Add(agent);
  164. agent.Name := 'SecondAgent';
  165. agent.Status := TAgentStatus.stFail;
  166. User.AgentList.Add(agent);
  167. //User2 := TMapper<TUser2>.Map(User);
  168. AutoMapper := TAutoMapper<TUser,TUser2>.Create;
  169. try
  170. //option1: you can define auto map different named properties (if OnDoMapping assigned customaping will be no effect)
  171. AutoMapper.CustomMapping.AddMap('Cash','Money');
  172. AutoMapper.CustomMapping.AddMap('Id','IdUser');
  173. AutoMapper.CustomMapping.AddMap('Job.Name','JobName');
  174. //option2: you can decide to modify each property manually or allow to auto someones
  175. AutoMapper.OnDoMapping := procedure(const aSrcObj : TUser; const aTargetName : string; out Value : TFlexValue)
  176. begin
  177. if aTargetName = 'Money' then Value := aSrcObj.Cash * 2
  178. else if aTargetName = 'IdUser' then Value := aSrcObj.Id;
  179. end;
  180. //option3: you can modify some properties after automapping done
  181. AutoMapper.OnAfterMapping := procedure(const aSrcObj : TUser; aTgtObj : TUser2)
  182. begin
  183. aTgtObj.Money := aSrcObj.Cash * 2;
  184. aTgtObj.IdUser := aSrcObj.Id;
  185. end;
  186. User2 := AutoMapper.Map(User);
  187. //User2 := TUser2.Create;
  188. //User.MapTo(User2);
  189. //User2.MapFrom(User);
  190. //User2 := User.Map<TUser2>;
  191. //UserClone := User.Clone as TUser;
  192. //User2 := TUser2(User.Clone);
  193. //User2 := TMapper<TUserBase>.Clone(User) as TUser2;
  194. cout('COMPARE USER VS USER2',etTrace);
  195. cout('User.Id = %d / User2.IdUser = %d',[User.Id,User2.IdUser],etInfo);
  196. cout('User.CreationDate = %s / User2.CreationDate = %s',[DateTimeToStr(User.CreationDate),DateTimetoStr(User2.CreationDate)],etInfo);
  197. cout('User.Name = %s / User2.Name = %s',[User.Name,User2.Name],etInfo);
  198. cout('User.Age = %d / User2.Age = %d',[User.Age,User2.Age],etInfo);
  199. cout('User.Numbers = %d / User2.Numbers = %d',[User.Numbers[1],User2.Numbers[1]],etInfo);
  200. cout('User.Cash = %d / User2.Money = %d',[User.Cash,User2.Money],etInfo);
  201. cout('User.Job.Name = %s / User2.Job.Name = %s',[User.Job.Name,User2.Job.Name],etInfo);
  202. cout('User.Job.DateFrom = %s / User2.Job.DateFrom = %s',[DateTimeToStr(User.Job.DateFrom),DateTimeToStr(User2.Job.DateFrom)],etInfo);
  203. cout('User.Car.Model = %s / User2.Car.Model = %s',[User.Car.Model,User2.Car.Model],etInfo);
  204. cout(' ',etInfo);
  205. cout('USER AS JSON RESULT',etTrace);
  206. cout('%s',[User.ToJson],etInfo);
  207. cout(' ',etInfo);
  208. cout('USER2 AS JSON RESULT',etTrace);
  209. cout('%s',[User2.ToJson],etInfo);
  210. finally
  211. AutoMapper.Free;
  212. User.Free;
  213. User2.Free;
  214. end;
  215. ConsoleWaitForEnterKey;
  216. except
  217. on E: Exception do
  218. Writeln(E.ClassName, ': ', E.Message);
  219. end;
  220. end.