A Data Provider is a GeneXus object aimed at providing data collections (for example, a list of customers) or a data structure (for example, customer data) in an easy and high-level, declarative way.
The output is a hierarchical structure: how GeneXus represents that? As an SDT or BC, or a collection of them.
The strength of Data Providers lies in that the focus is on the output. As a result, the GeneXus analyst only has to declare how the several data items of the hierarchical output structure should be filled.
Suppose that we have defined the 'Customer' transaction shown below:
We need to load the customers database into a collection to send it to another object, such as a Web Panel, for example, or any other object:
First, we have to define the SDT to load the customers. A first consideration is that we cannot name the SDT with the same name as the transaction (it's not allowed), so we will call it 'Clients'. A second consideration is about the fact that we have two ways to work with collections. As we will see, we can choose any of them.
Defining the 'Clients' collection SDT:
Next, we have to create the Data Provider. We will call it 'CustomersProvider'. In order to work less, after creating the Data Provider we can drag the 'Clients' SDT and the Source will look as follows:
We only have to fill the values on the right with CustomerId and CustomerName, respectively.
The 'Output' Property is a special property that should be given special attention, even if it goes unnoticed because the drag and drop operation automatically set it. The image below shows how we indicate which SDT will be the Data Provider output:
||Note the 'Collection' property. If we had chosen the other possibility, we would have changed to True, as we will see.
The rest is easy and familiar. From the object that requires the customer list, i.e. the web panel of the image above, we only have to define a variable of 'Clients' SDT data type to receive the output of the Data Provider and, for example, later convert it to XML format:
&clients = CustomersProvider()
&xml = &clients.toXML()
See more Using Data Providers in Other GX Objects
Defining the 'Client' SDT:
As in the previous option, now we have to create the 'CustomersProvider' Data Provider. Doing the same as before, after dragging the 'Client' SDT to the Source area, we will have the following:
with the Output Property:
However, this is not what we want... We need to load a collection of customers, so we need to change the 'Collection' property as follows:
Note that changing 'Collection' to True introduces a new 'Collection Name' Property. We have to enter a name and then change the Source, adding a group with that name as a root:
The rest is similar to the first option (defining the &clients variable as a collection of the 'Client' SDT data type).