AutoMappingObjects.dpr 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. public
  80. constructor Create;
  81. destructor Destroy; override;
  82. published
  83. property IdUser : Int64 read fIdUser write fIdUser;
  84. property Money : Integer read fMoney write fMoney;
  85. property Job : TJob read fJob write fJob;
  86. property Car : TCar read fCar write fCar;
  87. property CarList : TCarList read fCarList write fCarList;
  88. property Points : TPointsList read fPoints write fPoints;
  89. property AgentList : TAgentList read fAgentList write fAgentList;
  90. end;
  91. var
  92. User : TUser;
  93. User2 : TUser2;
  94. job : TJob;
  95. AutoMapper : TAutoMapper<TUser,TUser2>;
  96. { TUser }
  97. constructor TUser.Create;
  98. begin
  99. fCar := TCar.Create;
  100. fCarList := TCarList.Create(True);
  101. fPoints := TPointsList.Create;
  102. fAgentList := TAgentList.Create;
  103. end;
  104. destructor TUser.Destroy;
  105. begin
  106. fCar.Free;
  107. fCarList.Free;
  108. fPoints.Free;
  109. fAgentList.Free;
  110. inherited;
  111. end;
  112. { TUser2 }
  113. constructor TUser2.Create;
  114. begin
  115. fCar := TCar.Create;
  116. fCarList := TCarList.Create(True);
  117. fPoints := TPointsList.Create;
  118. fAgentList := TAgentList.Create;
  119. end;
  120. destructor TUser2.Destroy;
  121. begin
  122. fCar.Free;
  123. fCarList.Free;
  124. fPoints.Free;
  125. fAgentList.Free;
  126. inherited;
  127. end;
  128. var
  129. car : TCar;
  130. agent : TAgent;
  131. begin
  132. ReportMemoryLeaksOnShutdown := True;
  133. try
  134. User := TUser.Create;
  135. User.Id := 17;
  136. User.CreationDate := Now();
  137. User.Name := 'John Miller';
  138. User.Age := 30;
  139. User.Numbers := [1,2,3,4,5];
  140. User.Cash := 3500;
  141. job.Name := 'Designer';
  142. job.DateFrom := IncMonth(Now(),-12);
  143. job.DateTo := Now();
  144. User.Job := job;
  145. User.Car.Model := 'Ferrari';
  146. User.Car.CarType := ctOil;
  147. car := TCar.Create;
  148. car.Model := 'Ford';
  149. car.CarType := ctDiesel;
  150. User.CarList.Add(car);
  151. car := TCar.Create;
  152. car.Model := 'Nissan';
  153. car.CarType := ctDiesel;
  154. User.CarList.Add(car);
  155. User.Points.Add(77);
  156. User.Points.Add(100);
  157. User.Points.Add(30);
  158. agent.Name := 'FirstAgent';
  159. agent.Status := TAgentStatus.stIdle;
  160. User.Agent := agent;
  161. User.AgentList.Add(agent);
  162. agent.Name := 'SecondAgent';
  163. agent.Status := TAgentStatus.stFail;
  164. User.AgentList.Add(agent);
  165. //User2 := TMapper<TUser2>.Map(User);
  166. AutoMapper := TAutoMapper<TUser,TUser2>.Create;
  167. try
  168. //option1: you can define auto map different named properties
  169. AutoMapper.CustomMapping.AddMap('Cash','Money');
  170. AutoMapper.CustomMapping.AddMap('Id','IdUser');
  171. //option2: you can decide to modify each property manually or allow to auto someones
  172. AutoMapper.OnDoMapping := procedure(const aSrcObj : TUser; const aTargetName : string; out Value : TFlexValue)
  173. begin
  174. if aTargetName = 'Money' then Value := aSrcObj.Cash * 2
  175. else if aTargetName = 'IdUser' then Value := aSrcObj.Id;
  176. end;
  177. //option3: you can modify some properties after automapping done
  178. AutoMapper.OnAfterMapping := procedure(const aSrcObj : TUser; aTgtObj : TUser2)
  179. begin
  180. aTgtObj.Money := aSrcObj.Cash * 2;
  181. aTgtObj.IdUser := aSrcObj.Id;
  182. end;
  183. User2 := AutoMapper.Map(User);
  184. //User2 := TUser2.Create;
  185. //User.MapTo(User2);
  186. //User2.MapFrom(User);
  187. //User2 := User.Map<TUser2>;
  188. //UserClone := User.Clone as TUser;
  189. //User2 := TUser2(User.Clone);
  190. //User2 := TMapper<TUserBase>.Clone(User) as TUser2;
  191. cout('COMPARE USER VS USER2',etTrace);
  192. cout('User.Id = %d / User2.IdUser = %d',[User.Id,User2.IdUser],etInfo);
  193. cout('User.CreationDate = %s / User2.CreationDate = %s',[DateTimeToStr(User.CreationDate),DateTimetoStr(User2.CreationDate)],etInfo);
  194. cout('User.Name = %s / User2.Name = %s',[User.Name,User2.Name],etInfo);
  195. cout('User.Age = %d / User2.Age = %d',[User.Age,User2.Age],etInfo);
  196. cout('User.Numbers = %d / User2.Numbers = %d',[User.Numbers[1],User2.Numbers[1]],etInfo);
  197. cout('User.Cash = %d / User2.Money = %d',[User.Cash,User2.Money],etInfo);
  198. cout('User.Job.Name = %s / User2.Job.Name = %s',[User.Job.Name,User2.Job.Name],etInfo);
  199. cout('User.Job.DateFrom = %s / User2.Job.DateFrom = %s',[DateTimeToStr(User.Job.DateFrom),DateTimeToStr(User2.Job.DateFrom)],etInfo);
  200. cout('User.Car.Model = %s / User2.Car.Model = %s',[User.Car.Model,User2.Car.Model],etInfo);
  201. cout(' ',etInfo);
  202. cout('USER AS JSON RESULT',etTrace);
  203. cout('%s',[User.ToJson],etInfo);
  204. cout(' ',etInfo);
  205. cout('USER2 AS JSON RESULT',etTrace);
  206. cout('%s',[User2.ToJson],etInfo);
  207. finally
  208. AutoMapper.Free;
  209. User.Free;
  210. User2.Free;
  211. end;
  212. ConsoleWaitForEnterKey;
  213. except
  214. on E: Exception do
  215. Writeln(E.ClassName, ': ', E.Message);
  216. end;
  217. end.