How can I access a field that was created in TRoDataModule when I am at in other unit?

Hi, I don’t know if it’s a silly question but anyway…

I created a field in a TRoDataModule service and want to access it elsewhere.

So, I have:

[ROService(__ServiceName, __ServiceID)]
  TServicoMestre = class(TRORemoteDataModule)
  private
     FFoo: string;
  public
    property foo: string read FFoo;
    //[ROServiceMethod]
    //procedure NewMethod;
  end;

and I want to access foo elsewhere.

What I am trying to do is to create a field that is exclusive for each thread (created when a request arrives). I know that the server creates a thread when a request arrives, I’ve and don’t know how can I reference this dataModule from outside.

What I did was: create a threadvar variable in the unit, intercept the create and destroy events of the datamodule to fill and destroy it and let others units reference this variable.

Hi,

this is tricky q because it depends on used class factory.

by default, Per-Request instantiation is used - a new service instance will be created for each incoming request, and destroyed afterwards.

I can suggest to store value in session. in this case, you can access value even if service instance is destroyed.

Hi

I’m using per-request instantiation.
What I’ve done is:

  1. Create a new service and changed the other services to inherit from it.
  2. In this new service, I intercept the onCreate/onDestroy event and create my field with some appropriated value.
  3. Created a threadvar global variable in this unit and fill it with this field value (on the onCreate event).
  4. Everywhere in code, I access this variable.
[ROService(__ServiceName, __ServiceID)]
  TServicoMestre = class(TRORemoteDataModule)
  
  public
    constructor Create(aOwner: TComponent); override;
    destructor Destroy; override;

  end;

threadvar
  dmCurrencyDecimals: TdmCurrencyDecimals;

implementation

{%CLASSGROUP 'Vcl.Controls.TControl'}
{$R *.dfm}

{ TServicoMestre }

constructor TServicoMestre.Create(aOwner: TComponent);
begin
  inherited;
  dmCurrencyDecimals := TDmCurrencyDecimals.Create(nil);
  
end;

destructor TServicoMestre.Destroy;
begin
  
  FreeAndNil(dmCurrencyDecimals);
  inherited;
end;

Currently, it’s working flawlessly. Do you see any problems?

Hi,

no, I see no issue.

Can I ask why you need to access dmCurrencyDecimals outside of TServicoMestre instance?
Probably better to have global instance like server data module, because your TdmCurrencyDecimals isn’t dependent on your service.

Hi,
The values inside dmCurrencyDecimals changes based on user’s request. We have lots of functions that fills this datamodule first, then do its thing and go. So we have to have this datamodule for each thread isolated in order to prevent one thread’s value modify others.

How server data module works?

Hi,

as for me, better to pass service instance (TServicoMestre) or dmCurrencyDecimals instance as a parameter to your threads so they can modify required variable.

it isn’t suitable for your needs.

as for me, better to pass service instance (TServicoMestre ) or dmCurrencyDecimals instance as a parameter to your threads so they can modify required variable.

Yes, this would be the right solution, unfortunately, we don’t have time to change all the code involved.