delphimvcframework/samples/commons/BusinessObjectsU.pas

456 lines
11 KiB
ObjectPascal
Raw Normal View History

// ***************************************************************************
//
// Delphi MVC Framework
//
// Copyright (c) 2010-2020 Daniele Teti and the DMVCFramework Team
//
// https://github.com/danieleteti/delphimvcframework
//
// ***************************************************************************
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// *************************************************************************** }
2013-11-11 01:11:09 +01:00
unit BusinessObjectsU;
interface
uses
MVCFramework.Serializer.Commons,
Generics.Collections, Vcl.Graphics, JsonDataObjects;
2013-11-11 01:11:09 +01:00
type
2014-04-01 00:02:31 +02:00
2017-03-29 14:49:35 +02:00
[MVCNameCase(ncLowerCase)]
2013-11-11 01:11:09 +01:00
TPerson = class
private
2014-05-22 12:48:44 +02:00
FLastName: string;
2013-11-11 01:11:09 +01:00
FDOB: TDate;
2014-05-22 12:48:44 +02:00
FFirstName: string;
2013-11-11 01:11:09 +01:00
FMarried: boolean;
2019-03-10 16:29:18 +01:00
fID: Int64;
2013-11-11 01:11:09 +01:00
procedure SetDOB(const Value: TDate);
2014-05-22 12:48:44 +02:00
procedure SetFirstName(const Value: string);
procedure SetLastName(const Value: string);
2013-11-11 01:11:09 +01:00
procedure SetMarried(const Value: boolean);
2019-05-08 09:10:42 +02:00
function GetFullName: String;
2013-11-11 01:11:09 +01:00
public
function Equals(Obj: TObject): boolean; override;
2019-03-10 16:29:18 +01:00
property ID: Int64 read fID write fID;
2014-05-22 12:48:44 +02:00
property FirstName: string read FFirstName write SetFirstName;
property LastName: string read FLastName write SetLastName;
2019-05-08 09:10:42 +02:00
property FullName: String read GetFullName;
2013-11-11 01:11:09 +01:00
property DOB: TDate read FDOB write SetDOB;
property Married: boolean read FMarried write SetMarried;
2019-03-10 16:29:18 +01:00
constructor Create; virtual;
2015-04-01 17:01:23 +02:00
class function GetNew(AFirstName, ALastName: string; ADOB: TDate; AMarried: boolean): TPerson;
class function GetList(const aCount: Integer = 3): TObjectList<TPerson>;
2013-11-11 01:11:09 +01:00
end;
IPerson = interface
['{1D00C67A-A6D9-4B31-8291-705B339CDE9B}']
function GetName: String;
procedure SetName(const Value: String);
function GetAge: Integer;
procedure SetAge(const Value: Integer);
function GetDOB: TDate;
procedure SetDOB(const Value: TDate);
property Name: String read GetName write SetName;
property Age: Integer read GetAge write SetAge;
property DOB: TDate read GetDOB write SetDOB;
end;
TObjectWithJSONObject = class
private
fJSONObject: TJSONObject;
FStringProp: String;
procedure SetStringProp(const Value: String);
public
constructor Create;
destructor Destroy; override;
property StringProp: String read FStringProp write SetStringProp;
property JSONObject: TJSONObject read fJSONObject;
end;
[MVCNameCase(ncCamelCase)]
TInterfacedPerson = class(TInterfacedObject, IPerson)
private
fName: string;
FDOB: TDate;
fAge: Integer;
protected
function GetName: String;
procedure SetName(const Value: String);
function GetAge: Integer;
procedure SetAge(const Value: Integer);
function GetDOB: TDate;
procedure SetDOB(const Value: TDate);
public
property Name: String read GetName write SetName;
property Age: Integer read GetAge write SetAge;
property DOB: TDate read GetDOB write SetDOB;
end;
TPeople = class(TObjectList<TPerson>);
[MVCNameCase(ncLowerCase)]
TMetadata = class
private
FCustomData: string;
FStopProcessing: TDateTime;
FStartProcessing: TDateTime;
procedure SetCustomData(const Value: string);
procedure SetStartProcessing(const Value: TDateTime);
procedure SetStopProcessing(const Value: TDateTime);
public
property StartProcessing: TDateTime read FStartProcessing write SetStartProcessing;
property StopProcessing: TDateTime read FStopProcessing write SetStopProcessing;
property CustomData: string read FCustomData write SetCustomData;
end;
[MVCNameCase(ncLowerCase)]
TPeopleWithMetadata = class(TObject)
private
FItems: TPeople;
FMetadata: TMetadata;
public
constructor Create;
destructor Destroy; override;
property Items: TPeople read FItems;
property Metadata: TMetadata read FMetadata;
end;
2017-03-29 14:49:35 +02:00
[MVCNameCase(ncLowerCase)]
2013-11-11 01:11:09 +01:00
TCustomer = class
private
fName: string;
2014-05-22 12:48:44 +02:00
FAddressLine2: string;
FAddressLine1: string;
FContactFirst: string;
FCity: string;
FContactLast: string;
fLogo: TBitmap;
2014-05-22 12:48:44 +02:00
procedure SetAddressLine1(const Value: string);
procedure SetAddressLine2(const Value: string);
procedure SetCity(const Value: string);
procedure SetContactFirst(const Value: string);
procedure SetContactLast(const Value: string);
procedure SetName(const Value: string);
procedure SetLogo(const Value: TBitmap);
2013-11-11 01:11:09 +01:00
public
constructor Create;
destructor Destroy; override;
property Name: string read fName write SetName;
2017-03-20 21:42:28 +01:00
[MVCDoNotSerialize]
2014-05-22 12:48:44 +02:00
property ContactFirst: string read FContactFirst write SetContactFirst;
2017-03-20 21:42:28 +01:00
[MVCDoNotSerialize]
2014-05-22 12:48:44 +02:00
property ContactLast: string read FContactLast write SetContactLast;
property AddressLine1: string read FAddressLine1 write SetAddressLine1;
property AddressLine2: string read FAddressLine2 write SetAddressLine2;
property City: string read FCity write SetCity;
property Logo: TBitmap read fLogo write SetLogo;
2015-04-01 17:01:23 +02:00
class function GetList: TObjectList<TCustomer>;
2013-11-11 01:11:09 +01:00
end;
2017-03-29 14:49:35 +02:00
[MVCNameCase(ncLowerCase)]
2014-05-30 11:29:58 +02:00
TProgrammer = class(TPerson)
private
FSkills: string;
procedure SetSkills(const Value: string);
public
property Skills: string read FSkills write SetSkills;
end;
2017-03-29 14:49:35 +02:00
[MVCNameCase(ncLowerCase)]
2014-05-30 11:29:58 +02:00
TPhilosopher = class(TPerson)
private
FMentors: string;
procedure SetMentors(const Value: string);
2014-05-30 11:29:58 +02:00
public
property Mentors: string read FMentors write SetMentors;
2014-05-30 11:29:58 +02:00
end;
2013-11-11 01:11:09 +01:00
implementation
uses
System.SysUtils,
RandomUtilsU;
2013-11-11 01:11:09 +01:00
{ TPerson }
2019-03-10 16:29:18 +01:00
constructor TPerson.Create;
begin
inherited Create;
fID := 1000 + Random(1000);
end;
function TPerson.Equals(Obj: TObject): boolean;
begin
Result := Obj is TPerson;
if Result then
begin
Result := Result and (TPerson(Obj).LastName = Self.LastName);
Result := Result and (TPerson(Obj).FirstName = Self.FirstName);
Result := Result and (TPerson(Obj).Married = Self.Married);
Result := Result and (TPerson(Obj).DOB = Self.DOB);
end;
end;
2019-05-08 09:10:42 +02:00
function TPerson.GetFullName: String;
begin
Result := Format('%s, %s', [FFirstName, FLastName]);
end;
class function TPerson.GetList(const aCount: Integer): TObjectList<TPerson>;
var
I: Integer;
begin
if aCount = 3 then
begin // retrocompatibility
Result := TObjectList<TPerson>.Create(true);
Result.Add(TPerson.GetNew('Tony', 'Stark', EncodeDate(1965, 5, 15), true));
2019-03-10 16:29:18 +01:00
Result.Add(TPerson.GetNew('Steve', 'Rogers', 0, true));
Result.Add(TPerson.GetNew('Bruce', 'Banner', 0, true));
end
else
begin
Result := TObjectList<TPerson>.Create(true);
for I := 1 to aCount do
begin
Result.Add(TPerson.GetNew(GetRndFirstName, GetRndLastName, EncodeDate(1900 + Random(100), Random(12) + 1,
Random(27) + 1), true));
end;
end;
end;
class function TPerson.GetNew(AFirstName, ALastName: string; ADOB: TDate; AMarried: boolean): TPerson;
begin
Result := TPerson.Create;
Result.FLastName := ALastName;
Result.FFirstName := AFirstName;
Result.FDOB := ADOB;
Result.FMarried := AMarried;
end;
2013-11-11 01:11:09 +01:00
procedure TPerson.SetDOB(const Value: TDate);
begin
FDOB := Value;
end;
2014-05-22 12:48:44 +02:00
procedure TPerson.SetFirstName(const Value: string);
2013-11-11 01:11:09 +01:00
begin
FFirstName := Value;
end;
2014-05-22 12:48:44 +02:00
procedure TPerson.SetLastName(const Value: string);
2013-11-11 01:11:09 +01:00
begin
FLastName := Value;
end;
procedure TPerson.SetMarried(const Value: boolean);
begin
FMarried := Value;
end;
{ TCustomer }
constructor TCustomer.Create;
begin
inherited;
fLogo := TBitmap.Create;
end;
destructor TCustomer.Destroy;
begin
fLogo.Free;
inherited;
end;
2015-04-01 17:01:23 +02:00
class function TCustomer.GetList: TObjectList<TCustomer>;
var
C1: TCustomer;
I: Integer;
2015-04-01 17:01:23 +02:00
begin
Result := TObjectList<TCustomer>.Create(true);
for I := 1 to 1000 do
begin
C1 := TCustomer.Create;
C1.name := I.ToString + ': bit Time Professionals';
C1.ContactFirst := 'Daniele';
C1.ContactLast := 'Teti';
C1.AddressLine1 := 'Via di Valle Morta 10';
C1.City := 'Rome, IT';
Result.Add(C1);
C1 := TCustomer.Create;
C1.name := I.ToString + ': Stark Industries';
C1.ContactFirst := 'Tony';
C1.ContactLast := 'Stark';
C1.AddressLine1 := 'Superhero Street 555';
C1.City := 'Palo Alto, CA';
Result.Add(C1);
C1 := TCustomer.Create;
C1.name := I.ToString + ': Google Inc';
C1.ContactFirst := 'Larry';
C1.ContactLast := 'Page';
C1.AddressLine1 := '';
C1.City := 'Mountain View, CA';
Result.Add(C1);
end;
2015-04-01 17:01:23 +02:00
end;
2014-05-22 12:48:44 +02:00
procedure TCustomer.SetAddressLine1(const Value: string);
2013-11-11 01:11:09 +01:00
begin
FAddressLine1 := Value;
end;
2014-05-22 12:48:44 +02:00
procedure TCustomer.SetAddressLine2(const Value: string);
2013-11-11 01:11:09 +01:00
begin
FAddressLine2 := Value;
end;
2014-05-22 12:48:44 +02:00
procedure TCustomer.SetCity(const Value: string);
2013-11-11 01:11:09 +01:00
begin
FCity := Value;
end;
2014-05-22 12:48:44 +02:00
procedure TCustomer.SetContactFirst(const Value: string);
2013-11-11 01:11:09 +01:00
begin
FContactFirst := Value;
end;
2014-05-22 12:48:44 +02:00
procedure TCustomer.SetContactLast(const Value: string);
2013-11-11 01:11:09 +01:00
begin
FContactLast := Value;
end;
procedure TCustomer.SetLogo(const Value: TBitmap);
begin
fLogo := Value;
end;
2014-05-22 12:48:44 +02:00
procedure TCustomer.SetName(const Value: string);
2013-11-11 01:11:09 +01:00
begin
fName := Value;
2013-11-11 01:11:09 +01:00
end;
2014-05-30 11:29:58 +02:00
{ TProgrammer }
procedure TProgrammer.SetSkills(const Value: string);
begin
FSkills := Value;
end;
{ TPhilosopher }
procedure TPhilosopher.SetMentors(const Value: string);
2014-05-30 11:29:58 +02:00
begin
FMentors := Value;
end;
{ TMetadata }
procedure TMetadata.SetCustomData(const Value: string);
begin
FCustomData := Value;
end;
procedure TMetadata.SetStartProcessing(const Value: TDateTime);
begin
FStartProcessing := Value;
end;
procedure TMetadata.SetStopProcessing(const Value: TDateTime);
begin
FStopProcessing := Value;
end;
{ TPeopleWithMetadata }
constructor TPeopleWithMetadata.Create;
begin
inherited;
FMetadata := TMetadata.Create;
FItems := TPeople.Create(true);
end;
destructor TPeopleWithMetadata.Destroy;
begin
FMetadata.Free;
FItems.Free;
inherited;
end;
{ TInterfacedPerson }
function TInterfacedPerson.GetAge: Integer;
begin
Result := fAge;
end;
function TInterfacedPerson.GetDOB: TDate;
begin
Result := FDOB;
end;
function TInterfacedPerson.GetName: String;
begin
Result := fName;
end;
procedure TInterfacedPerson.SetAge(const Value: Integer);
begin
fAge := Value;
end;
procedure TInterfacedPerson.SetDOB(const Value: TDate);
begin
FDOB := Value;
end;
procedure TInterfacedPerson.SetName(const Value: String);
begin
fName := Value;
end;
{ TObjectWithJSONObject }
constructor TObjectWithJSONObject.Create;
begin
inherited;
fJSONObject := TJsonObject.Create;
end;
destructor TObjectWithJSONObject.Destroy;
begin
fJSONObject.Free;
inherited;
end;
procedure TObjectWithJSONObject.SetStringProp(const Value: String);
begin
FStringProp := Value;
end;
2019-03-10 16:29:18 +01:00
initialization
Randomize;
2013-11-11 01:11:09 +01:00
end.