ManagerORM
ORM Object Relational Mapping Manager is an unit that map FDQuery to object<T>
- Quiet · last change 9-11-2020
- ★ 8 on GitHub
README
ManagerORM
ORM Object Relational Mapping Manager. This is an unit that map FDQuery to object<T>
Requirements Recently Delphi version Support for Generics Support for rtti Suport for Firedac
Usage
- Declare a class that represents your database entities (Model)
TCar = class
Brand: String;
Year: Tdatetime;
Power: Integer;
end;- Use TManager to bring data from database to object
var
Car: TCar;
Manager: TManager<TCar>;
-- FdQuery1: TFdQuery; Put a FdQuery into Datamodule and configure a FdConnection
begin
Car := TCar.Create;
Manager := TManager<TCar>.Create(Car, FdQuery1);
Manager.Open(); //it performs 'select brand, year, power from car' automatically
while Manager.Iterate do
ListBox1.add(format('%s - %d hp', [Car.Brand, Car.Power]), Car);
end;$? #output
> Maserati Levante - 350 hp
> Alfa Romeo Giulia 952 - 200 hp3.Create a new car and save to database
var
Car: TCar;
Manager: TManager<TCar>;
begin
Car := TCar.Create;
Car.Brand := 'Dodge Viper II';
Car.Year := 2007;
Car.Power := 608;
Manager := TManager<TCar>.Create(Car, FdQuery1);
Manager.Post(Car);
Car.Free;
Manager.Free;
end;- Normally you need to configure FdQuery with fielddefs to allow Firedac to generate autoinc, and to fill required field with defaults.
To automatize this task use custom sql to save data
with Car do Manager.Post(Car, 'insert into car (Brand, Year, Power) values(:b, :y, :p)', [Brand, Year, Power]);
Dependencies This unit uses Grijjy.Bson.Serialization lib to generate JSON
uses ...
Grijjy.Bson.Serialization, // ToJson
Grijjy.Bson; // Json Annotationgit clone https://github.com/grijjy/GrijjyFoundation.gitRead documentation https://grijjy.github.io/GrijjyFoundation/
To remove that dependency just delete uses and implement the method ToJson() with your way

