Aurelius Entity and RTTI

Hi,

I'm assigning values to a Aurelius Entity class from another calss using RTTI with the following code:

AProperty.SetValue(AEntity,TValue.From<Nullable<string>>(Value.AsString));

When I try to read the values from the Aurelius Entity using RTTI with the following code I get an "invalid type cast error":

var CadenaNullable: Nullable<string>;
Value := AProperty.GetValue(AEntity);
CadenaNullable := Value.AsType<Nullable<string>>;

Any hints?

Thanks in advance,

Omar Zelaya

Which Nullable implementation is this ? Spring4D?
If so, you may be also calling Spring unit TValue record helper methods instead of system.rtti pure tvalue methods.
This way, everything must be handled with care: Aurelius + System.RTTI + Spring4D.

Are you making some sort of generic all-purpose dynamic RTTI assignments? Wouldn't a couple of custom Assign methods in your entities class helpers solve your problem?

Aurelius Nullable implementation. "Aurelius.Types.Nullable.pas".

I have fixed my code.

Thanks.

Do you mind sharing the solution for other users that might have the same issue?

Sorry. At the first time I read the topic, <string> was not visible, so I thought you were using Spring4D non-generic Nullable record type.

Hi,

Actually not fixed yet.

When I read ther values just after I set them using "AProperty.SetValue(AEntity,TValue.From<Nullable>(Value.AsString));" it works.

If I try to read the values after getting the entity by calling "Manager.Find" I get the invalid type cast error.

Try referencing unit Bcl.Types.Nullable instead of Aurelius.Types.Nullable.

Hi,

Unfortunantly I get the same results. Not able to retrieve values from the entity.

It works when assigning values to a newly created entity, If I do a "Manager.Find" , I'm not able to change or read the values of the entity.

Thanks in advance,

Omar Zelaya

My guess is that you are missing Nullable<T> types from Bcl.Types.Nullable and Aurelius.Types.Nullable. Those are two different types.

It would be helpful If you could provide a small sample project reproducing the issue.

I'm using Data modeler to generate the entities pas file and it automatically includes "Aurelius.Types.Nullable" thats why i'm using the same on my code to set and get the values. looks like the code that load the entity from database is not using the same.

Or at least the code where you implement your AProperty.GetValue & AProperty.SetValue.

Up there you use Value.AsString . Aurelius Nullable relays on it's implicit and explicit typecast operators, and TValue does not access those operators. I suspect this typecast error comes from introducing something that is a String (Value.ASString), and then trying to invoke it through TValue as a Nullable.

So, changing it to Bcl.Types.Nullable solves your issue?

Hi,

Changing to "Bcl.Types.Nullable" I get the same "invalid type cast error".

Can you guide me in which unit(function) does Aurelius loads the entity from the database?

Thanks in advance,

Omar Zelaya

It's Aurelius.Commands.Loader unit.
Again, if you send a small project reproducing the issue we might be able to take a look at it.

GettingStarted.zip (78.2 KB)

Hi,

I have modified the getting started demo, so the email update is done using RTTI.

Thanks in advance,

Omar Zelaya

In Entities.pas unit, changing the used unit Aurelius.Types.Nullable by Bcl.Types.Nullable solves the issue:

unit Entities;

interface

uses
  Aurelius.Mapping.Attributes,
  Bcl.Types.Nullable;

type
  [Entity]

Or, setting the property using the Aureilus.Types.Nullable type:

               lProperty.SetValue(AEntity,TValue.From<Aurelius.Types.NullableNullable<string>>(AValue.AsString))

In summary, if your property is Nullable<T> coming from Aurelius.Types.Nullable, you should set it using the Nullable<T> type from the same unit.