DapperDataSource Class
Definition
- Namespace
- Savvyio.Extensions.Dapper
- Assemblies
- 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
-
objectDapperDataSource
- 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
| Name | Description |
|---|---|
| DapperDataSource(DapperDataSourceOptions) | Initializes a new instance of the DapperDataSource class. |
Properties
| Name | Description |
|---|---|
| ConnectionString | Gets or sets the string used to open a database. |
| ConnectionTimeout | Gets the time to wait (in seconds) while trying to establish a connection before terminating the attempt and generating an error. |
| Database | Gets the name of the current database or the database to be used after a connection is opened. |
| State | Gets the current state of the connection. |
Methods
| Name | Description |
|---|---|
| BeginTransaction() | Begins a database transaction. |
| BeginTransaction(IsolationLevel) | Begins a database transaction. |
| ChangeDatabase(string) | Changes the current database for an open |
| Close() | Closes the connection to the database. |
| CreateCommand() | Creates and returns a Command object associated with the connection. |
| OnDisposeManagedResources() | Called when this object is being disposed by either Dispose() or Dispose(bool) having |
| Open() | Opens a database connection with the settings specified by the |