Source files
  • DataManagementEngineStandard/Connections/DefaulDataConnection.cs
  • DataManagementEngineStandard/Connections/FileConnection.cs
  • DataManagementModelsStandard/ConfigUtil/ConnectionProperties.cs
  • DataManagementModelsStandard/ConfigUtil/ConnectionDriversConfig.cs
  • DataManagementModelsStandard/DataBase/IDataConnection.cs

Overview

The connections layer binds BeepDM abstractions to concrete connection models. It provides:

  • Connection abstraction - IDataConnection interface for all connection types
  • Connection properties - ConnectionProperties as the standard connection config
  • Driver configuration - ConnectionDriversConfig for driver metadata
  • Default connection - DefaulDataConnection for standard datasources
  • File connection - FileConnection for file-based datasources

IDataConnection Interface

public interface IDataConnection
{
    IConnectionProperties ConnectionProp { get; set; }
    ConnectionDriversConfig DataSourceDriver { get; set; }
    ConnectionState ConnectionStatus { get; set; }
    IDMEEditor DMEEditor { get; set; }
    IDMLogger Logger { get; set; }
    IErrorsInfo ErrorObject { get; set; }
    bool InMemory { get; set; }
    int ID { get; set; }
    string GuidID { get; set; }

    // Connection operations
    ConnectionState OpenConnection();
    ConnectionState OpenConnection(DataSourceType dbtype, string host, int port,
        string database, string userid, string password, string parameters);
    ConnectionState OpenConnection(DataSourceType dbtype, string connectionstring);
    ConnectionState CloseConn();
    string ReplaceValueFromConnectionString();
}

ConnectionProperties

The standard configuration object for all data source connections:

public class ConnectionProperties : IConnectionProperties
{
    public string ConnectionName { get; set; }
    public string ConnectionString { get; set; }
    public DataSourceType DatabaseType { get; set; }
    public DatasourceCategory Category { get; set; }
    public string DriverName { get; set; }
    public string DriverVersion { get; set; }
    public string FileName { get; set; }
    public string Host { get; set; }
    public int Port { get; set; }
    public string Database { get; set; }
    public string UserID { get; set; }
    public string Password { get; set; }
    public string Parameters { get; set; }
    public bool UseConnectionString { get; set; }
    public string GuidID { get; set; }
}

Connection String Pattern

// RDBMS
var sqlite = new ConnectionProperties
{
    ConnectionName = "MyDB",
    ConnectionString = "Data Source=./app.db",
    DatabaseType = DataSourceType.SqlLite,
    Category = DatasourceCategory.RDBMS,
    DriverName = "SQLite"
};

// File-based
var csv = new ConnectionProperties
{
    ConnectionName = "MyCSV",
    ConnectionString = "./data/",
    FileName = "customers.csv",
    DatabaseType = DataSourceType.Csv,
    Category = DatasourceCategory.FileBased
};

// Web API
var api = new ConnectionProperties
{
    ConnectionName = "MyAPI",
    ConnectionString = "https://api.example.com/v1",
    DatabaseType = DataSourceType.WebService,
    Category = DatasourceCategory.CLOUD
};

DefaulDataConnection

The base connection implementation for standard datasources:

public class DefaulDataConnection : IDataConnection
{
    public DefaulDataConnection();
    public DefaulDataConnection(IDMEEditor dmEditor);

    public IConnectionProperties ConnectionProp { get; set; }
    public ConnectionDriversConfig DataSourceDriver { get; set; }
    public ConnectionState ConnectionStatus { get; set; }
    public IDMEEditor DMEEditor { get; set; }
    public IDMLogger Logger { get; set; }
    public IErrorsInfo ErrorObject { get; set; }
    public bool InMemory { get; set; }
    public int ID { get; set; }
    public string GuidID { get; set; }

    public ConnectionState OpenConnection();
    public ConnectionState OpenConnection(DataSourceType dbtype, string host,
        int port, string database, string userid, string password, string parameters);
    public ConnectionState OpenConnection(DataSourceType dbtype, string connectionstring);
    public ConnectionState CloseConn();
    public string ReplaceValueFromConnectionString();
}

