mirror of
https://github.com/danieleteti/delphimvcframework.git
synced 2024-11-16 08:15:53 +01:00
96bbb83209
Better multi thread handling in MVCActiveRecord
191 lines
4.2 KiB
ObjectPascal
191 lines
4.2 KiB
ObjectPascal
unit Entities;
|
|
|
|
interface
|
|
|
|
uses
|
|
MVCFramework.Serializer.Commons,
|
|
MVCFramework.ActiveRecord,
|
|
System.Classes, MVCFramework;
|
|
|
|
type
|
|
|
|
[MVCNameCase(ncLowerCase)]
|
|
[Table('PEOPLE')]
|
|
[EntityActions([eaCreate, eaRetrieve, eaUpdate, eaDelete])]
|
|
TPerson = class(TMVCActiveRecord)
|
|
private
|
|
[PrimaryKey('ID', [foAutoGenerated])]
|
|
fID: Int64;
|
|
[TableField('LAST_NAME')]
|
|
fLastName: string;
|
|
[TableField('FIRST_NAME')]
|
|
fFirstName: string;
|
|
[TableField('DOB')]
|
|
fDOB: TDate;
|
|
[TableField('FULL_NAME')]
|
|
fFullName: string;
|
|
[TableField('IS_MALE')]
|
|
fIsMale: Boolean;
|
|
[TableField('NOTE')]
|
|
fNote: string;
|
|
[TableField('PHOTO')]
|
|
fPhoto: TStream;
|
|
|
|
// transient fields
|
|
fAge: Integer;
|
|
|
|
procedure SetLastName(const Value: string);
|
|
procedure SetID(const Value: Int64);
|
|
procedure SetFirstName(const Value: string);
|
|
procedure SetDOB(const Value: TDate);
|
|
function GetFullName: string;
|
|
procedure SetIsMale(const Value: Boolean);
|
|
procedure SetNote(const Value: string);
|
|
protected
|
|
procedure OnAfterLoad; override;
|
|
procedure OnBeforeInsertOrUpdate; override;
|
|
procedure OnValidation; override;
|
|
procedure OnBeforeInsert; override;
|
|
public
|
|
constructor Create; override;
|
|
destructor Destroy; override;
|
|
property ID: Int64 read fID write SetID;
|
|
property LastName: string read fLastName write SetLastName;
|
|
property FirstName: string read fFirstName write SetFirstName;
|
|
property Age: Integer read fAge;
|
|
property DOB: TDate read fDOB write SetDOB;
|
|
property FullName: string read GetFullName;
|
|
property IsMale: Boolean read fIsMale write SetIsMale;
|
|
property Note: string read fNote write SetNote;
|
|
property Photo: TStream read fPhoto;
|
|
end;
|
|
|
|
[MVCNameCase(ncLowerCase)]
|
|
[Table('ARTICLES')]
|
|
[EntityActions([eaCreate, eaRetrieve, eaUpdate, eaDelete])]
|
|
TArticle = class(TMVCActiveRecord)
|
|
private
|
|
[PrimaryKey('ID', [foAutoGenerated])]
|
|
fID: Int64;
|
|
[TableField('PRICE')]
|
|
FPrice: Currency;
|
|
[TableField('DESCRIPTION')]
|
|
FDescription: string;
|
|
procedure SetID(const Value: Int64);
|
|
procedure SetDescription(const Value: string);
|
|
procedure SetPrice(const Value: Currency);
|
|
public
|
|
property ID: Int64 read fID write SetID;
|
|
property Description: string read FDescription write SetDescription;
|
|
property Price: Currency read FPrice write SetPrice;
|
|
end;
|
|
|
|
|
|
implementation
|
|
|
|
uses
|
|
System.DateUtils,
|
|
System.SysUtils;
|
|
|
|
{ TPersona }
|
|
|
|
constructor TPerson.Create;
|
|
begin
|
|
inherited;
|
|
fPhoto := TMemoryStream.Create;
|
|
end;
|
|
|
|
destructor TPerson.Destroy;
|
|
begin
|
|
fPhoto.Free;
|
|
inherited;
|
|
end;
|
|
|
|
function TPerson.GetFullName: string;
|
|
begin
|
|
Result := fFullName;
|
|
end;
|
|
|
|
procedure TPerson.OnAfterLoad;
|
|
begin
|
|
inherited;
|
|
fAge := Yearsbetween(fDOB, now);
|
|
end;
|
|
|
|
procedure TPerson.OnBeforeInsert;
|
|
begin
|
|
inherited;
|
|
TMemoryStream(fPhoto).LoadFromFile('C:\DEV\dmvcframework\samples\_\customer_small.png');
|
|
end;
|
|
|
|
procedure TPerson.OnBeforeInsertOrUpdate;
|
|
begin
|
|
inherited;
|
|
fLastName := fLastName.ToUpper;
|
|
fFirstName := fFirstName.ToUpper;
|
|
fFullName := fFirstName + ' ' + fLastName;
|
|
end;
|
|
|
|
procedure TPerson.OnValidation;
|
|
begin
|
|
inherited;
|
|
if fLastName.Trim.IsEmpty or fFirstName.Trim.IsEmpty then
|
|
raise EMVCActiveRecord.Create('Validation error. FirstName and LastName are required');
|
|
end;
|
|
|
|
procedure TPerson.SetLastName(const Value: string);
|
|
begin
|
|
fLastName := Value;
|
|
end;
|
|
|
|
procedure TPerson.SetNote(const Value: string);
|
|
begin
|
|
fNote := Value;
|
|
end;
|
|
|
|
procedure TPerson.SetDOB(const Value: TDate);
|
|
begin
|
|
fDOB := Value;
|
|
end;
|
|
|
|
procedure TPerson.SetID(const Value: Int64);
|
|
begin
|
|
fID := Value;
|
|
end;
|
|
|
|
procedure TPerson.SetIsMale(const Value: Boolean);
|
|
begin
|
|
fIsMale := Value;
|
|
end;
|
|
|
|
procedure TPerson.SetFirstName(const Value: string);
|
|
begin
|
|
fFirstName := Value;
|
|
end;
|
|
|
|
{ TArticle }
|
|
|
|
procedure TArticle.SetDescription(const Value: string);
|
|
begin
|
|
FDescription := Value;
|
|
end;
|
|
|
|
procedure TArticle.SetID(const Value: Int64);
|
|
begin
|
|
fID := Value;
|
|
end;
|
|
|
|
procedure TArticle.SetPrice(const Value: Currency);
|
|
begin
|
|
FPrice := Value;
|
|
end;
|
|
|
|
initialization
|
|
|
|
ActiveRecordMappingRegistry.AddEntity('people', TPerson);
|
|
ActiveRecordMappingRegistry.AddEntity('articles', TArticle);
|
|
|
|
finalization
|
|
|
|
end.
|