Stackoverflow/dapper的Dapper-Extensions用法(一)
Dapper-Extensions
Dapper Extensions is a small library that complements Dapper by adding basic CRUD operations (Get, Insert, Update, Delete) for your POCOs. For more advanced querying scenarios, Dapper Extensions provides a predicate system. The goal of this library is to keep your POCOs pure by not requiring any attributes or base class inheritance.
github:https://github.com/tmsmith/Dapper-Extensions
- Dapper是一个开源轻的量级的orm,他的优点和用法在之前写的博客中有提到。可是它只支持带sql语句的CRUD。
- Dapper-Extensions也是一个开源库,他在Dapper的基础上封装了基本的CRUD操作,使得一些简单的数据库操作可以不用自己写sql语句。使用起来更方面。
若不熟悉Dapper的,请移步:
- 《Dapper的基本使用》:http://www.cnblogs.com/Sinte-Beuve/p/4231053.html
- 《(扩展)利用Dapper ORM搭建三层架构》:http://www.cnblogs.com/Sinte-Beuve/p/4230943.html
后续文档翻译
下面是对他的用法的描述,也就是对项目文档的翻译。如果读者英文不错可以直接看原版文档,见github。
Introduction
Dapper Extensions是github上的一个开源库是对StackOVerflow开发的Dapper ORM的一个扩展。它增加了基础的CRUD操作((Get, Insert, Update, Delete)),对更高级的查询场景,该类库还提供了一套谓词系统。它的目标是保持POCOs的纯净,不需要额外的attributes和类的继承。
自定义映射请参见 ClassMapper
Features
- 零配置
- 自动映射POCOs的CRUD操作
- GetList, Count等方法可以用于更高级的场景。
- GetPage for returning paged result sets.支持分页
- 自动支持Guid和Integer类型的主键,(也可以手动指定其他键类型)
- 通过使用ClassMapper可以使保证POCOs纯净。 (Attribute Free!)
- 可以通过使用ClassMapper来自定义entity-table映射
- 支持联合主键
- POCO类名默认与数据表名相匹配,也可以自定义
- 易于使用的 Predicate System适用于高级场合
- 在生成SQL语句时正确转义表名和类名 (Ex: SELECT FirstName FROM Users WHERE Users.UserId = @ UserId_0)
- 覆盖单元测试(覆盖了150+个单元测试)
Naming Conventions(命名约定)
- POCO类名与数据库中表名匹配,多元化(Pluralized)的表名(暂时理解为别名吧)可以通过PlurizedAutoClassMapper来自定义。
- POCO类中属性名和数据库表中的类名匹配。
- 暂约定主键被命名为Id.使用其他主键需要自定义映射(ClassMapper)。
Installation
Nuget:
http://nuget.org/List/Packages/DapperExtensions
PM> Install-Package DapperExtensions
Examples
pernson POCO的定义
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool Active { get; set; }
public DateTime DateCreated { get; set; }
}
Get Operation
using (SqlConnection cn = new SqlConnection(_connectionString))
{
cn.Open();
int personId = 1;
Person person = cn.Get<Person>(personId);
cn.Close();
}
Simple Insert Operation
using (SqlConnection cn = new SqlConnection(_connectionString))
{
cn.Open();
Person person = new Person { FirstName = "Foo", LastName = "Bar" };
int id = cn.Insert(person);
cn.Close();
}
Advanced Insert Operation (Composite Key)复合主键
//返回dynamic类型,若主键为单,返回主键值,若主键为复合的,返回IDictionary<string,object>
public static dynamic Insert<T>(this IDbConnection connection, T entity, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
public class Car
{
public int ModelId { get; set; }
public int Year { get; set; }
public string Color { get; set; }
}
using (SqlConnection cn = new SqlConnection(_connectionString))
{
cn.Open();
Car car = new Car { Color = "Red" };
//返回o
var multiKey = cn.Insert(car);
cn.Close();
int modelId = multiKey.ModelId;
int year = multiKey.Year;
}
Simple Update Operation
using (SqlConnection cn = new SqlConnection(_connectionString))
{
cn.Open();
int personId = 1;
Person person = _connection.Get<Person>(personId);
person.LastName = "Baz";
cn.Update(person);
cn.Close();
}
Simple Delete Operation
using (SqlConnection cn = new SqlConnection(_connectionString))
{
cn.Open();
Person person = _connection.Get<Person>(1);
cn.Delete(person);
cn.Close();
}
GetList Operation (with Predicates)
using (SqlConnection cn = new SqlConnection(_connectionString))
{
cn.Open();
var predicate = Predicates.Field<Person>(f => f.Active, Operator.Eq, true);
IEnumerable<Person> list = cn.GetList<Person>(predicate);
cn.Close();
}
Generated SQL
SELECT
[Person].[Id]
, [Person].[FirstName]
, [Person].[LastName]
, [Person].[Active]
, [Person].[DateCreated]
FROM [Person]
WHERE ([Person].[Active] = @Active_0)
Count Operation (with Predicates)
using (SqlConnection cn = new SqlConnection(_connectionString))
{
cn.Open();
var predicate = Predicates.Field<Person>(f => f.DateCreated, Operator.Lt, DateTime.UtcNow.AddDays(-5));
int count = cn.Count<Person>(predicate);
cn.Close();
}
Generated SQL
SELECT
COUNT(*) Total
FROM [Person]
WHERE ([Person].[DateCreated] < @DateCreated_0)
Stackoverflow/dapper的Dapper-Extensions用法(一)的更多相关文章
- 【ORM】关于Dapper的一些常见用法
引言 Dapper是.Net平台下一款小巧玲珑的开源Orm框架,简单实用的同时保持高性能,非常适合我这种喜欢手写SQL的人使用,下面介绍一下如何使用Dapper. 相关资料 Dapper的GitHub ...
- Dapper学习 - Dapper的基本用法(一) - 查询
上一篇, 提到Query<Test>查询的时候, 如果Test中包含自定义class, Dapper不会给自定义class完成映射, 而是直接给null, 其实是可以实现的, 答案就在下面 ...
- Dapper学习 - Dapper的基本用法(三) - CUD
之前介绍了Dapper的查询, 存储过程, 函数的调用, 接下来要说一下Dapper的增删改, 其实Dapper的增删改, 都是同一种模式的. 我就不分开介绍了, 直接在一个例子上展现好了. var ...
- Dapper学习 - Dapper的基本用法(二) - 存储过程/函数
上一篇貌似少介绍了自定义函数和存储过程, 因为这两个也可以使用查询的方式来实现功能, 这一篇就补上 一.自定义函数的创建和调用 (mysql的) Delimiter $$ drop function ...
- Dapper学习 - Dapper.Rainbow(三) - Read
前面已经介绍了新增/修改/删除了, 接下来介绍一下Rainbow的Read方法. 一.Read -- Rainbow原生 1. 先看测试代码 var conStr = ConfigurationMan ...
- Dapper学习 - Dapper.Rainbow(二) - Update/Delete
上一篇介绍了Rainbow的Create方法, 这里就来介绍一下Update方法吧, 毕竟新增和修改是双胞兄弟嘛. 一.Update 1. 测试代码: var conStr = Configurati ...
- Dapper学习 - Dapper.Rainbow(一) - Create
Dapper这个ORM有许多扩展, 我自己用过两种, 也算是比较主流的两种, Rainbow和Extension, 这里就先介绍下Rainbow吧, 毕竟这个先用, 当然, 由于我使用的是mysql数 ...
- Dapper:安装Dapper时报错
今天在使用VS 2013安装Dapper的时候报错,具体报错信息如下: 经过网上查找错误原因,发现是安装的Dapper版本过高,.Net Framework版本不支持该版本的Dapper. 解决方案: ...
- Dapper扩展Dapper.Common框架 Linq To Sql 底层源码.net ORM框架
源代码:https://github.com/1448376744/Dapper.CommonNUGET: Dapper.CommonQQ群:642555086 一.基本结构,此处可用委托,或动态代理 ...
随机推荐
- django 在字符串[str(list)]中精确查找
1.问题描述 1.1表结构 1.2问题 ref_list为id列表的字符串,需要从ref_list中找出包含指定id的数据(eg id=8).如果实用models.objects.filter(ref ...
- Python之路Day21-自定义分页和cookie
本节知识点概要 1.URL 2.views - 请求其他信息 - 装饰器 3.Templates - 母版 - 自定义 4.Models操作 5.分页(自定义分页) 6.cookie 7.sessio ...
- TP5验证规则使用
定义验证器类: namespace app\index\validate; use think\Validate; class User extends Validate { protected $r ...
- angular学习之路(一)
angular是什么? angular是一个用于设计动态web应用的结构框架! 它不仅仅是一个JavaScript框架,他的核心其实是对HTML标签的增强. 何为HTML标签的增强?其实就是使用标签完 ...
- SQL Server 2012大幅增强T-SQL
SQL Server 2012对T-SQL进行了大幅增强,其中包括支持ANSI FIRST_VALUE和LAST_VALUE函数,支持使用FETCH与OFFSET进行声明式数据分页,以及支持.NET中 ...
- 解读ASP.NET 5 & MVC6系列(7):依赖注入
在前面的章节(Middleware章节)中,我们提到了依赖注入功能(Dependency Injection),ASP.NET 5正式将依赖注入进行了全功能的实现,以便开发人员能够开发更具弹性的组件程 ...
- c#语言-高阶函数
介绍 如果说函数是程序中的基本模块,代码段,那高阶函数就是函数的高阶(级)版本,其基本定义如下: 函数自身接受一个或多个函数作为输入. 函数自身能输出一个函数,即函数生产函数. 满足其中一个条件就可以 ...
- sysbench 压力测试
200 ? "200px" : this.width)!important;} --> 介绍 sysbench是一个模块化.跨平台.多线程基准测试工具,主要用于测试不同系统参 ...
- [Oracle](不会的是三炮)把状态列表作为存储过程参数这件小事
抱歉用了这么渣的标题,其实是一个很简单而且很常见的需求:假设我们有一个学生表,它有一个状态字段: create table T_STU ( STU_ID ) not null, NAME ), COD ...
- EF:自定义Oracle的映射类型
oracle在DB First模式下,int类型的字段会自动映射为decmial类型的属性. 我们可以通过自定义类型映射进行“纠整”. 在app.config 自定义映射规则: <oracle. ...