Skip to content

Dapper Database Provider

Teuz edited this page Apr 3, 2025 · 1 revision

For easy integration with Dapper, you can use the FeatureManagement.Database.Dapper package.

This package provides:

  • A default FeatureStore implementation of the IFeatureStore interface, which can be extended as needed.
  • An IDbConnectionFactory for creating database connections.

Installation

First, install the package:

dotnet add package FeatureManagement.Database.Dapper

Basic Setup

Implement IDbConnectionFactory to create a connection to your database:

public class SqlServerConnectionFactory : IDbConnectionFactory
{
    private readonly string _connectionString;

    public SqlServerConnectionFactory(string connectionString)
    {
        _connectionString = connectionString;
    }

    public IDbConnection CreateConnection()
    {
        return new SqlConnection(_connectionString);
    }
}

Then configure the services:

services.AddDatabaseFeatureManagement<FeatureStore>()
    .UseDapper(new SqlServerConnectionFactory(
        Configuration.GetConnectionString("DefaultConnection")));

Using Different Database Providers

Dapper works with any ADO.NET provider. Here are examples for different databases:

SQL Server

public class SqlServerConnectionFactory : IDbConnectionFactory
{
    private readonly string _connectionString;

    public SqlServerConnectionFactory(string connectionString)
    {
        _connectionString = connectionString;
    }

    public IDbConnection CreateConnection()
    {
        return new SqlConnection(_connectionString);
    }
}

PostgreSQL

public class NpgsqlConnectionFactory : IDbConnectionFactory
{
    private readonly string _connectionString;

    public NpgsqlConnectionFactory(string connectionString)
    {
        _connectionString = connectionString;
    }

    public IDbConnection CreateConnection()
    {
        return new NpgsqlConnection(_connectionString);
    }
}

SQLite

public class SqliteConnectionFactory : IDbConnectionFactory
{
    private readonly string _connectionString;

    public SqliteConnectionFactory(string connectionString)
    {
        _connectionString = connectionString;
    }

    public IDbConnection CreateConnection()
    {
        return new SqliteConnection(_connectionString);
    }
}

Dependency Injection Setup

For better integration with DI, define your connection factory as a service:

// Register connection factory
services.AddSingleton<IDbConnectionFactory>(_ => 
    new SqlServerConnectionFactory(Configuration.GetConnectionString("DefaultConnection")));

// Use the registered connection factory
services.AddDatabaseFeatureManagement<FeatureStore>()
    .UseDapper();

Customizing SQL Queries

You can customize SQL queries by extending the default FeatureStore:

public class CustomFeatureStore : FeatureStore
{
    public CustomFeatureStore(IDbConnectionFactory connectionFactory) 
        : base(connectionFactory)
    {
    }

    protected override string GetAllFeaturesQuery => 
        "SELECT f.Id, f.Name, f.RequirementType, fs.Id, fs.FeatureId, fs.FilterType, fs.Parameters " +
        "FROM CustomFeatures f " +
        "LEFT JOIN CustomFeatureSettings fs ON f.Id = fs.FeatureId";
        
    protected override string GetFeatureByNameQuery =>
        "SELECT f.Id, f.Name, f.RequirementType, fs.Id, fs.FeatureId, fs.FilterType, fs.Parameters " +
        "FROM CustomFeatures f " +
        "LEFT JOIN CustomFeatureSettings fs ON f.Id = fs.FeatureId " +
        "WHERE f.Name = @FeatureName";
}

Then register your custom feature store:

services.AddDatabaseFeatureManagement<CustomFeatureStore>()
    .UseDapper(new SqlServerConnectionFactory(connectionString));

Complete Example

public void ConfigureServices(IServiceCollection services)
{
    // Get connection string
    var connectionString = Configuration.GetConnectionString("DefaultConnection");
    
    // Register Dapper feature management
    services.AddDatabaseFeatureManagement<FeatureStore>()
        .UseDapper(new SqlServerConnectionFactory(connectionString))
        .WithCacheService(options => 
        {
            options.SlidingExpiration = TimeSpan.FromMinutes(5);
        });
    
    // Other service registrations...
}

Clone this wiki locally