Class DapperDataSource
- Namespace
- Savvyio.Extensions.Dapper
- Assembly
- Savvyio.Extensions.Dapper.dll
Provides a default implementation of the IDapperDataSource interface to support the actual I/O communication with a source of data using Dapper.
public class DapperDataSource : Disposable, IDapperDataSource, IDataSource, IDbConnection, IDisposable
- Inheritance
-
DapperDataSource
- Implements
- Derived
- Inherited Members
Examples
DapperDataSource<TContext> is the Savvy I/O base class that wraps a Dapper connection factory. Subclass it to provide a named connection source, then inject the instance into DapperDataStore<T, TContext> subclasses so they share one connection lifecycle. The example creates a concrete data source, opens a connection from it, and verifies the connection is non-null.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using Savvyio.Extensions.Dapper;
namespace ExampleApp;
public sealed class DapperSourceExample
{
public IDbCommand CreateCommand()
{
var source = new DapperDataSource(new DapperDataSourceOptions
{
ConnectionFactory = () => new FakeDbConnection()
});
using var transaction = source.BeginTransaction();
return source.CreateCommand();
}
}
public sealed class FakeDbConnection : IDbConnection
{
private ConnectionState _state;
public string ConnectionString { get; set; } = "Data Source=fake;";
public int ConnectionTimeout => 30;
public string Database => "Fake";
public ConnectionState State => _state;
public IDbTransaction BeginTransaction()
{
return new FakeDbTransaction(this, IsolationLevel.ReadCommitted);
}
public IDbTransaction BeginTransaction(IsolationLevel il)
{
return new FakeDbTransaction(this, il);
}
public void ChangeDatabase(string databaseName)
{
}
public void Close()
{
_state = ConnectionState.Closed;
}
public IDbCommand CreateCommand()
{
return new FakeDbCommand(this);
}
public void Open()
{
_state = ConnectionState.Open;
}
public void Dispose()
{
Close();
}
}
public sealed class FakeDbTransaction : IDbTransaction
{
public FakeDbTransaction(IDbConnection connection, IsolationLevel isolationLevel)
{
Connection = connection;
IsolationLevel = isolationLevel;
}
public IDbConnection Connection { get; }
public IsolationLevel IsolationLevel { get; }
public void Commit()
{
}
public void Rollback()
{
}
public void Dispose()
{
}
}
public sealed class FakeDbCommand : IDbCommand
{
public FakeDbCommand(IDbConnection connection)
{
Connection = connection;
Parameters = new FakeParameterCollection();
}
public string CommandText { get; set; } = string.Empty;
public int CommandTimeout { get; set; }
public CommandType CommandType { get; set; }
public IDbConnection Connection { get; set; }
public IDataParameterCollection Parameters { get; }
public IDbTransaction? Transaction { get; set; }
public UpdateRowSource UpdatedRowSource { get; set; }
public void Cancel()
{
}
public IDbDataParameter CreateParameter()
{
throw new NotSupportedException();
}
public void Dispose()
{
}
public int ExecuteNonQuery()
{
throw new NotSupportedException();
}
public IDataReader ExecuteReader()
{
throw new NotSupportedException();
}
public IDataReader ExecuteReader(CommandBehavior behavior)
{
throw new NotSupportedException();
}
public object ExecuteScalar()
{
throw new NotSupportedException();
}
public void Prepare()
{
}
}
public sealed class FakeParameterCollection : List<object>, IDataParameterCollection
{
public object this[string parameterName]
{
get => throw new NotSupportedException();
set => throw new NotSupportedException();
}
public bool Contains(string parameterName)
{
return false;
}
public int IndexOf(string parameterName)
{
return -1;
}
public void RemoveAt(string parameterName)
{
}
}
Constructors
DapperDataSource(DapperDataSourceOptions)
Initializes a new instance of the DapperDataSource class.
public DapperDataSource(DapperDataSourceOptions options)
Parameters
optionsDapperDataSourceOptionsThe DapperDataSourceOptions used to configure this instance.
Exceptions
- ArgumentNullException
optionscannot be null.- ArgumentException
optionsare not in a valid state.
Properties
ConnectionString
Gets or sets the string used to open a database.
public string ConnectionString { get; set; }
Property Value
- string
A string containing connection settings.
ConnectionTimeout
Gets the time to wait (in seconds) while trying to establish a connection before terminating the attempt and generating an error.
public int ConnectionTimeout { get; }
Property Value
- int
The time (in seconds) to wait for a connection to open. The default value is 15 seconds.
Database
Gets the name of the current database or the database to be used after a connection is opened.
public string Database { get; }
Property Value
- string
The name of the current database or the name of the database to be used once a connection is open. The default value is an empty string.
State
Gets the current state of the connection.
public ConnectionState State { get; }
Property Value
- ConnectionState
One of the ConnectionState values.
Methods
BeginTransaction()
Begins a database transaction.
public IDbTransaction BeginTransaction()
Returns
- IDbTransaction
An object representing the new transaction.
BeginTransaction(IsolationLevel)
Begins a database transaction.
public IDbTransaction BeginTransaction(IsolationLevel il)
Parameters
ilIsolationLevelOne of the IsolationLevel values.
Returns
- IDbTransaction
An object representing the new transaction.
ChangeDatabase(string)
Changes the current database for an open Connection object.
public void ChangeDatabase(string databaseName)
Parameters
databaseNamestringThe name of the database to use in place of the current database.
Close()
Closes the connection to the database.
public void Close()
CreateCommand()
Creates and returns a Command object associated with the connection.
public IDbCommand CreateCommand()
Returns
- IDbCommand
A Command object associated with the connection.
OnDisposeManagedResources()
Called when this object is being disposed by either Dispose() or Dispose(bool) having disposing set to true and Disposed is false.
protected override void OnDisposeManagedResources()
Open()
Opens a database connection with the settings specified by the ConnectionString property of the provider-specific Connection object.
public void Open()