-
Notifications
You must be signed in to change notification settings - Fork 2
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
FeatureStoreimplementation of theIFeatureStoreinterface, which can be extended as needed. - An
IDbConnectionFactoryfor creating database connections.
First, install the package:
dotnet add package FeatureManagement.Database.DapperImplement 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")));Dapper works with any ADO.NET provider. Here are examples for different databases:
public class SqlServerConnectionFactory : IDbConnectionFactory
{
private readonly string _connectionString;
public SqlServerConnectionFactory(string connectionString)
{
_connectionString = connectionString;
}
public IDbConnection CreateConnection()
{
return new SqlConnection(_connectionString);
}
}public class NpgsqlConnectionFactory : IDbConnectionFactory
{
private readonly string _connectionString;
public NpgsqlConnectionFactory(string connectionString)
{
_connectionString = connectionString;
}
public IDbConnection CreateConnection()
{
return new NpgsqlConnection(_connectionString);
}
}public class SqliteConnectionFactory : IDbConnectionFactory
{
private readonly string _connectionString;
public SqliteConnectionFactory(string connectionString)
{
_connectionString = connectionString;
}
public IDbConnection CreateConnection()
{
return new SqliteConnection(_connectionString);
}
}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();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));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...
}