Hallo.
I am writing a tier2 ordering software for network usage. So we have client and server.
On the client I create Objects of TBest in which the Product ID, the amount and the user who orders it are saved. (So this is a item of an Order).
An order can have multiple items and those are saved in an array to later send the created order to the server. The class that holds the array is called TBestellung.
So i created both
TBest.toString: string;
and
TBest.fromString(source: string): TBest;
Now, I send the toString result to the server via socket and on the server I create the object using fromString (its parsing the attributes received). This works as intended.
Question: Is there a better and more elegant way to do that? Serialisation is a keyword, yes, but isn't that awful / difficult when you serialize an object (TBestellung in this case) that contains an Array of other Objects (TBest in this case)?
//Small amendment: Before it gets asked. Yes I should create an extra (static) class for toString and fromString because otherwise the server needs to create an "empty" TBest in order to be able to use fromString.
There are free serialization libraries for Delphi (also for Free Pascal):
JSON and XML are cross-platform / cross-language which might be helpful for future interfaces to other systems like PHP-based web shop for example. They are also free and open standards (no vendor lock-in)
Update: I would not use the constructor-based approach, while maybe looking trivial in the beginning, (de)serialization can become a complex process which would add more and more non-class-specific code to the classes which need serialization. Instead, I would use 'builder' / 'parser' classes (Factory pattern) to keep responsibilities clear and dependencies low.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With