Running DataSourceApi by passing the result to a DataSource class
Libraries That Should Be Used
The following two libraries should be added to the project.
using Bimser.CSP.DataSource.Api;
using ```<```ProjeAdı```>```. DataSources;
Use Case
This example shows how to execute the DataSource and then export the result from the query into a class and read it from there.
Step 1: Defining the DataSource
As an example, a DataSource is defined in the project with the name TestDataSource, which contains the following query.
QUERY : SELECT ID, USERNAME, FIRSTNAME, LASTNAME FROM OSUSERS
Step 2: Defining the Class to Meet the Columns
The class that will meet the columns from this query is defined.
public class User
{
public int ID {get; set;}
public string USERNAME {get; set;}
public string FIRSTNAME {get; set;}
public string LASTNAME {get; set;}
}
Step 3: Preparing the Codes to Run the DataSource
A DataSource can be run by giving the parameters detailed below to the method named GetData of the DataSourceApi class.
IMPORTANT NOTE: The DataSource to be run must be located in the project where this example is prepared. In this scenario, the DataSource in another external project cannot be run.
In the following code block, it is explained in parts which part should be written and how.
Sample Code:
List```<```User```>``` allUsers = DataSourceApi.GetData```<```User, TestDatasourceRequest```>```(
"TestDataSource",
new TestDatasourceRequest()
{
PARAMETRE1 = "Value1",
PARAMETRE2 = "Value2"
},
Context
);
List'''
<
User>
''' allUsers: In this section, the name of the class defined in Step2 is given to List as a type.DataSourceApi.GetData
<
User, TestDatasourceRequest>
( : In the part that says ' ' "User", the name of the class is given again. For the "TestDatasourceRequest" part, up to "Request" is the name of the DataSource to be executed. That is, the name of the DataSource and the word Request are written contiguously."TestDataSource" : The name of the DataSource to run is given.
new TestDatasourceRequest() ... : Given the parameters to send to the query. If the parameter is not to be given, it should be left as follows. new TestDatasourceRequest()
Context: Context information is given here. It can be left as it is.
Reading the result from the list
The following example illustrates how to read the result from the query. Different methods can also be used.
if(allUsers.Count ```>``` 0 ) // results are coming.
{
LogExtension.Log(allUsers[0].ID, Session);
LogExtension.Log(allUsers[0]. USERNAME, Session);
LogExtension.Log(allUsers[0]. FIRSTNAME, Session);
LogExtension.Log(allUsers[0]. LASTNAME, Session);
}