Getting started
Install
Most applications need one package. The others add integrations.
| Package | Use it for | Targets |
|---|---|---|
Open.Database.Extensions.Core |
Everything in this guide except channels, dataflow and SQL Server helpers. Provider-agnostic. | netstandard2.0, netstandard2.1, net10.0, net11.0 |
Open.Database.Extensions.Channel |
Reading into channels | same |
Open.Database.Extensions.Dataflow |
Reading into dataflow blocks | same |
Open.Database.Extensions.MSSqlClient |
SqlConnectionFactory and SQL Server specializations on Microsoft.Data.SqlClient |
net472, net10.0, net11.0 |
.NET 8 and .NET 9 applications use the netstandard2.1 build; .NET Framework applications use netstandard2.0 (or net472 for the SQL Server package). Version 11 is a release candidate, so ask for prereleases:
dotnet add package Open.Database.Extensions.Core --prerelease
Core does not reference any database provider. Bring your own: the examples in this guide use Microsoft.Data.Sqlite so that you can run them without a server:
dotnet add package Microsoft.Data.Sqlite
connectionString in the examples is your own, such as "Data Source=blog.db". The runnable samples use a shared in-memory SQLite database that Db.cs creates.
Your first query
A model is a class with public properties, as below. They can be read-only after construction: init properties work (C# 9), and so does required (C# 11), which here makes a query without an id column fail on its first row (a query that returns no rows does not fail). A positional record or a class with one public constructor works too: it is built through that constructor (see Records and constructors).
public class Post
{
public required int Id { get; init; }
public int Author { get; init; }
public string Title { get; init; } = "";
public string? Body { get; init; }
}
Columns are matched to property and constructor parameter names exactly, else ignoring case, else ignoring case and underscores (first_name feeds FirstName); see Mapping rows to objects.
A command needs a connection. A connection factory creates them, and you create it once per application:
var connectionFactory = new DbConnectionFactory<SqliteConnection>(
()
=> new SqliteConnection(connectionString));
There are two ways to start a command: from the factory, or from a connection you own. Both end the same way, with AddParam for parameters and a terminal (the call that runs the query, such as ToListAsync()). In the samples author is a variable (int author = 42;) and AddParam(author) names the parameter author after it; AddParam("author", 42) names it yourself. Side by side, row against row ("nothing" marks a step the factory makes unnecessary):
From a connection factory
// nothing: the factory creates, opens and disposes the connection
List<Post> posts = await connectionFactory
.Command(ListSql)
.AddParam(author) // name and type inferred from the variable
.Map<Post>()
.ToListAsync(cancellationToken);
From a connection you own
await using var connection = new SqliteConnection(connectionString);
List<Post> posts = await connection
.Command(ListSql)
.AddParam(author) // name and type inferred from the variable
.Map<Post>()
.ToListAsync(cancellationToken);
In both forms the command opens the connection only if it is closed. The factory form disposes the connection when the terminal returns; in the connection form the command closes only what it opened and never disposes it. See Who owns the connection.
Before and after
The same query with plain ADO.NET and with the extensions, row against row: the connection, the command, the parameter, then the read loop and the mapping.
Plain ADO.NET
await using var connection = new SqliteConnection(connectionString);
await connection.OpenAsync(cancellationToken);
await using var command = connection.CreateCommand();
command.CommandText = ListSql;
var parameter = command.CreateParameter();
parameter.ParameterName = "@author";
parameter.Value = author;
command.Parameters.Add(parameter);
var posts = new List<Post>();
await using var reader = await command.ExecuteReaderAsync(cancellationToken);
while (await reader.ReadAsync(cancellationToken))
{
posts.Add(new Post
{
Id = reader.GetInt32(0),
Author = reader.GetInt32(1),
Title = reader.GetString(2),
Body = reader.IsDBNull(3) ? null : reader.GetString(3),
});
}
Open.Database.Extensions (factory)
// nothing: the factory creates, opens and disposes the connection
List<Post> posts = await connectionFactory
.Command(ListSql)
.AddParam(author) // name and type inferred from the variable
.Map<Post>()
.ToListAsync(cancellationToken);
Lines you write, counted from plain_list_aligned and ode_list_aligned (ConnectionPattern.cs; not counting blank lines, // nothing comments or the int author = 42; that declares the value): plain ADO.NET 20, Open.Database.Extensions 5, plus three lines of one-time setup per application.
With SQL Server
Open.Database.Extensions.MSSqlClient provides SqlConnectionFactory. Everything above works the same way, and stored procedures are one call away:
// Open.Database.Extensions.MSSqlClient wraps Microsoft.Data.SqlClient.
var connectionFactory = new SqlConnectionFactory(
@"Server=(localdb)\MSSQLLocalDB;Database=Blog;Integrated Security=true");
Post[] posts = await connectionFactory
.Command("select * from posts where author = @author")
.AddParam(author) // name and type inferred from the variable
.Map<Post>()
.ToArrayAsync(cancellationToken);
int touched = await connectionFactory
.StoredProcedure("[dbo].[TouchPosts]")
.AddParam(author)
.ExecuteNonQueryAsync(cancellationToken);
Next steps
- The foundation explains the extensions underneath, and who owns a connection.
- Expressive commands covers parameters and every terminal.
- Mapping rows to objects lists exactly what
Map<T>()does and does not do. - How little code writes four tasks with ODX, Dapper, EF Core and plain ADO.NET and counts the lines.