[]
        
(Showing Draft Content)

Develop a .NET Custom Data Source

A .NET Custom Data Source

A custom .NET data source requires the .NET8 SDK. The steps to create it involve creating a library project in Visual Studio, referencing the packages (Gcef.CustomProvider.Native.dll) and the Nuget package for the data provider for the database you want to support and finally writing a class that implements a single interface (Gcef.CustomProvider.Native.INativeQueryDataProvider) which includes few properties and methods.


Here are the step-by-step instructions on how to build a sample custom data source for Firebird Relational Database.

Develop a Firebird Data Source

Create a Visual Studio Project

1. Create a Class Library project in Visual Studio.

VSnewproject


2. Choose a class library project


VSConfigureProject

Reference the Custom Provider Library

Search NuGet for Gcef.CustomProvider.Native and install the package in your project.


VSDependencies

Reference the Firebird Client Library

Similarly, search for and add the FirebirdSql.Data.FirebirdClient NuGet package to the project.


VSProviderDependency

Add Project Resources

1. Three resource files should be added to the project:

  • A small 16x16 icon that is shown in Wyn.

  • A large 180x130 icon that is shown in Wyn.

  • A markdown file named UserGuide.md file. In this file explains add information about the database and the connection string to use.


For the Firebird example, you can download these three resources from this zip file for Firebird to complete this sample project.


FirebirdProviderResources.zip


ProviderUserGuide


2. Select these resource files in the Visual Studio project and set Output Directory "Copy Always" in the properties window.


CopyAlways

Implement the Interface in the Main Class

Name the main class of the project according to the data source. It should implement the INativeQueryDataProvider interface.


For the Firebird example, you can replace the content of the class with the complete sample code below.


VSDevelopCustomProvider

Implement the Interface

Implement the methods in the FirebirdProvider class.

using FirebirdSql.Data.FirebirdClient; 
using Gcef.CustomProvider.Native;
using System.Data;
using System.Reflection;

namespace FirebirdProvider
{
    public class FirebirdProvider : INativeQueryDataProvider
    {
        public static string ProviderName => "Firebird";

        public static void Configure(IFeatureCollection features)
        {
            features.Metadata().DisplayName = ProviderName;
            features.Metadata().Description = "This is Firebird provider.";

            var assemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            if (Path.Combine(assemblyDirectory!, "UserGuide.md") is var userGuidePath && File.Exists(userGuidePath))
            {
                features.UserGuide().UserGuideMarkdown = File.ReadAllText(userGuidePath);
            }

            if (Path.Combine(assemblyDirectory!, "firebird_16x16.png") is var smallIconPath && File.Exists(smallIconPath))
            {
                features.Metadata().SmallIcon = GetDataURL(smallIconPath);
            }

            if (Path.Combine(assemblyDirectory!, "firebird_180x130.png") is var largeIconPath && File.Exists(largeIconPath))
            {
                features.Metadata().LargeIcon = GetDataURL(largeIconPath);
            }

            static string GetDataURL(string imgFilePath)
            {
                return "data:image/png;base64," + Convert.ToBase64String(File.ReadAllBytes(imgFilePath));
            }

            features.Get<IParameterParseFeature>().NeedFillParameters = false;
        }

        public static INativeQueryDataProvider CreateInstance() => new FirebirdProvider();

        public async Task ExecuteAsync(INativeQuery nativeQuery, Action<IDataReader> readerConsumer, params NativeParameter[] parameters)
        {
            using var connection = new FbConnection(nativeQuery.ConnectionString);
            await connection.OpenAsync();
            var command = connection.CreateCommand();
            command.CommandText = nativeQuery.QueryText;
            foreach (var parameter in parameters)
            {
                var para = command.CreateParameter();
                para.ParameterName = parameter.Name;
                para.Value = parameter.ParameterValue;
                command.Parameters.Add(para);
            }
            readerConsumer(await command.ExecuteReaderAsync());
        }

        public async Task TestConnectionAsync(string connectionString)
        {
            using var connection = new FbConnection(connectionString);
            await connection.OpenAsync();
        }
    }
}

VSCodeFinished

Publish the Project to Generate the Artifact File.

Right-click the project name in Visual Studio. Choose a target folder and click "Publish" to publish your project.


VSPublishArtifacts


Artifacts Folder

Send to the Wyn Administrator

Zip the published artifacts folder and send it to the Wyn Server administrator to deploy to the Wyn Server machine. If you are the administrator, the instructions to deploy the custom data source and configure Wyn are in the Deploy and Configure a Custom Data Source in the Adminstrators Guide.