Recent

Author Topic: ORM with default Values using RTTI and Property  (Read 519 times)

StraightFree

  • New Member
  • *
  • Posts: 28
ORM with default Values using RTTI and Property
« on: January 15, 2020, 07:22:36 pm »
Hello my friends,

I'm creating my own ORM.
I use the class TCrateDB to verify if database exists. If database not exists, I create it.

After this, I use RTTI to read Custom Attributes in my business classes to populate SQL statements and create the table.

Now, I want use some way to populate the database with values default, like this:

Table UNITS (auxiliary of Table PRODUCTS)

ID  |  DESCRIPTION  |  ABBREVIATION
-----------------------------------------------------
1    |  UNITY                | UN
-----------------------------------------------------
2    |  KILOGRAMA      | KG
-----------------------------------------------------
3    |  MILLILITER        |  ML

I think create a property with typekind TStringList with the values default.

Is there any way to retrieve the value of a property using RTTI?
I tried that way:

Code: Pascal  [Select][+][-]
  1. var
  2.  RCtx: TRttiContext;
  3.  RType: TRttiType;
  4.  Prop: TRttiProperty;
  5. begin
  6.   RCtx := TRttiContext.Create;
  7.   try
  8.     RType := RCtx.GetType(aClass);
  9.     Prop := RType.GetProperty('Teste1');
  10.  
  11.     ShowMessage(Prop.Name);
  12.     ShowMessage(Prop.GetValue("I do not know what to put here").ToString);
  13.  
  14.   finally
  15.     RCtx.Free;
  16.   end;
  17. end;
  18.  

Any help will be most welcome.

Thank you.

dsiders

  • Sr. Member
  • ****
  • Posts: 378
Re: ORM with default Values using RTTI and Property
« Reply #1 on: January 15, 2020, 10:51:02 pm »
Hello my friends,

I'm creating my own ORM.
I use the class TCrateDB to verify if database exists. If database not exists, I create it.

After this, I use RTTI to read Custom Attributes in my business classes to populate SQL statements and create the table.

Now, I want use some way to populate the database with values default, like this:

Table UNITS (auxiliary of Table PRODUCTS)

ID  |  DESCRIPTION  |  ABBREVIATION
-----------------------------------------------------
1    |  UNITY                | UN
-----------------------------------------------------
2    |  KILOGRAMA      | KG
-----------------------------------------------------
3    |  MILLILITER        |  ML

I think create a property with typekind TStringList with the values default.

Is there any way to retrieve the value of a property using RTTI?
I tried that way:

Code: Pascal  [Select][+][-]
  1. var
  2.  RCtx: TRttiContext;
  3.  RType: TRttiType;
  4.  Prop: TRttiProperty;
  5. begin
  6.   RCtx := TRttiContext.Create;
  7.   try
  8.     RType := RCtx.GetType(aClass);
  9.     Prop := RType.GetProperty('Teste1');
  10.  
  11.     ShowMessage(Prop.Name);
  12.     ShowMessage(Prop.GetValue("I do not know what to put here").ToString);
  13.  
  14.   finally
  15.     RCtx.Free;
  16.   end;
  17. end;
  18.  

Any help will be most welcome.

Thank you.

First, I would suggest another forum. FPC > General might have been the better choice since RTTI is an FPC feature and not a Lazarus-specific thing.

The test harness for RTTI (located in fpc/.../packages/rtl-objpas/tests/tests.rtti.pas) demonstrates the correct usage.

Getting the RTTI type should be:

Code: Pascal  [Select][+][-]
  1. RType := RCtx.GetType(aClass.ClassInfo);

And, getting the property value should be:

Code: Pascal  [Select][+][-]
  1. var AValue: TValue;
  2. ...
  3. AValue := Prop.GetValue(AClass);
  4. ShowMessage(AValue.AsString)

Hope that helps...



























Lazarus 2.1 (SVN) / FPC 3.0.4 / FPC 3.2.0 / x86-win64 / Windows 8.1

PascalDragon

  • Hero Member
  • *****
  • Posts: 2248
  • Compiler Developer
Re: ORM with default Values using RTTI and Property
« Reply #2 on: January 16, 2020, 09:34:57 am »
Is there any way to retrieve the value of a property using RTTI?
In addition to what dsiders wrote: please note that currently properties must be declared as published to be picked up.

 

TinyPortal © 2005-2018