This is the base class for datasource-specific connections. It automatically initializes ConnectionProp, DataSourceDriver, and a new GuidID on construction.

FileConnection

Specialized connection for file-based datasources:

public class FileConnection : IDataConnection
{
    public FileConnection(IDMEEditor dmEditor);

    public IConnectionProperties ConnectionProp { get; set; }
    public ConnectionDriversConfig DataSourceDriver { get; set; }
    public ConnectionState ConnectionStatus { get; set; }
    public IDMEEditor DMEEditor { get; set; }
    public IDbConnection DbConn { get; set; }

    public ConnectionState OpenConnection();
    public ConnectionState CloseConn();
    public string ReplaceValueFromConnectionString();
}

Open Flow

  1. Link ConnectionProperties to driver via ConnectionHelper.LinkConnection2Drivers
  2. Resolve dynamic values in connection string
  3. Verify file exists at resolved path
  4. If found: set ConnectionState.Open
  5. If not found: set ConnectionState.Broken

ConnectionDriversConfig

Driver metadata registered in ConfigEditor.DataDriversClasses:

public class ConnectionDriversConfig
{
    public string GuidID { get; set; }
    public string PackageName { get; set; }      // e.g., "SQLite"
    public string DriverClass { get; set; }       // e.g., "SQLite"
    public string version { get; set; }           // e.g., "1.0.0"
    public string DbConnectionType { get; set; }  // e.g., "SQLiteConnection"
    public string ConnectionString { get; set; }  // Template: "Data Source={Database};"
    public string classHandler { get; set; }      // e.g., "SQLiteDataSource"
    public DatasourceCategory DatasourceCategory { get; set; }
    public DataSourceType DatasourceType { get; set; }
    public bool ADOType { get; set; }             // true for ADO.NET providers
}

Driver Registration

var driver = new ConnectionDriversConfig
{
    PackageName = "PostgreSQL",
    DriverClass = "Npgsql",
    version = "7.0.0",
    DbConnectionType = "NpgsqlConnection",
    ConnectionString = "Host={Host};Port={Port};Database={Database};Username={UserID};Password={Password}",
    classHandler = "PostgreDataSource",
    DatasourceCategory = DatasourceCategory.RDBMS,
    DatasourceType = DataSourceType.Postgresql,
    ADOType = true
};

editor.ConfigEditor.DataDriversClasses.Add(driver);

Connection States

StateValueDescription
Closed0Connection is closed
Open1Connection is open and ready
Connecting2Connection is being established
Executing4Executing a command
Fetching8Fetching data
Broken16Connection is broken and needs repair

Usage Examples

Creating a Connection

// Create properties
var props = new ConnectionProperties
{
    ConnectionName = "ProductionDB",
    DatabaseType = DataSourceType.SqlServer,
    Category = DatasourceCategory.RDBMS,
    Host = "prod-server",
    Port = 1433,
    Database = "ProductsDB",
    UserID = "app_user",
    Password = "secret"
};

// Add to config
editor.ConfigEditor.AddDataConnection(props);
editor.ConfigEditor.SaveDataconnectionsValues();

Opening a Connection

var state = editor.OpenDataSource("ProductionDB");
if (state == ConnectionState.Open)
{
    var ds = editor.GetDataSource("ProductionDB");
    // Use datasource...
}
else
{
    var error = editor.ErrorObject;
    Console.WriteLine($"Failed: {error.Message}");
}

File Connection

var fileProps = new ConnectionProperties
{
    ConnectionName = "CSVData",
    ConnectionString = "./data/",
    FileName = "export_2024.csv",
    DatabaseType = DataSourceType.Csv,
    Category = DatasourceCategory.FileBased
};

editor.ConfigEditor.AddDataConnection(fileProps);

var fileDs = editor.GetDataSource("CSVData");
// FileConnection checks if ./data/export_2024.csv exists