zzzprojects / awesome-dapper Goto Github PK
View Code? Open in Web Editor NEWAwesome Dapper third party-libraries, documentation, and tutorials!
License: MIT License
Awesome Dapper third party-libraries, documentation, and tutorials!
License: MIT License
https://github.com/Drizin/DapperQueryBuilder/
Dapper Query Builder using String Interpolation and Fluent API
We all love Dapper and how Dapper is a minimalist library.
This library is a wrapper around Dapper mostly for helping building dynamic SQL queries and commands (as a replacement for Dapper.SqlBuilder).
It's based on 2 fundamentals:
Sample usage:
int maxPrice = 1000;
int maxWeight = 15;
string search = "%Mountain%";
var products = cn.QueryBuilder($@"
SELECT ProductId, Name, ListPrice, Weight
FROM [Product]
WHERE [ListPrice] <= {maxPrice}
AND [Weight] <= {maxWeight}
AND [Name] LIKE {search}
ORDER BY ProductId").Query<Product>();
Query<>
invokes Dapper and automatically passes the underlying SQL statement and Parameters. Additional parameters (transaction, commandTimeout) can still be passed. Underlying SQL will be fully parameterized like:
SELECT ProductId, Name, ListPrice, Weight
FROM [Product]
WHERE [ListPrice] <= @p0
AND [Weight] <= @p1
AND [Name] LIKE @p2
ORDER BY ProductId
You can also start with an initial query and then append dynamic conditions:
var query = cn.QueryBuilder($"SELECT * FROM [Product] WHERE 1=1");
query.Append($"AND Name LIKE {productName}");
query.Append($"AND ProductSubcategoryID = {subCategoryId}");
var products = query.Query<Product>();
Or leaving filters to be described later:
var cn = new SqlConnection(connectionString);
// You can build the query manually and just use QueryBuilder to replace "where" filters (if any)
var q = cn.QueryBuilder(@"SELECT ProductId, Name, ListPrice, Weight
FROM [Product]
/**where**/
ORDER BY ProductId
");
// You just pass the parameters as if it was an interpolated string,
// and QueryBuilder will automatically convert them to Dapper parameters (injection-safe)
q.Where($"[ListPrice] <= {maxPrice}");
q.Where($"[Weight] <= {maxWeight}");
q.Where($"[Name] LIKE {search}");
// Query() will automatically build your query and replace your /**where**/ (if any filter was added)
var products = q.Query<Product>();
Mixing and/or filters in structured way:
q.Where(new Filters(Filters.FiltersType.OR)
{
new Filters(Filters.FiltersType.AND)
{
$"[Weight] <= {maxWeight}",
$"[Weight] >= {minWeight}",
}
new Filter($"[Name] LIKE {search}")
});
Chained-methods (Fluent API) if you have something like a QueryBuilder exposed in UI to your end-users:
var q = cn.QueryBuilder()
.Select($"ProductId")
.Select($"Name")
.Select($"ListPrice")
.Select($"Weight")
.From($"[Product]")
.Where($"[ListPrice] <= {maxPrice}")
.Where($"[Weight] <= {maxWeight}")
.Where($"[Name] LIKE {search}")
.OrderBy($"ProductId"); // there's also GROUP BY, HAVING, ORDERBY, and PAGING (OFFSET/FETCH NEXT)
var products = q.Query<Product>();
More examples and features here
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.