Advertisement
SirRufo

Untitled

Jun 20th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.06 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   System.SyncObjs,
  7.   System.SysUtils;
  8.  
  9. type
  10.   TCustomerId = type Integer;
  11.  
  12.   TCustomerDTO = class
  13.   private
  14.     FId  : TCustomerId;
  15.     FName: string;
  16.   public
  17.     property Id  : TCustomerId read FId write FId;
  18.     property name: string read FName write FName;
  19.   public
  20.     function ToString( ): string; override;
  21.   end;
  22.  
  23.   ICustomerRepository = interface
  24.     [ '{9B0E352B-F236-47F7-A817-A3CCB83A5DA8}' ]
  25.     function Get( const Id: TCustomerId ): TCustomerDTO;
  26.     function Post( const Customer: TCustomerDTO ): TCustomerDTO;
  27.     procedure Delete( const Id: TCustomerId );
  28.   end;
  29.  
  30.   TCustomerModel = class
  31.   private
  32.     FRepository: ICustomerRepository;
  33.     FData      : TCustomerDTO;
  34.     function GetId: TCustomerId;
  35.     procedure SetId( const Value: TCustomerId );
  36.     function GetName: string;
  37.     procedure SetName( const Value: string );
  38.   public
  39.     property Id  : TCustomerId read GetId write SetId;
  40.     property name: string read GetName write SetName;
  41.   public
  42.     // DI via Container
  43.     constructor Create( ); overload;
  44.     // DI via constructor
  45.     constructor Create( const Repository: ICustomerRepository ); overload;
  46.     destructor Destroy; override;
  47.  
  48.     procedure Delete( );
  49.     procedure Fetch( );
  50.     procedure Save( );
  51.  
  52.     function ToString( ): string; override;
  53.   end;
  54.  
  55.   // Very Simple DI Container
  56.  
  57.   Repositories = class
  58.   private
  59.     class var FCustomerRepository: TFunc<ICustomerRepository>;
  60.   public
  61.     class property CustomerRepository: TFunc<ICustomerRepository> read FCustomerRepository write FCustomerRepository;
  62.   end;
  63.  
  64. implementation
  65.  
  66. { TCustomerModel }
  67.  
  68. constructor TCustomerModel.Create( const Repository: ICustomerRepository );
  69. begin
  70.   inherited Create( );
  71.  
  72.   FRepository := Repository;
  73.   FData       := TCustomerDTO.Create;
  74. end;
  75.  
  76. constructor TCustomerModel.Create;
  77. begin
  78.   // Get the Repo from Container
  79.   Create( Repositories.CustomerRepository( ) );
  80. end;
  81.  
  82. procedure TCustomerModel.Delete;
  83. begin
  84.   FRepository.Delete( Id );
  85. end;
  86.  
  87. destructor TCustomerModel.Destroy;
  88. begin
  89.   FData.Free;
  90.   inherited;
  91. end;
  92.  
  93. procedure TCustomerModel.Fetch;
  94. var
  95.   lData: TCustomerDTO;
  96. begin
  97.   lData := FRepository.Get( Id );
  98.   try
  99.     lData := TInterlocked.Exchange( FData, lData );
  100.   finally
  101.     lData.Free;
  102.   end;
  103. end;
  104.  
  105. function TCustomerModel.GetId: TCustomerId;
  106. begin
  107.   Result := FData.Id;
  108. end;
  109.  
  110. function TCustomerModel.GetName: string;
  111. begin
  112.   Result := FData.Name;
  113. end;
  114.  
  115. procedure TCustomerModel.Save;
  116. var
  117.   lData: TCustomerDTO;
  118. begin
  119.   lData := FRepository.Post( FData );
  120.   try
  121.     lData := TInterlocked.Exchange( FData, lData );
  122.   finally
  123.     lData.Free;
  124.   end;
  125. end;
  126.  
  127. procedure TCustomerModel.SetId( const Value: TCustomerId );
  128. begin
  129.   FData.Id := Value;
  130. end;
  131.  
  132. procedure TCustomerModel.SetName( const Value: string );
  133. begin
  134.   FData.Name := Value;
  135. end;
  136.  
  137. function TCustomerModel.ToString: string;
  138. begin
  139.   Result := FData.ToString( );
  140. end;
  141.  
  142. { TCustomerDTO }
  143.  
  144. function TCustomerDTO.ToString: string;
  145. begin
  146.   Result := Format( '{"id":%d, "name":"%s"}', [ Id, name ] );
  147. end;
  148.  
  149. end.
  150.  
  151. {**************************}
  152.  
  153. unit Unit2;
  154.  
  155. interface
  156.  
  157. uses
  158.   Unit1, System.SysUtils;
  159.  
  160. type
  161.   TFakeCustomerRepository = class( TInterfacedObject, ICustomerRepository )
  162.   private { ICustomerRepository }
  163.     procedure Delete( const Id: TCustomerId );
  164.     function Get( const Id: TCustomerId ): TCustomerDTO;
  165.     function Post( const Customer: TCustomerDTO ): TCustomerDTO;
  166.   end;
  167.  
  168. implementation
  169.  
  170. { TFakeCustomerRepository }
  171.  
  172. procedure TFakeCustomerRepository.Delete( const Id: TCustomerId );
  173. begin
  174.   WriteLn( Format( '>>> Delete(%d)', [ Id ] ) );
  175. end;
  176.  
  177. function TFakeCustomerRepository.Get( const Id: TCustomerId ): TCustomerDTO;
  178. begin
  179.   WriteLn( Format( '>>> Get(%d)', [ Id ] ) );
  180.   // return a faked customer
  181.   Result      := TCustomerDTO.Create( );
  182.   Result.Id   := Id;
  183.   Result.name := 'SomeCustomer';
  184. end;
  185.  
  186. function TFakeCustomerRepository.Post( const Customer: TCustomerDTO ): TCustomerDTO;
  187. begin
  188.   WriteLn( Format( '>>> Post(%s)', [ Customer.ToString() ] ) );
  189.   // Copy then Customer
  190.   Result      := TCustomerDTO.Create( );
  191.   Result.Id   := Customer.Id;
  192.   Result.name := Customer.name;
  193. end;
  194.  
  195. end.
  196.  
  197. {**************************}
  198.  
  199. program Project3;
  200.  
  201. {$APPTYPE CONSOLE}
  202. {$R *.res}
  203.  
  204. uses
  205.   System.SysUtils,
  206.   Unit1 in 'Unit1.pas',
  207.   Unit2 in 'Unit2.pas';
  208.  
  209. procedure Main( );
  210. var
  211.   lCustomer: TCustomerModel;
  212. begin
  213.   lCustomer := TCustomerModel.Create( );
  214.   try
  215.  
  216.     lCustomer.Id := 42;
  217.     lCustomer.Fetch( );
  218.  
  219.     WriteLn( lCustomer.ToString( ) );
  220.  
  221.     lCustomer.Name := 'FooBar';
  222.  
  223.     WriteLn( lCustomer.ToString( ) );
  224.  
  225.     lCustomer.Save( );
  226.  
  227.   finally
  228.     lCustomer.Free;
  229.   end;
  230. end;
  231.  
  232. begin
  233.   try
  234.     // Setup the DI Container
  235.     Repositories.CustomerRepository :=
  236.       function: ICustomerRepository
  237.       begin
  238.         Result := TFakeCustomerRepository.Create( );
  239.       end;
  240.  
  241.     Main( );
  242.  
  243.   except
  244.     on E: Exception do
  245.       WriteLn( E.ClassName, ': ', E.Message );
  246.   end;
  247.   ReadLn;
  248.  
  249. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement