AutoMapperObjects.lpr 7.4 KB

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