Hi,
A way (or an example) for create an object Instance from its class name (in Xdata, refering Aurelius entities)? I need a function that return the correct entity type depending from its class name that is passed as a string parameter.
Thanks a lot!
That's just pure Delphi code, you can do it via RTT.
Aurelius itself has a helper class for that, in TMappingExplorer class, which can be accessed, for example, via
Manager.Explorer.ObjectFactory.CreateInstance(MyClass);
But MyClass
must be a TClass
. To find a class from its name you can use FindClass
or TRttiContext.FindType
. Here is a reference:
Thanks Wagner,
I need help in this scenario:
I need to create a function in Xdata that allows me to deserialize a Json record (from a custom Applyupdate on the client, via json-rpc) in an Aurelius instance, carry out the validation, and then insert the entity into the DB.
However, I would like to generalize and pass the class type as a parameter so that I can insert records into each class of the model, without having to instantiate all the classes at design time, but via RTTi in a single function.
A function prototype:
///
function TSerGenService.ClassDeser(ADeserializer: TXdataJsonClientDeserializer; AClassName: String; AValue: String): TObject;
var
tmpjson: string;
tmpRttiTypeIstance: TRttiInstanceType;
tmpRttiContextChild: TRttiContext;
tmpobj: Tobject;
begin
tmpobj:=nil;
tmpRttiTypeIstance := tmpRttiContextChild.FindType(AClassName) as TRttiInstanceType;
tmpobj := tmpRttiTypeIstance.GetMethod('Create').Invoke(tmpRttiTypeIstance.MetaclassType, ).AsObject;
//// tmpobject must be casted? <<<<<<< ???
tmpobj:= ADeserializer.Read(Avalue); //<<<<<<<<<<< ????
TXDataOperationContext.Current.GetManager.update(tmpobj);
end;
////
Basically I'm looking for a way to generalize data insertion into Xdata via a service, passing the class name and the Json that contains the data.
Could anyone provide me with help on this feature?
Thanks in advance
ADeserializer.Read
has a non-generic overload where you pass the class type:
tmpobjvalue := ADeserializer.Read(Value, tmpRttiTypeInstance.MetaclassType);
tmpobj := tmpobjvalue.AsObject;
Hi Wagner,
You're right, I found it, Thanks.
The problem is that everything works with published properties, while the properties of Aurelius classes are declared public, so they are not created. A way to solve?
Thanks
I'm not sure what you are referring to. Can you please elaborate?
Hi,
I think that I must enable advanced RTTI for fill Aurelius enities (Aurelius classes have public properties, not Published). Right?
Thanks
By default it's not needed, the serializer actually uses private fields for the serialization, and those are already available.