Table of Contents

Class DataCommand

Namespace
FluentCommand
Assembly
FluentCommand.dll

A fluent class to build a data command.

public class DataCommand : DisposableBase, IDataCommand, IDataQuery, IDisposable, IDataQueryAsync, IAsyncDisposable
Inheritance
DataCommand
Implements
Inherited Members
Extension Methods

Constructors

DataCommand(IDataSession, DbTransaction)

Initializes a new instance of the DataCommand class.

public DataCommand(IDataSession dataSession, DbTransaction transaction)

Parameters

dataSession IDataSession

The data session.

transaction DbTransaction

The DbTransaction for this DataCommand.

Properties

Command

Gets the underlying DbCommand for this DataCommand.

public DbCommand Command { get; }

Property Value

DbCommand

Methods

CommandTimeout(int)

Sets the wait time (in seconds) before terminating the attempt to execute the command and generating an error.

public IDataCommand CommandTimeout(int timeout)

Parameters

timeout int

The time, in seconds, to wait for the command to execute.

Returns

IDataCommand

A fluent interface to the data command.

DisposeManagedResources()

Disposes the managed resources.

protected override void DisposeManagedResources()

DisposeResourcesAsync()

Disposes the managed resources.

protected override ValueTask DisposeResourcesAsync()

Returns

ValueTask

Execute()

Executes the command against a connection.

public int Execute()

Returns

int

The number of rows affected.

ExecuteAsync(CancellationToken)

Executes the command against a connection asynchronously.

public Task<int> ExecuteAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

Task<int>

The number of rows affected.

ExpireCache<TEntity>()

Expires cached items that have been cached using the current DataCommand.

public IDataCommand ExpireCache<TEntity>()

Returns

IDataCommand

A fluent interface to the data command.

Type Parameters

TEntity

The type of the entity.

Remarks

Cached keys are created using the current DataCommand state. When any Query operation is executed with a cache policy, the results are cached. Use this method with the same parameters to expire the cached item.

LogState(object)

Use to pass a state to the IDataQueryLogger.

public IDataCommand LogState(object state)

Parameters

state object

The state to pass to the logger.

Returns

IDataCommand

A fluent interface to the data command.

Remarks

Use the state to help control what is logged.

Parameter(DbParameter)

Adds the parameter to the underlying command.

public IDataCommand Parameter(DbParameter parameter)

Parameters

parameter DbParameter

The DbParameter to add.

Returns

IDataCommand

A fluent interface to the data command.

Exceptions

ArgumentNullException

parameter is null

QueryAsync<TEntity>(Func<IDataReader, TEntity>, CommandBehavior, CancellationToken)

Executes the command against the connection and converts the results to TEntity objects asynchronously.

public Task<IEnumerable<TEntity>> QueryAsync<TEntity>(Func<IDataReader, TEntity> factory, CommandBehavior commandBehavior = CommandBehavior.SingleResult, CancellationToken cancellationToken = default)

Parameters

factory Func<IDataReader, TEntity>

The delegate factory to convert the IDataReader to TEntity.

commandBehavior CommandBehavior

Provides a description of the results of the query and its effect on the database.

cancellationToken CancellationToken

The cancellation instruction.

Returns

Task<IEnumerable<TEntity>>

An IEnumerable<T> of TEntity objects.

Type Parameters

TEntity

The type of the entity.

Exceptions

ArgumentNullException

factory is null

QueryMultiple(Action<IDataQuery>)

Executes the command against the connection and sends the resulting IDataQuery for reading multiple results sets.

public void QueryMultiple(Action<IDataQuery> queryAction)

Parameters

queryAction Action<IDataQuery>

The query action delegate to pass the open IDataQuery for reading multiple results.

QueryMultipleAsync(Func<IDataQueryAsync, Task>, CancellationToken)

Executes the command against the connection and sends the resulting IDataQueryAsync for reading multiple results sets.

public Task QueryMultipleAsync(Func<IDataQueryAsync, Task> queryAction, CancellationToken cancellationToken = default)

Parameters

queryAction Func<IDataQueryAsync, Task>

The query action delegate to pass the open IDataQueryAsync for reading multiple results.

cancellationToken CancellationToken

The cancellation instruction.

Returns

Task

QuerySingleAsync<TEntity>(Func<IDataReader, TEntity>, CommandBehavior, CancellationToken)

Executes the query and returns the first row in the result as a TEntity object asynchronously.

public Task<TEntity> QuerySingleAsync<TEntity>(Func<IDataReader, TEntity> factory, CommandBehavior commandBehavior = CommandBehavior.SingleResult | CommandBehavior.SingleRow, CancellationToken cancellationToken = default)

Parameters

factory Func<IDataReader, TEntity>

The delegate factory to convert the IDataReader to TEntity.

commandBehavior CommandBehavior

Provides a description of the results of the query and its effect on the database.

cancellationToken CancellationToken

The cancellation instruction.

Returns

Task<TEntity>

A instance of TEntity if row exists; otherwise null.

Type Parameters

TEntity

The type of the entity.

Exceptions

ArgumentNullException

factory is null

QuerySingle<TEntity>(Func<IDataReader, TEntity>, CommandBehavior)

Executes the query and returns the first row in the result as a TEntity object.

