View on GitHub

Belgrade-SqlClient

Small async ADO.NET helper library

Command-Query Responsibility Segregation

Belgrade Sql Client can be used in the architectures that use Command-Query Responsibility Segregation (CQRS) pattern. Data access classes are divided in two classes:

Contents

Query Mapper
Query Pipe
Command

Initializing data access components

In order to initialize data access components, you can provide standard SqlConnection as a constructor:

const string ConnString = "Server=<SERVER NAME>;Database=<DB NAME>;Integrated Security=true";
IQueryMapper sqlMapper = new QueryMapper(new SqlConnection(ConnString));
IQueryPipe sqlPipe = new QueryPipe(new SqlConnection(ConnString));
ICommand sqlCmd = new Command(new SqlConnection(ConnString));

Query Mappers

QueryMapper is data-access component that executes a query against database and maps results using mapper function.

await sqlMapper.Sql(command).Map(row => { /* Populate an object from the row */ });

You can provide function that accepts DataReader as an argument and populate fields from DataReader into some object.

Query Pipes

QueryPipe is data-access component that executes a query against database and stream results into an output stream.

await sqlQuery
        .Sql("select * from Product FOR JSON PATH")
        .Stream(Response.Body);

Method Stream in QueryPipe class may accept two or three parameters:

Command

Command is data-access component that executes a query or stored procedure that don’t return any results. Commands are used in update statements.

await cmd
        .Proc("InsertProduct")
        .Param("Product", product);
        .Exec();

It is just an async wrapper around standard SqlComand that handles errors and manage connection.

See also