IConnectionProvider Interface

Assembly: CodeEffects.Rule.Connection.dll
Namespace: CodeEffects.Rule.Connection

Summary

Supplies the database connections that the Code Effects libraries open to reach rule storage. You implement it once for your database and hand it to the library through an IDbProvider.

Syntax

public interface IConnectionProvider

Remarks

The Rule Repository and the Audit library never hold a connection. Each unit of work asks the provider for a new one, opens it, uses it, and disposes it. This keeps connection pooling, credentials, and the choice of ADO.NET provider in your application.

Return a new, unopened connection from every call. Do not return a shared or cached instance: the libraries open and dispose what they receive, and several requests can ask at the same time, so the implementation must be safe to call from several threads.

For SQL Server and PostgreSQL you do not have to write this interface: SqlServer.DbProvider and Postgres.DbProvider take a connection string and provide all of IDbProvider, including this one. Implement it yourself when you need to control how the connection is made, for example to take the connection string from a secret store on each call or to set up the connection before it is used.

Methods

  • CreateConnection

    Returns: System.Data.IDbConnection - A new connection that has not been opened.

    Creates a connection to the database that holds rule storage. The caller opens it and disposes it. Do not open it yourself.

    Parameters

    • None.

Example

using System.Data;
using CodeEffects.Rule.Connection;
using Microsoft.Data.SqlClient;

public class AppConnectionProvider : IConnectionProvider
{
	private readonly string connectionString;

	public AppConnectionProvider(string connectionString)
	{
		this.connectionString = connectionString;
	}

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

Ask on Stack Overflow - Yes, it's still active
Ask in GitHub Discussions

p102

l097 --

l102

p101

×