QueryDictionary 2.1.0

dotnet add package QueryDictionary --version 2.1.0
                    
NuGet\Install-Package QueryDictionary -Version 2.1.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="QueryDictionary" Version="2.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="QueryDictionary" Version="2.1.0" />
                    
Directory.Packages.props
<PackageReference Include="QueryDictionary" />
                    
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 QueryDictionary --version 2.1.0
                    
#r "nuget: QueryDictionary, 2.1.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.
#:package QueryDictionary@2.1.0
                    
#: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=QueryDictionary&version=2.1.0
                    
Install as a Cake Addin
#tool nuget:?package=QueryDictionary&version=2.1.0
                    
Install as a Cake Tool

QueryDictionary

Introduction

QueryDictionary is a library for managing and loading SQL queries from various sources such as embedded resources, folders, and XML files. It provides a convenient way to organize and access your SQL queries in a structured manner.

Prerequisites

This package targets .NET Standard 2.1, so it should work with most .NET platforms.

Installation

Use NuGet to install the package. Use can use the UI, or use the following command in the package manager console:

Install-Package QueryDictionary

or

use the following command in the dotnet CLI:

dotnet add package QueryDictionary

Contributing

If you want to contribute, please create a pull request. I will review it as soon as possible. Use visual studio 2022 version 17.8 or later to build this library. The main library targets NETStandard 2.0, but the tests use .NET 8.0.

Author

This library was created by Jan Geert Hek, a software developer from the Netherlands. You can find more information about me on my LinkedIn page.

License

This library is licensed under the MIT License - see the LICENSE file for details.

Part 1. Loading queries from various sources

Loading Queries from an Embedded Resource

You can load SQL queries embedded in an assembly using the QueryDictionaryEmbedded class.

using QueryDictionary;

// Load queries from the assembly containing the specified type
var queryDictionary = QueryDictionaryEmbedded.LoadAssemblyOfType<SomeType>(namespacePrefix: "YourNamespace.Queries");

// Access a query by its key
string query = queryDictionary["GetOneClient"];

Loading Sql Server Query files from an Embedded Resource

You can also load SQL queries embedded in an assembly with a specific namespace prefix.

using QueryDictionary;

// Load queries from the assembly containing the specified type with SQL queries
var queryDictionary = QueryDictionaryEmbedded.LoadAssemblyOfTypeWithSqlQueries<SomeType>(namespacePrefix: "YourNamespace.Queries");

// Access a query by its key
string query = queryDictionary["GetOneClient"];

Loading Queries from a Folder

You can load SQL queries from a folder using the QueryDictionaryFolder class.

using QueryDictionary;

// Load queries from a folder
var queryDictionary = QueryDictionaryFolder.LoadSqlServerQueryFolder("path/to/your/folder");

// Access a query by its key
string query = queryDictionary["GetOneClient"];

Loading Queries from an XML File

You can load SQL queries from an XML file using the QueryDictionaryXml class.

using QueryDictionary;

// Load queries from an XML file
var queryDictionary = QueryDictionaryXml.ForFile("path/to/your/queries.xml");

// Access a query by its key
string query = queryDictionary["GetOneClient"];

Part 2. Using the QueryDictionary

Use your DI to inject the QueryDictionary

// Load queries from embedded files.
var queryDictionary = QueryDictionaryEmbedded.LoadAssemblyOfTypeWithSqlQueries<SomeType>(namespacePrefix: "YourNamespace.Queries");

// Register the query dictionary as a singleton service
services.AddSingleton<IQueryDictionary>(queryDictionary);

Use the QueryDictionary in your services

QueryDictionary works well with Dapper, a micro ORM for .NET. You can use the QueryDictionary to load SQL queries and execute them with Dapper.

public SomeController(IQueryDictionary queryDictionary)
{
    _queryDictionary = queryDictionary;
}

[HttpGet("something/{id}")]
public IActionResult GetSomething(int id)
{
    string query = _queryDictionary["GetSomething"];

    using (var connection = new SqlConnection(...))
    {
        connection.Open();
        var result = connection.Query(query, new { MyThingId = id });
        return Ok(result);
    }
}

Credits

The icon used has been designed by Flaticon.com

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.  net9.0 was computed.  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 was computed.  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 netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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.
  • .NETStandard 2.1

    • No dependencies.

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
2.1.0 157 9/17/2024
2.0.11 122 9/17/2024
2.0.10 120 9/16/2024
2.0.9 121 9/16/2024