Custom Data Sources

As we know, data providers are used by data sources to provide endpoints with data. It is a regular C# class that implements the IDataProvider interface. There are several built-in data providers, but all of them work with the objects. Let’s create a custom one that will just provide a hardcoded text.

Create the MyDataProvider class inside the main web application project and implement the IDataProvider interface:

public class MyDataProvider : IDataProvider
{
  public string Description => "Provides the hardcoded values.";

  public IEnumerable<ParameterGroup> ParameterGroups => new ParameterGroup[] { };

  public async Task<dynamic> GetDataAsync(HttpContext httpContext, DataSource dataSource)
  {
    ExpandoObjectBuilder expandoObjectBuilder = new ExpandoObjectBuilder();

    expandoObjectBuilder.AddProperty("Value", "Some hardcoded value from our custom data source.");
    return expandoObjectBuilder.Build();
  }
}

Now, when our data provider class is added, navigate to the backend’s Development/Endpoints section, then to the data source list of the Default endpoint, and create one more data source:

../../_images/117.png

Please note, that our new data provider C# class is automatically resolved and added to the drop down list. Click the Save button. Data source is created:

../../_images/29.png

All the regular pages now have the Hardcoded property inside their view models (thanks to the DefaultRequestProcessor request processor; your own request processor might act in a different way to process provided data).

Let’s update the RegularPage.cshtml view to display the value from our new data source:

<p>@Model.Hardcoded.Value</p>

Run the web application and check the output:

../../_images/37.png

Good. Everything works as expected.

Data Source Parameters and Parameter Groups

As well as the form handlers and endpoints, data providers support parameters and parameter groups. The implementation is absolutely the same, so please just take a look at the endpoints for the sample. Also, please take a look at the built-in data providers to see how they get the parameter values.