Advertisement
Guest User

Untitled

a guest
Feb 6th, 2015
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.31 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Vcl.Forms, Vcl.Dialogs, System.Rtti, System.Classes, Vcl.Controls,
  7.   Vcl.StdCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Button1: TButton;
  12.     procedure Button1Click(Sender: TObject);
  13.   end;
  14.  
  15.   TSession = class sealed(TObject)
  16.   public
  17.     function Load<TRecord: class, constructor>(const AId: TValue): TRecord;
  18.   end;
  19.  
  20.   UUId = ShortString;
  21.   RawString = string;
  22.  
  23.   TPerson = class(TObject)
  24.   private
  25.     FName: RawString;
  26.     FId: UUId;
  27.   public
  28.     property Id: UUId read FId write FId;
  29.     property Name: RawString read FName write FName;
  30.   end;
  31.  
  32. var
  33.   Form1: TForm1;
  34.  
  35. implementation
  36.  
  37. {$R *.dfm}
  38.  
  39. { TSession }
  40.  
  41. function TSession.Load<TRecord>(const AId: TValue): TRecord;
  42. { Mock }
  43. var
  44.   VPerson: TPerson;
  45. begin
  46.   { Mock }
  47.   VPerson := TPerson.Create;
  48.   VPerson.Id := 'B62DDA747C4E490A89B53EDE2B973138';
  49.   VPerson.Name := 'Test OK';
  50.  
  51.   Result := TRecord(VPerson);
  52. end;
  53.  
  54. { TForm1 }
  55.  
  56. procedure TForm1.Button1Click(Sender: TObject);
  57. var
  58.   VPerson: TPerson;
  59.   VSession: TSession;
  60. begin
  61.   VSession := TSession.Create;
  62.   VPerson := nil;
  63.   try
  64.     VPerson := VSession.Load<TPerson>('B62DDA747C4E490A89B53EDE2B973138');
  65.     ShowMessageFmt('ID: %s, Name: %s', [VPerson.Id, VPerson.Name]);
  66.   finally
  67.     VPerson.Free;
  68.     VSession.Free;
  69.   end;
  70. end;
  71.  
  72. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement