Skip to content

Commit 9779883

Browse files
committed
add: custom repository for a sample
1 parent 605e8e7 commit 9779883

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed

samples/repository_showcase/MainFormU.pas

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -345,36 +345,33 @@ procedure TMainForm.btnNamedQueriesClick(Sender: TObject);
345345

346346
procedure TMainForm.btnCustomMethodsClick(Sender: TObject);
347347
var
348-
lRepo: TCustomerRepository;
348+
lRepo: ICustomerRepository;
349349
lCustomers: TObjectList<TCustomer>;
350350
lCustomer: TCustomer;
351351
begin
352352
Log('');
353353
Log('** Custom Repository Methods');
354354

355355
lRepo := TCustomerRepository.Create;
356+
357+
// Custom method: GetCustomersByCity
358+
lCustomers := lRepo.GetCustomersByCity('London');
356359
try
357-
// Custom method: GetCustomersByCity
358-
lCustomers := lRepo.GetCustomersByCity('London');
359-
try
360-
Log(Format('GetCustomersByCity("London") returned %d customers', [lCustomers.Count]));
361-
for lCustomer in lCustomers do
362-
Log(' ' + lCustomer.ToString);
363-
finally
364-
lCustomers.Free;
365-
end;
360+
Log(Format('GetCustomersByCity("London") returned %d customers', [lCustomers.Count]));
361+
for lCustomer in lCustomers do
362+
Log(' ' + lCustomer.ToString);
363+
finally
364+
lCustomers.Free;
365+
end;
366366

367-
// Custom method: GetTopRatedCustomers
368-
lCustomers := lRepo.GetTopRatedCustomers;
369-
try
370-
Log(Format('GetTopRatedCustomers returned %d customers', [lCustomers.Count]));
371-
for lCustomer in lCustomers do
372-
Log(' ' + lCustomer.ToString);
373-
finally
374-
lCustomers.Free;
375-
end;
367+
// Custom method: GetTopRatedCustomers
368+
lCustomers := lRepo.GetTopRatedCustomers;
369+
try
370+
Log(Format('GetTopRatedCustomers returned %d customers', [lCustomers.Count]));
371+
for lCustomer in lCustomers do
372+
Log(' ' + lCustomer.ToString);
376373
finally
377-
lRepo.Free;
374+
lCustomers.Free;
378375
end;
379376
end;
380377

samples/repository_showcase/RepositoriesU.pas

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,14 @@ interface
3636
/// Customer Repository - uses default implementation
3737
/// Can be injected as IMVCRepository<TCustomer> in controllers
3838
/// </summary>
39-
TCustomerRepository = class(TMVCRepository<TCustomer>)
39+
40+
ICustomerRepository = interface(IMVCRepository<TCustomer>)
41+
['{969BC9CE-E30E-4104-8F74-CE2A7C18AD21}']
42+
function GetCustomersByCity(const City: string): TObjectList<TCustomer>;
43+
function GetTopRatedCustomers: TObjectList<TCustomer>;
44+
end;
45+
46+
TCustomerRepository = class(TMVCRepository<TCustomer>, ICustomerRepository)
4047
public
4148
// Add custom methods here if needed
4249
function GetCustomersByCity(const City: string): TObjectList<TCustomer>;

samples/simple_api_using_repository_with_injection/CustomersControllerU.pas

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ function TCustomersController.CreateCustomer(const Customer: TCustomer; Customer
6767
end;
6868

6969
function TCustomersController.DeleteCustomerByID(const ID: Integer;
70-
CustomersRepository: IMVCRepository<TCustomer>): IMVCResponse;
70+
CustomersRepository: IMVCRepository<TCustomer>): IMVCResponse;
7171
begin
72+
//Automatic transaction handling
7273
var lTx := TMVCRepository.UseTransactionContext;
7374
var lCustomer := CustomersRepository.GetByPK(ID);
7475
CustomersRepository.Delete(lCustomer);

0 commit comments

Comments
 (0)