Need a special offer?Find out if your project fits.
+
All documentation
  • Introduction
  • Connecting to Data Source
    1. Supported data sources
    2. Connecting to other data sources
  • Browser compatibility
  • Documentation for older versions
  • Implementing the custom parser

    One of the DLL’s main advantages is its ability to visualize data from a custom data source. To use the custom data source with the DLL, you need to implement a special parser.

    This guide describes how to create and use a parser for the custom data source.

    Example of a custom parser

    To simplify the implementation of the custom parser, we prepared a sample ASP.NET Core application using Flexmonster.DataServer.Core.dll. This application contains an example of a custom parser, which can be viewed in the CustomParser.cs file.

    To create a new ASP.NET Core application with the Data Server or to embed the DLL in an existing project, refer to the guide on Flexmonster.DataServer.Core.dll.

    Initializing the custom parser

    The CustomParser class implements the IParser interface.

    First, the custom parser’s properties are specified:

    public string Type { get; private set; }

    public IndexOptions IndexOptions { get; set; }

    public Dictionary<int, ColumnInfo> ColumnInfoByIndex { get; set; }

    To learn more about these properties, see the IParser interface section.

    Then, the parser’s type is set in the constructor (in this case, the type is "custom"):

    public CustomParser()
    {
    Type = "custom";
    }

    Specifying the data

    This parser uses built-in data defined in the data variable. The data has three fields; their names and types are added to the ColumnInfoByIndex collection in the FillDataInfo() method:

    private void FillDataInfo()
    {
    ColumnInfoByIndex = new Dictionary<int, ColumnInfo>();
    ColumnInfoByIndex.Add(0, new ColumnInfo("Country", data[0, 0].GetType()));
    ColumnInfoByIndex.Add(1, new ColumnInfo("Price", data[0, 1].GetType()));
    ColumnInfoByIndex.Add(2, new ColumnInfo("Date", data[0, 2].GetType()));
    }

    Parsing the data

    The Parse() method is responsible for parsing data. In this case, Parse() calls the FillDataInfo() method first, then processes and returns the data:

    public IEnumerable<object[,]> Parse()
    {
    FillDataInfo();
    for (int i = 0; i < data.GetLength(0); i++)
    {
    var partData = new object[1, 3];
    for (int j = 0; j < data.GetLength(1); j++)
    {
    partData[0, j] = data[i, j];
    }
    //return data by line
    yield return partData;
    }
    }

    See the IParser interface section to learn more about the Parse() method.

    Using the created parser

    To be available to the DLL, the custom parser is added to the dependency injection container in the Program.cs file:

    using Flexmonster.DataServer.Core.Parsers;
    
    var builder = WebApplication.CreateBuilder(args);
    // Other configurations
    
    builder.Services.AddTransient<IParser, CustomParser>(); 
    // Other configurations

    The AddTransient() method is used to provide a new parser instance each time it is requested from the service container.

    The IParser interface

    All the parsers should implement the IParser interface. IParser has the following definition:

    public interface IParser
    {
        string Type { get; }
        IndexOptions IndexOptions { get; set; }
        IEnumerable<object[,]> Parse();
        Dictionary<int, ColumnInfo> ColumnInfoByIndex { get; }
    }

    Below is a detailed description of the parameters:

    • Type - String. The parser's name. It will be used in the appsettings.json file when creating an index, for example:
      "DataSources": 
      [
         {
            "Type": "custom",
            "Indexes": {
              "custom-index": null
            }
         }
      ]
    • Parse - Function. Responsible for parsing the data. The purpose of the function is partial data loading, which allows working with large datasets. The Parse() method has the following signature:
      IEnumerable<object[,]> Parse(); 
      object can have either any primitive type or the DateTime type, so all dates in the dataset to parse should have the DateTime type.
    • IndexOptions - IndexOptions. Stores the options defined when creating an index. Index options can be set either via the appsettings.json file or inside the dependency injection container in the Program.cs file. See how to set configs for the Data Server as a DLL.
      To specify custom index options, create a class based on the IndexOptions class (e.g., CustomIndexOptions):
      public class CustomIndexOptions: IndexOptions
      {
          public string CustomOption { get; set; }
          // Other options

          public CustomIndexOptions(
              string customOption,
              // Other options
          ): base("custom")
      {
      CustomOption = customOption;
      // Other options
      }
      }
      You can set custom index options only in the Program.cs file:
      var builder = WebApplication.CreateBuilder(args);
      // Other service configurations

      builder.Services.Configure<DatasourceOptions>(options =>
      {
      // Other configs

         options.Indexes.Add("custom-index", new CustomIndexOptions(
      "custom",
      // Other options
      ));
         // Other configs
      });
      // Other service and app configurations
      You can also use the created class in the custom parser. For example, in the Parse method:
      public IEnumerable<object[,]> Parse()
      {
      var customIndexOptions = (CustomIndexOptions)IndexOptions;
      if (customIndexOptions.CustomOption == "custom")
      {
      // Other code
      }
      // Other code
      }
    • ColumnInfoByIndex - Dictionary<int, ColumnInfo>. Allows setting the name and type for columns (note that each column must have the name and type). ColumnInfoByIndex is a collection of [key, value] pairs, where key is the column’s number (starting from 0) and value is an instance of the following class:
      {
         public string Name { get; set; }
         public Type Type { get; set; }
      }

      Note The ColumnInfoByIndex collection should contain information about columns before the Parse() method returns the data.

    What's next?

    You may be interested in the following articles: