SqlKata.QueryMan
1.0.5
See the version list below for details.
dotnet add package SqlKata.QueryMan --version 1.0.5
NuGet\Install-Package SqlKata.QueryMan -Version 1.0.5
<PackageReference Include="SqlKata.QueryMan" Version="1.0.5" />
paket add SqlKata.QueryMan --version 1.0.5
#r "nuget: SqlKata.QueryMan, 1.0.5"
// Install SqlKata.QueryMan as a Cake Addin #addin nuget:?package=SqlKata.QueryMan&version=1.0.5 // Install SqlKata.QueryMan as a Cake Tool #tool nuget:?package=SqlKata.QueryMan&version=1.0.5
QueryMan
Simple and very concrete, fluent query manager based on SQLKata and Dapper.
Features
- Fluent queries (you have more freedom than with
Linq
,HQL
orQueryOver
) - Save/Update/Delete entities
- Transactions
- Pagination
Installation
Using dotnet cli
$ dotnet add package SqlKata.QueryMan
Using Nuget Package Manager
PM> Install-Package SqlKata.QueryMan
If you need transaction filter
$ dotnet add package SqlKata.QueryMan.AspNetCore
or
PM> Install-Package SqlKata.QueryMan.AspNetCore
How to use
Init QueryMan (freeman way):
using QueryMan;
using SqlKata.Compilers;
using System.Data.SQLite;
// Method 1
using (var Db = new QueryRunner(new SQLiteConnection(ConnectionString), new SqliteCompiler()))
{
// Man, do you queries here
}
// Method 2
Db = new QueryRunner(new SQLiteConnection(ConnectionString), new SqliteCompiler());
// Man, do you queries here
Db.Dispose();
Please remember to always
Dispose
your QueryRunner instance when you finish.
Init QueryMan (asp.net core):
using QueryMan;
using QueryMan.AspNetCore;
using SqlKata.Compilers;
using System.Data.SQLite;
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.ConfigureQueryMan(new QueryManConfig()
{
Connection = (s) => new SQLiteConnection(s.GetRequiredService<IConfiguration>().GetConnectionString("DefaultConnection")),
Compiler = (s) => new SqliteCompiler(),
});
// Optionally you can globally enable database transactions against every http request.
// As alternative use [TransactionActionFilter] attribute against your controllers or actions.
// It will create a new database transaction before every request
// and perform a commit when the action (request) executed without erros, otherwise it will rollback all db changes.
services.AddControllers(o => o.Filters.Add<TransactionActionFilter>());
}
}
// Your controller
public class CustomerController : ControllerBase
{
private readonly QueryRunner _db;
public PageBController(QueryRunner db)
{
_db = db;
}
[HttpGet]
public Customer Get(string id)
{
return _db.Get<Customer>(id);
}
[HttpPost]
[TransactionActionFilter] // It will execute sql queries inside the transaction.
public void Put(Customer customer)
{
_db.SaveOrUpdate(customer);
}
}
The preffered way to have an entity against your datatable:
[Table("customers")]
public class Customer
{
protected Customer() // Used by ObservableProxy to create a proxy object and filling with data from database.
{
}
public Customer(string name) // Used by developers to create a new object before it will be saved to the db. See how the `mandatory Name` is implemented by `protected set`.
{
SetName(name);
}
public virtual void SetName(string name)
{
if (string.IsNullOrEmpty(name))
throw new ArgumentNullException(nameof(name));
Name = name;
}
[Key] // Primary key column
public virtual string Id { get; protected set; } // "virtual" allows proxy to override properties, otherwise this property won't be treated as a database column and won't pull or persist any data when you pull data as proxies.
[Column("CustomerName")] // Custom column in the database (by defaut it will use a property name).
public virtual string Name { get; protected set; }
}
Save to db:
var customer = new Customer("Customer 1");
Db.BeginTransaction();
await Db.SaveOrUpdateAsync(customer);
Db.Commit();
customer = await Db.GetAsync<Customer>(customer.Id);
Assert.Equal("Customer 1", customer.Name);
Get by Id:
var customer = await Db.GetAsync<Customer>(id);
Get from db:
Customer customer = null;
(string CustomerId, string CustomerName) queryResult = default;
var query = Db.Query<Customer>()
.Select(() => customer.Id, () => queryResult.CustomerId)
.Select(() => customer.Name, () => queryResult.CustomerName)
;
var result = await Db.ToListNoProxy<(string CustomerId, string CustomerName)>(query);
AssertHelper.CollectionContainsAll(result,
item => item.CustomerName == "Customer 1"
);
Get from db with GroupBy:
Contact contact = null;
Customer customer = null;
(string CustomerName, int ContactCount) queryResult = default;
var query = Db.Query(() => customer)
.LeftJoin(() => contact, () => contact.CustomerId, () => customer.Id)
.Select(() => customer.Name, () => queryResult.CustomerName)
.SelectCount(() => contact.Id, () => queryResult.ContactCount)
.GroupBy()
;
var result = await Db.ToListNoProxy<(string CustomerName, int ContactCount)>(query);
AssertHelper.CollectionContainsAll(result,
item => item.CustomerName == "Customer 1a" && item.ContactCount == 1,
item => item.CustomerName == "Customer 2" && item.ContactCount == 0
);
Batch Update:
Customer customer = null;
var query = Db.SelectAll(() => customer);
var result = await Db.ToList<Customer>(query);
foreach (var (c, i) in result.Select((c, i) => (c, i)))
{
c.SetName($"Customer No {i+1}");
}
Db.BeginTransaction();
await Db.SaveOrUpdateAsync(result);
Db.Commit();
Nuget
How to contribute
If you have any issues please provide us with Unit Test Example.
Please create an issue ticket to become a contributer.
Donations
Donate with nano.
Thank you!
Product | Versions 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. |
-
.NETStandard 2.0
- CaseExtensions (>= 1.1.0)
- Castle.Core (>= 4.4.1)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 2.0.0)
- SqlKata (>= 2.3.7)
- SqlKata.Execution (>= 2.3.7)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on SqlKata.QueryMan:
Package | Downloads |
---|---|
SqlKata.QueryMan.AspNetCore
Fluent query manager based on SQLKata. Transaction Action Filter addon used in ASP.NET applications. |
|
SqlKata.QueryMan.AspNetCore.Identity
Fluent query manager based on SQLKata. ASP.NET Identity Stores implementation. |
GitHub repositories
This package is not used by any popular GitHub repositories.