IDbProvider Interface
Assembly: CodeEffects.Rule.Connection.dll
Namespace: CodeEffects.Rule.Connection
Summary
Describes the database that holds rule storage to the Code Effects libraries: how to connect to it, which SQL dialect it speaks, and in which schema its tables live.
Syntax
public interface IDbProvider
Every call into the Rule Repository and the Audit library that touches the database takes an IDbProvider: the static methods of Context, such as SyncSources(..), GetRules(..), and GetResponse(..), and the constructor of AuditRecorder. Create the provider once at startup and share it; the libraries do not change it.
For SQL Server, use CodeEffects.Rule.Connection.SqlServer.DbProvider. It takes a connection string and an optional schema name that defaults to dbo. For PostgreSQL, use CodeEffects.Rule.Connection.Postgres.DbProvider, whose schema defaults to public:
var dbProvider = new CodeEffects.Rule.Connection.SqlServer.DbProvider(connectionString);
Implement the interface yourself when your application owns the connection, for instance to take the connection string from a secret store. Return your own IConnectionProvider from ConnectionProvider, and the dialect that matches your database from Dialect. The dialect of the SQL Server provider is available as the Dialect property of SqlServer.DbProvider, so a provider of your own can reuse it. A class may implement both interfaces and return itself from ConnectionProvider.
Properties
ConnectionProvider
Type: CodeEffects.Rule.Connection.IConnectionProvider
Gets the provider that creates the connections to the database. Required.
Dialect
Type: CodeEffects.Rule.Connection.IDialect
Gets the object that supplies everything in SQL that differs between databases. The libraries ask it for the quoting of names, the parameter markers, paging, and the classification of failures. Required.
SchemaName
Type: System.String
Gets the name of the database schema that holds the tables of rule storage, such as dbo for SQL Server. The libraries pass it to IDialect.Table(..) to qualify every table name, and the dialect quotes it. An empty name leaves table names unqualified. Required.
Example
using CodeEffects.Rule.Connection;
public class AppDbProvider : IDbProvider
{
private readonly IConnectionProvider connections;
public AppDbProvider(IConnectionProvider connections, IDialect dialect, string schemaName)
{
this.connections = connections;
this.Dialect = dialect;
this.SchemaName = schemaName;
}
public IConnectionProvider ConnectionProvider { get { return connections; } }
public IDialect Dialect { get; }
public string SchemaName { get; }
}
// Reuse the SQL Server dialect with a connection provider of your own
var dialect = new CodeEffects.Rule.Connection.SqlServer.DbProvider(connectionString).Dialect;
var dbProvider = new AppDbProvider(new AppConnectionProvider(connectionString), dialect, "dbo");