0

How do I access my Remote Data Module(RDM)'s instance from another unit at runtime? (The RDM is single instance). When I create a normal Data Module descendant Delphi creates a variable for it in the same unit (ex: MyDM: TMyDM), but when I create a RDM's descendant there's no variable.

I'm trying to set the provider of a TClientDataSet created at runtime in another unit to a TDataSetProvider in my RDM, but I can't find a reference to my RDM's instance.

I also tried to do it at design time but while I have no problems to set the connection property of a TSQLQuery from the same unit to a TSQLConnection in that RDM, I wasn't able to set the TClientDataSet's provider, because no providers from the RDM appears in the TClientDataSet's provider list.

Daniel Santos
  • 1,451
  • 15
  • 38
  • Seems that there are not much people here (including me) that understand what you really want. I suggest to rephrase the question and give some more background to make the problem clear. – Uwe Raabe Sep 19 '13 at 17:55
  • Any idea how I could rephrase the question? – Daniel Santos Sep 19 '13 at 18:27
  • I guess the problem is that you set a Provider to a TClientDataset in a remote datamodule in design time, but that provider wasn´t found during run-time. That´s why you decided to set the provider yourself, by code. Am I right? – AlexSC Sep 19 '13 at 18:33
  • No @AlexSC. I've edited the question again, maybe it's clearer now. – Daniel Santos Sep 19 '13 at 18:49
  • @Daniel: ProviderName only shows the names of local providers. If you need them in a different unit, your indeed will have to look for them, but not the way you would like to. – AlexSC Sep 19 '13 at 18:57
  • @Daniel: Is the server you're creating a DLL or an Exe with a main form? If the latter, it's almost trivial to catch hold of a given instance of the RDM when it's created. – MartynA Sep 19 '13 at 21:22

2 Answers2

4

First you need to set the RemoteServer property of your client dataset, assign it an instance of TLocalConnection component (which should be placed on your remote data module since you are not using it remotely). The remote data module unit has to be in the uses clause of the unit with the client dataset, of course.

Then you can assign the ProviderName property of your client dataset.

Ondrej Kelle
  • 36,941
  • 2
  • 65
  • 128
0

I did some study on TRemoteDataModule and learned that it is dedicated to support COM application servers.

The fact you don´t have a variable to your RDM is because you are not supposed to access it like a regular DM. The application server will instantiate the RDM in response to a remote call, just like any COM application. It will be destroyed when no more references exist to that RDM.

Since the life-cicle of that object depends on the client, not the server, having a reference to it in the server is highly dangerous. You never know when it´s valid or not. Besides, more than one instance will exist, one for each client that is accessing that object in a given moment.

Considering that, I believe is very reasonable to tell you that it´s impossible to access the RDM after it is created to perform the correction you intend to do.

If you really need to put the TDatasetProvider in a different unit, then my best suggestion is to make the RDM look for that provider in some kind of Provider poll service. Doing like this will enable you to find the provider you need everytime a new RDM is instantiated and only when it is instantiated.

In your place I would add a handler to the OnCreate event of the RDM and in that handler I would call a method like TProviderPool.GetProvider. That method would give me a provider and I would assign its name to the ProviderName property of the CDS.

AlexSC
  • 1,823
  • 3
  • 28
  • 54
  • Is this case the RDM is single instance, in other words, every client will start it's own application server process with a single instance of the RDM. I just thought that could retrieve a reference to that instance. – Daniel Santos Sep 19 '13 at 19:07
  • @Daniel: yes, I noticed it in question after writing the answer, but everything continues to apply. Even being a single instance, you don´t know when it will be created because the COM object creation is a response to the `CreateRemoteObject()` that happens in the clients. And, when the client drops the remote reference, the COM object will be destroyed, which is another event that can't be noticed by any code outside the RDM code. – AlexSC Sep 19 '13 at 19:41