public TEntity QuerySingle<TEntity>(Func<IDataReader, TEntity> factory, CommandBehavior commandBehavior = CommandBehavior.SingleResult | CommandBehavior.SingleRow)

Parameters

factory Func<IDataReader, TEntity>

The delegate factory to convert the IDataReader to TEntity.

commandBehavior CommandBehavior

Provides a description of the results of the query and its effect on the database.

Returns

TEntity

A instance of TEntity if row exists; otherwise null.

Type Parameters

TEntity

The type of the entity.

Exceptions

ArgumentNullException

factory is null

QueryTable()

Executes the command against the connection and converts the results to a DataTable.

public DataTable QueryTable()

Returns

DataTable

A DataTable of the results.

QueryTableAsync(CancellationToken)

Executes the command against the connection and converts the results to a DataTable asynchronously.

public Task<DataTable> QueryTableAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

The cancellation instruction.

Returns

Task<DataTable>

A DataTable of the results.

QueryValueAsync<TValue>(Func<object, TValue>, CancellationToken)

Executes the query and returns the first column of the first row in the result set returned by the query asynchronously. All other columns and rows are ignored.

public Task<TValue> QueryValueAsync<TValue>(Func<object, TValue> convert, CancellationToken cancellationToken = default)

Parameters

convert Func<object, TValue>

The delegate to convert the value..

cancellationToken CancellationToken

The cancellation instruction.

Returns

Task<TValue>

The value of the first column of the first row in the result set.

Type Parameters

TValue

The type of the value.

QueryValue<TValue>(Func<object, TValue>)

Executes the query and returns the first column of the first row in the result set returned by the query. All other columns and rows are ignored.

public TValue QueryValue<TValue>(Func<object, TValue> convert)

Parameters

convert Func<object, TValue>

The delegate to convert the value..

Returns

TValue

The value of the first column of the first row in the result set.

Type Parameters

TValue

The type of the value.

Query<TEntity>(Func<IDataReader, TEntity>, CommandBehavior)

Executes the command against the connection and converts the results to TEntity objects.

public IEnumerable<TEntity> Query<TEntity>(Func<IDataReader, TEntity> factory, CommandBehavior commandBehavior = CommandBehavior.SingleResult)

Parameters

factory Func<IDataReader, TEntity>

The delegate factory to convert the IDataReader to TEntity.

commandBehavior CommandBehavior

Provides a description of the results of the query and its effect on the database.

Returns

IEnumerable<TEntity>

An IEnumerable<T> of TEntity objects.

Type Parameters

TEntity

The type of the entity.

Read(Action<IDataReader>, CommandBehavior)

Executes the command against the connection and sends the resulting IDataReader to the readAction delegate.

public void Read(Action<IDataReader> readAction, CommandBehavior commandBehavior = CommandBehavior.Default)

Parameters

readAction Action<IDataReader>

The read action delegate to pass the open IDataReader.

commandBehavior CommandBehavior

Provides a description of the results of the query and its effect on the database.

ReadAsync(Func<IDataReader, CancellationToken, Task>, CommandBehavior, CancellationToken)

Executes the command against the connection and sends the resulting IDataReader to the readAction delegate.

public Task ReadAsync(Func<IDataReader, CancellationToken, Task> readAction, CommandBehavior commandBehavior = CommandBehavior.Default, CancellationToken cancellationToken = default)

Parameters

readAction Func<IDataReader, CancellationToken, Task>

The read action delegate to pass the open IDataReader.

commandBehavior CommandBehavior

Provides a description of the results of the query and its effect on the database.

cancellationToken CancellationToken

The cancellation instruction.

Returns

Task

RegisterCallback<TParameter>(DbParameter, Action<TParameter>)

Register a return value callback for the specified parameter.

public IDataCommand RegisterCallback<TParameter>(DbParameter parameter, Action<TParameter> callback)

Parameters

parameter DbParameter

The IDbDataParameter to add.

callback Action<TParameter>

The callback used to get the out value.

Returns

IDataCommand

A fluent interface to the data command.

Type Parameters

TParameter

The type of the parameter value.

Sql(string)

Set the data command with the specified SQL statement.

public IDataCommand Sql(string sql)

Parameters

sql string

The SQL statement.

Returns

IDataCommand

A fluent interface to a data command.

StoredProcedure(string)

Set the data command with the specified stored procedure name.

public IDataCommand StoredProcedure(string storedProcedure)

Parameters

storedProcedure string

Name of the stored procedure.

Returns

IDataCommand

A fluent interface to a data command.

UseCache(DateTimeOffset)

Uses cache to insert and retrieve cached results for the command with the specified absoluteExpiration.

public IDataCommand UseCache(DateTimeOffset absoluteExpiration)

Parameters

absoluteExpiration DateTimeOffset

A value that indicates whether a cache entry should be evicted after a specified duration.

Returns

IDataCommand

A fluent interface to the data command.

Exceptions

InvalidOperationException

A command with Output or Return parameters can not be cached.

UseCache(TimeSpan)

Uses cache to insert and retrieve cached results for the command with the specified slidingExpiration.

public IDataCommand UseCache(TimeSpan slidingExpiration)

Parameters

slidingExpiration TimeSpan

A value that indicates whether a cache entry should be evicted if it has not been accessed in a given span of time.

Returns

IDataCommand

A fluent interface to the data command.

Exceptions

InvalidOperationException

A command with Output or Return parameters can not be cached.