Dapper.Lite extension
Dapper itself is not convenient and requires extensions. Dapper has nothing to do with specific databases. If it is encapsulated, the codes for different databases will be different. Dapper.Lite supports the most databases at the lowest cost and is designed to be simple, easy to use, stable and reliable.
Dapper.Lite has recently been refactored and streamlined, as well as a version that does not rely on Dapper, LiteSql. These two ORMs are basically completed, please recommend them.
Dapper.Lite is a Dapper extension that supports Lambda expressions for single-table queries and SQL splicing query conditions. It aims to provide everyone with an easy-to-use, stable and reliable ORM that supports Oracle, MSSQL, MySQL, PostgreSQL, SQLite, Access, ClickHouse and other databases.
Its features include:
- Single table query supports Lambda
List list = session.Queryable().Where(t => t.Id <= 20 && t.Remark.Contains("Test") ).ToList();
Single table query does not require writing SQL.
In this reconstruction, even complex functions such as table queries and subqueries have been deleted to reduce the possibility of bugs.
- Mainly based on SQL, no matter what kind of database, it is written in the following way. This is the most common usage
Some databases are prefixed with the @ symbol, and some are with the : symbol, but the ClickHouse database is different and is a bit more troublesome to write, so it is unified here.
Session means a database session, mainly for database transactions. If there is no transaction, you can directly db.Sql
List list = session.Sql("select * from sys_user where id <= @Id and remark like @Remark", 20, "%test%"). ToList(); //Parameters come in order, one or two will not be dazzled easily
or
List list = session.Sql("select * from sys_user where id <= @Id and remark like @Remark", new { Id = 20, Remark = " %Test%" }).ToList(); //If there are many parameters, just write it like this
Then splicing:
.Append("and name like @Name", "%test%");
- SQL splicing query conditions support Lambda expressions
List kpTaskList = await session
.Sql(@"
select t.*, m.model_start as ModelStart, m.model_end as ModelEnd
from kp_task t
left join kp_model m on m.model_id=t.model_id")
.Where(t => t.IsDel == 0)
.Where(t => new int?[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }.Contains(t.OpeType))
.Where(m => m.ModelStart <= DateTime.Now && DateTime.Now <= m.ModelEnd)
.ToListAsync();
Since you have written SQL, there must be shortcomings. For example, if the table name is changed, the SQL must also be changed.
It is more convenient to write Lambda for Where condition than to write SQL.
If you plan to use Dapper and want to find an extension, you might as well try Dapper.Lite. Even if there are bugs, the source code of Dapper.Lite is something you can hold on to.
Source code address
https://gitee.com/s0611163/Dapper.Lite
https://github.com/0611163/Dapper.Lite