How to link design-time TWebActions with a structure of TObjects?

196 Views Asked by At

I have a web service with an ever growing number of TWebAction items defined at design time on a TWebModule, and coupled to OnAction events.

Requests are HTTP GETs through the URL and responses are 'handcrafted' JSON data, i.e. composed at runtime with statement like 'TSuperObject'.S['errormessage'] := lErrMsg;
I want to change this to HTTP POST requests with JSON data, and then all request/response JSON structures being (de)serialized from/to proper Objects (and maybe records) using SuperObjects TSuperRttiContext AsJSON and AsType methods.

How can I create data structures for mapping/linking my new 'Objects to be JSON (de)serialized' with the design time TWebactions? These objects will all be different (some properties can be in a common ancestor), and may contain object or record properties themselves (things like FData: Array of TSubObject [which SuperObject can serialize fine]).

Note: There is already a quick-and-dirty enumerated type that I have used to gather timing statistics (which needs to be maintained too), this could be integrated as well.

type
   TWebAct = (
    ttinfo,
    ttlogin,
    ...
    ttgetcostitemlist,
    ttgetvacationplanning
    );

Basically, I want to minimize the number of places where I need to do maintenance whenever a new TWebAction is added.

1

There are 1 best solutions below

5
On BEST ANSWER

You can use the TWebAct enumeration to put together an array of records that tie the web actions to their respective request and response structures.

type
  // ancestor class for all requests and responses
  TJSONStructure = class(TObject);

  TJSONRequest = class(TJSONStructure);
  TJSONRequestClass = class of TJSONRequest;

  TJSONResponse = class(TJSONStructure);
  TJSONResponseClass = class of TJSONResponse;

const 
  WEBACT_STRUCTURES: array[TWebAct] of 
    record
      RequestClass: TJSONRequestClass;
      ResponseClass: TJSONResponseClass;
    end = (
      {ttInfo}   (RequestClass: TInfoRequest; ResponseClass: TInfoResponse)
    , {ttLogin}  (RequestClass: TLoginRequest; ResponseClass: TLoginResponse)
    ...
  );