Salix.Dapper.Cqrs.MsSql 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Salix.Dapper.Cqrs.MsSql --version 1.0.0                
NuGet\Install-Package Salix.Dapper.Cqrs.MsSql -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Salix.Dapper.Cqrs.MsSql" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Salix.Dapper.Cqrs.MsSql --version 1.0.0                
#r "nuget: Salix.Dapper.Cqrs.MsSql, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Salix.Dapper.Cqrs.MsSql as a Cake Addin
#addin nuget:?package=Salix.Dapper.Cqrs.MsSql&version=1.0.0

// Install Salix.Dapper.Cqrs.MsSql as a Cake Tool
#tool nuget:?package=Salix.Dapper.Cqrs.MsSql&version=1.0.0                

Dapper.Cqrs

Libraries (NuGet packages) to help employ CQRS (Command Query Responsibility Segregation) pattern to database access in various .Net projects. Approach unifies database data retrieval under CQRS handler, which is the only dependency for business logic (getting rid of multiple repository injections).

Package uses Dapper - a simple object mapper for .Net built by StackOverflow developers and is one of the most performing ORMs in .Net landscape (if not fastest).

Solution is separating concerns into Abstractions (no dependencies), Database-specific and Testing helpers to be used in more architecturally utilizable way with practical developer usage ease and ability to validate "magic string queries" against database to avoid runtime fails due to database structural changes not followed in code.

Repo containing actual usage sample project and Visual Studio item templates for most of development needs.

Detailed documentation is in WIKI.

Usage

When packages are added and set-up (see "Installation" section below) - as application developer you need to do two things (besides writing tests).

  • Create IQuery (Data retrieval) or ICommand (Data modification) implementation classes based on provided base classes.
  • Inject ICommandQueryContext into your business logic class and use it to execute IQuery and ICommand classes against database engine. This is the only dependency required to work with database (Yeah, no more numerous repositories to depend upon!)

IQuery & MsSqlQuery*Base

Required to be able to read data from database. Create new class and implement its interface via provided base classes. Case class MsSqlQueryMultipleBase<T> is class, implementing most of IQuery interface demands to retrieve multiple records (IEnumerable<T>) from database. Base class MsSqlQuerySingleBase<T> does the same, but to retrieve only one record mapped to database poco object T.

ICommand

Required to be able to modify data in database. Create new class and implement its interface via provided base classes.

Execution

Inject ICommandQueryContext into your business logic classes, which require to work with data in database.

public class SampleLogic : ISampleLogic
{
    private readonly ICommandQueryContext _db;
    
    // Constructor - yes, inject just this one (no more numerous repositories!)
    public SampleLogic(ICommandQueryContext db) => _db = db;
}

Then in this class methods you can use this injected object and use its methods with prepared IQuery and ICommand classes to do database calls.

// Reading data
public async Task<IEnumerable<SampleData>> GetAll(int refId) => 
    await _db.QueryAsync(new SampleQuery(refId));

// Creating (saving) data
public async Task<int> Create(SampleData dataObject) => 
    await _db.ExecuteAsync(new SampleCreateCommand(dataObject));

Testability

As package uses interfaces for everything - it is easy to be mocked for isolation in pure unit-testing routines.

Testing package includes prepared base classes and helpers to automatically find all Queries and Commands in your project and validate SQL statements in those against database for syntax validity. There are helpers for other testing needs, like compare DTO with database object and write/read tests.

Registration

Register package components with your dependency injection container, like here for MS.Extensions.DI

services.AddScoped<IMsSqlContext, DatabaseContext>(svc =>
    new DatabaseContext(
        connectionString,
        svc.GetService<ILogger<DatabaseContext>>()));
services.AddScoped<IDatabaseSession, SqlDatabaseSession>();
services.AddScoped<ICommandQueryContext, CommandQueryContext>();
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Salix.Dapper.Cqrs.MsSql:

Package Downloads
Salix.Dapper.Cqrs.MsSql.Testing.XUnit

Base classes, fixture, helpers to test functionality built with Salix.Dapper.Cqrs.MsSql package with xUnit. Entire Salix.Dapper.CQRS package suite provides easy, testable and well documented approach to work with database from .Net application projects. Available new file templates takes care of creating necessary boilerplate code.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.3 165 2/17/2024
1.2.1 3,362 11/10/2022
1.2.0 733 1/15/2022
1.1.0 556 10/28/2021
1.0.0 1,512 5/28/2021
1.0.0-rc1 285 5/27/2021

Initial release providing ability to use Dapper in CQRS manner for MS SQL database engine.