Dynamic Properties

I am trying to get the "id" property from my entity using Dynamic Properties. However it is empty. I have tried with other field "nome" and it is empty either.


The entity contains value. 

I have added the property required:

    property    Props: TDynamicProperties read FProps;

On my base class, that is not tagged as [Entity], all my entity classes inherits from it.

I am not sure that is allowed. If the property itself needs to be in the class that is the table, can I keep the FProps on my base class?

I want to have a way to interact with the data in a generic way, using my base class only. For that I have added another property 

    property    Value[const PropName: string]: Variant read GetVariantProp write SetVariantProp; default;

That is default for the class to read the TDynamicProperties and be used like this:

EntityItem['nome']

for each of my entity classes I have a registering initialization:

  NaharEntityFactory.RegisterEntity(TEntityItem.EntityName,
    function(AContext: INaharContext): TNaharEntity
    begin
      result := TEntityItem.Create;
      result.Context := AContext;
    end,
    function(AMappingSetup: TMappingSetup): Boolean
    begin
      AMappingSetup.MappedClasses.RegisterClass(TEntityItem);

      AMappingSetup.DynamicProps[TEntityItem];
      result := true;
    end);

The first anonymous function is to create the entity, the second is called when preparing the map setup and then the TMappingExplorer for each database. I used this way to give a chance to add new fields to the entity on initialization.

I am trying to clone the entity and I see that CopyFieldValues is working.

Now I need to extract the ID from my source entity and I am trying to do that using Dynamic Properties, but it is empty.

I think you misunderstand the use of dynamic props? Or I didn't understand what you want to do.

Dynamic properties are a way to define table fields at runtime because you don't know at design/compile-time what fields your database will have. This is typical when your customer has fields in the database you don't know in advance, or if you have customers with different databases being accessed by the same application.

So for example, a table "customer" might have a field "Birthday" in the database, but you are not sure. So you can't declare a property Birthday in your class, for several reasons:

1. The Birthday field might not be in database
2. Birthday is not part of your "Default" setup, it's something specific in your customer, so you don't want Aurelius to create that field in all customers if you do an UpdateDatabase.

Thus you use dynamic properties for that. Given that explanation, I think it's clear that you can't have "id" or any other existing property in the dynamic properties - just the "extra" ones.

well.. i totally misunderstood the usage of this... I am sorry.


I got the idea of extending, and tested that and works just fine. However I believed it could access the current properties from the class either. 

I can tell you that it could be nice either. Good I was not counting with that.

My future idea was to move for something like this:

EntityItem['name'] := 'john' 

so, that I could not work directly with the target class, but always with a base calss.

Thank you