Class ExpressiveCommand
- Namespace
- Open.Database.Extensions
- Assembly
- Open.Database.Extensions.Core.dll
A deferred, fluent command for running SQL through any IDbConnection, IDbTransaction, connection factory, or connection pool; nothing runs until an execute method is called.
public class ExpressiveCommand : ExpressiveCommandBase<IDbConnection, IDbCommand, IDataReader, DbType, ExpressiveCommand>, IExecuteCommand<IDbCommand>, IExecuteCommand, IExecuteReader<IDataReader>, IExecuteReader
- Inheritance
-
ExpressiveCommand
- Implements
- Inherited Members
- Extension Methods
Examples
List<Post> posts = await connection
.Command("SELECT Id, Title FROM Posts WHERE AuthorId = @authorId")
.AddParam(authorId)
.Map<Post>()
.ToListAsync(cancellationToken);
Remarks
Create one with the Command and StoredProcedure extension methods in ConnectionExtensions, add parameters with AddParam, optionally set a timeout and cancellation token, then run it with one of the execute methods (for example ExecuteNonQuery, ExecuteScalar, ExecuteReader, or Map<T>()).
Connections follow a "get in, get out" model. Each execution takes a connection, opens it only if it is not already open, builds the database command, runs it, and releases the connection. A connection supplied directly is closed again only if this execution opened it and is never disposed. A connection that comes from a factory is disposed after the execution, and one that comes from a pool is given back to the pool.
A command built on a single connection supports one execution at a time; overlapping executions throw InvalidOperationException. Commands built on a factory or pool use a separate connection for each execution.
ExecuteScalarAsync and ExecuteNonQueryAsync are on ExpressiveDbCommand.
Constructors
- ExpressiveCommand(IDbConnectionFactory, CommandType, string, IEnumerable<Param>?)
Initializes a new instance that creates a new connection from a connection factory for each execution.
- ExpressiveCommand(IDbConnectionPool, CommandType, string, IEnumerable<Param>?)
Initializes a new instance that takes its connections from a connection pool.
- ExpressiveCommand(IDbConnection, CommandType, string, IEnumerable<Param>?)
Initializes a new instance that executes on an existing connection.
- ExpressiveCommand(IDbConnection, IDbTransaction?, CommandType, string, IEnumerable<Param>?)
Initializes a new instance that executes on an existing connection, optionally within a transaction.
- ExpressiveCommand(IDbTransaction, CommandType, string, IEnumerable<Param>?)
Initializes a new instance that executes within a transaction, on the transaction's connection.
- ExpressiveCommand(Func<IDbConnection>, CommandType, string, IEnumerable<Param>?)
Initializes a new instance that invokes a function to create a new connection for each execution.
Methods
- AddParams(IDbCommand)
Handles adding the list of parameters to a new command.
- GetProcedureParameterNames(IDbConnection, string)
Gets the parameter names a stored procedure declares through the ADO.NET schema collection
ProcedureParameterswhen the connection is a DbConnection whose provider supports it (SQL Server does), so parameters added withAddParam(value)find their real names.