NET.Database.QueryLayer 3.0.1

dotnet add package NET.Database.QueryLayer --version 3.0.1
                    
NuGet\Install-Package NET.Database.QueryLayer -Version 3.0.1
                    
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="NET.Database.QueryLayer" Version="3.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NET.Database.QueryLayer" Version="3.0.1" />
                    
Directory.Packages.props
<PackageReference Include="NET.Database.QueryLayer" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add NET.Database.QueryLayer --version 3.0.1
                    
#r "nuget: NET.Database.QueryLayer, 3.0.1"
                    
#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.
#:package NET.Database.QueryLayer@3.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=NET.Database.QueryLayer&version=3.0.1
                    
Install as a Cake Addin
#tool nuget:?package=NET.Database.QueryLayer&version=3.0.1
                    
Install as a Cake Tool

NET-Database query layer

A thin database layer to make more easy queries (.NET C#)

<a href="https://www.nuget.org/packages/NET.Database.QueryLayer"><img src="https://img.shields.io/nuget/v/NET.Database.QueryLayer.svg" alt="NuGet Version" /></a> <a href="https://www.nuget.org/packages/NET.Database.QueryLayer"><img src="https://img.shields.io/nuget/dt/NET.Database.QueryLayer.svg" alt="NuGet Download Count" /></a>

What is it?

NET-Database query layer components will be a useful addition to any application or website to create SQL queries to your database in a OO way. It supports the majority of different database types such as SQL Server® MySQL® o PostgreSQL®, but is easy extend to any ADO.NET provider.

Supported frameworks

Compatible with every framework supported by Slapper.AutoMapper 3.0.0:

  • .NET Standard 2.0 and 2.1
  • .NET Framework 4.7 and 4.8
  • .NET 6, 8, 9 and 10

Is this an ORM?

No, NET-Database query layer only allow query to database and not include any persistence model.

Why use NET-Database query layer if I am using EntityFramework®, NHibernate® or ADO.NET?

In CQRS architectures, the persistence model (command model) is separated to query model. NET- Database query layer is perfect to implement the query model, allowing it to maintain in a different process or running on different hardware.

More info about CQRS: http://martinfowler.com/bliki/CQRS.html

Other application is in Command-query separation principle: every method should either be a command that performs an action, or a query that returns data to the caller, but not both. .

More info about Command-query separation http://martinfowler.com/bliki/CommandQuerySeparation.html

Configure NET-Database query layer. App.config or Web.config

Microsoft SQL Server®

<configSections>
	<section name="daProviders" type="ADO.Query.Helper.DataAccessSectionHandler, ADO.Query.Core" />
</configSections>

<daProviders>
	<daProvider alias="MsSQL" type="ADO.Query.Helper.MsSql, ADO.Query.Core" connectionStringName="DBConnection" />
</daProviders>

<connectionStrings>
    <add name="DBConnection" connectionString="YOUR_CONNECTION_STRING" />
</connectionStrings> 

MySQL®

<configSections>
	<section name="daProviders" type="ADO.Query.Helper.DataAccessSectionHandler, ADO.Query.Core" />
</configSections>

<daProviders>
	<daProvider alias="MySQL" type="ADO.Query.Helper.MySql, ADO.Query.Core" connectionStringName="DBConnection" />
</daProviders>

<connectionStrings>
	<add name="DBConnection" connectionString="YOUR_CONNECTION_STRING" />
</connectionStrings> 

PostgreSQL®

<configSections>
	<section name="daProviders" type="ADO.Query.Helper.DataAccessSectionHandler, ADO.Query.Core" />
</configSections>

<daProviders>
	<daProvider alias="PgSQL" type="ADO.Query.Helper.PgSql, ADO.Query.Core" connectionStringName="DBConnection" />
</daProviders>

<connectionStrings>
	<add name="DBConnection" connectionString="YOUR_CONNECTION_STRING" />
</connectionStrings> 

How create instance of QueryRunner?

NET-Database query layer implement factory pattern to determinate which type of class to create.

var queryRunner = QueryRunner.CreateHelper("MsSQL", new QueryMapper());

The first paramaeter allow to specificate the alias used in the configuration: this parameter identify the instance to create. The second parameter allow to pass the query mapper object used to convert query resulto to DTO.

Using dependency injection (Unity)

var container = new UnityContainer();
container.RegisterType<IQueryRunner>(new InjectionFactory(c => QueryRunner.CreateHelper("MsSQL", new QueryMapper())));

var queryRunner = container.Resolve<IQueryRunner>();

An override method of factory can be used without query mapper object parameter, but in this case your query will be used only to return Datatable, datareader or scalar result.

var queryRunner = QueryRunner.CreateHelper("MsSQL");

Query Model

All query model implement ISqlQuery Interface

public interface ISqlQuery
{
    string Expression { get; }
    IDictionary<string, object> Parameters { get; }
}
  • Expression: SQL string to execute in database
  • Parameters: Name and value collection with parameters included in SQL string

My first Query using NET-Database query layer

class QuerySimple : ISqlQuery
{
    public QuerySimple()
    {
        this.Expression = "select id as Id, name as Name from table_in_database";
    }

    public string Expression { get; private set; }

    public IDictionary<string, object> Parameters { get; private set; }
}

Get DataTable

var queryRunner = QueryRunner.CreateHelper("MsSQL");
var dt = queryRunner.ExecuteDataTable(new QuerySimple());

Get Datareader

var queryRunner = QueryRunner.CreateHelper("MsSQL");
var dr = queryRunner.ExecuteReader(new QuerySimple());

Get the first column of the first row in the query result

var queryRunner = QueryRunner.CreateHelper("MsSQL");
var id = queryRunner.ExecuteScalar<int>(new QuerySimple());

Query model with parameters

class QueryWithParameters : ISqlQuery
{
    public QueryWithParameters(int id, string name)
    {
       this.Expression = "select id as Id, name as Name from table_in_database where id = @id and name = @name";

       this.Parameters = new Dictionary<string, object>
       {
          {"id", id},
          {"name", name}
       };
    }

    public string Expression { get; private set; }

    public IDictionary<string, object> Parameters { get; private set; }
 }

Get DTO from query result

NET-Database query layer use Slapper.AutoMapper https://github.com/SlapperAutoMapper/Slapper.AutoMapper to convert dynamic data into static types and populate complex nested child objects returned by queries.

Transform Query result to DTO

Example

public class SimpleDto
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Return IEnumerable<DTO>

var result = queryRunner.Execute<SimpleDto>(new QuerySimple()).ToList();

Return Single DTO

var singleDto = queryRunner.Execute<SimpleDto>(new QuerySimple()).ToSingle();

Return First or default

var singleDto = queryRunner.Execute<SimpleDto>(new QuerySimple()).ToFirstOrDefault();

Paged result

To paged result, must implement the interface ISqlPagedQuery

public interface ISqlPagedQuery : ISqlQuery
{
   string SqlCount { get; }
   int Page { get; }
   int ItemsPerPage { get; }
}

A sample pagination SQL query

public class QueryPageSpecification : ISqlPagedQuery
{
    public QueryPageSpecification(int page, int itemsPerPages)
    {
        this.Expression = "select...";
        this.SqlCount = "select count(*)...";

        this.ItemsPerPage = itemsPerPages;
        this.Page = page;
    }

    public string Expression { get; private set; }
    public IDictionary<string, object> Parameters { get; private set; }

    public string SqlCount { get; private set; }

    public int Page { get; private set; }
    public int ItemsPerPage { get; private set; }
}

Execute paged query

var pagedList = queryRunner.Execute<SimpleDto>(new QueryPageSpecification(page: 1, itemsPerPages: 2));

and will return a PageSqlResult<T>

public class PageSqlResult<T>
{
    public long TotalItems { get; set; }
    public long TotalPages { get; set; }
    public long CurrentPage { get; set; }

    public IEnumerable<T> Result { get; set; }
}

Store procedure support

Store procedure is not supported at this moment.

Enjoy 😉

License

MIT License: http://opensource.org/licenses/MIT

Copyright (c) 2015, Omar del Valle ( http://odelvalle.com ) All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 is compatible.  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.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 is compatible.  net471 was computed.  net472 was computed.  net48 is compatible.  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

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.1 58 11/23/2026
3.0.0 61 11/23/2026
2.3.0 990 4/13/2021
2.1.4 856 6/20/2020
2.1.3 732 6/20/2020
1.0.0.2 1,707 2/19/2016
1.0.0.1 1,470 1/19/2016

- Fix vulnerable dependencies: Npgsql 4.1.13 and System.Text.Encodings.Web 4.7.2
- Include README in the package
- Fix assembly name in README configuration examples (ADO.Query.Core)
- Update Slapper.AutoMapper to 3.0.0
- Breaking: Slapper 3.0.0 scopes instance caches to each mapping call, eliminating cross-query instance reuse and cache collisions. The default QueryMapper no longer shares or merges instances across queries, even for the same destination type and Id; keepCache and RemoveCacheAfterExecuteQuery no longer control cross-query instance reuse.
- Multi-target: netstandard2.0, netstandard2.1, net47, net48, net6.0, net8.0, net9.0, net10.0