前言
这是针对dapper的一个扩展,支持lambda表达式的写法,链式风格让开发者使用起来更加优雅、直观。现在暂时只有MsSql的扩展,也没有实现事务的写法,将会在后续的版本补充。
这是个人业余的开源小项目,如果大家有更好的实现方式和好的建议欢迎拍砖
本项目已经在github上开源了:Sikiro.DapperLambdaExtension.MsSql
去年写了《整理自己的.net工具库》,里面提供的源码重新发布到了github并用新的项目名Sikiro.Tookits
这两个项目都发布到Nuget上了,可以在Nuget搜索Sikiro可以全部查看到
另外该项目会用到一些表达式树的知识,如果有兴趣的朋友可以先去了解,我之前也写过一篇简单的文章《表达式树的解析.》
下面是简单的使用介绍
开始
Nuget
你可以运行以下下命令在你的项目中安装 Sikiro.DapperLambdaExtension.MsSql。
PM> Install-Package Sikiro.DapperLambdaExtension.MsSql
现在暂时只有MsSql的扩展,也没有实现事务的写法,将会在后续的版本补充
SqlConnection
var con = new SqlConnection("Data Source=192.168.13.46;Initial Catalog=SkyChen;Persist Security Info=True;User ID=sa;Password=123456789");
定义User
[Table("SYS_USER")]public class SysUser{ /// <summary> /// 主键 /// </summary> [Key] [Required] [StringLength(32)] [Display(Name = "主键")] [Column("SYS_USERID")] public string SysUserid { get; set; } /// <summary> /// 创建时间 /// </summary> [Required] [Display(Name = "创建时间")] [Column("CREATE_DATETIME")] public DateTime CreateDatetime { get; set; } /// <summary> /// 邮箱 /// </summary> [Required] [StringLength(32)] [Display(Name = "邮箱")] [Column("EMAIL")] public string Email { get; set; } /// <summary> /// USER_STATUS /// </summary> [Required] [Display(Name = "USER_STATUS")] [Column("USER_STATUS")] public int UserStatus { get; set; } }
Insert
con.CommandSet<SysUser>().Insert(new SysUser{ CreateDatetime = DateTime.Now, Email = "287245177@qq.com", SysUserid = Guid.NewGuid().ToString("N"), UserName = "chengong", });
当不存在某条件记录Insert
con.CommandSet<SysUser>().IfNotExists(a => a.Email == "287245177@qq.com").Insert(new SysUser { CreateDatetime = DateTime.Now, Email = "287245177@qq.com", SysUserid = Guid.NewGuid().ToString("N"), UserName = "chengong", });
UPDATE
您可以根据某个条件把指定字段更新
con.CommandSet<SysUser>().Where(a => a.Email == "287245177@qq.com").Update(a => new SysUser { Email = "123456789@qq.com" });
也可以根据主键来更新整个实体字段信息
User.Email = "123456789@qq.com"; condb.CommandSet<SysUser>().Update(User);
DELETE
您可以根据条件来删除数据
con.CommandSet<SysUser>().Where(a => a.Email == "287245177@qq.com").Delete()
QUERY
GET
获取过滤条件的一条数据(第一条)
con.QuerySet<SysUser>().Where(a => a.Email == "287245177@qq.com").Get()
TOLIST
当然我们也可以查询出符合条件的数据集
con.QuerySet<SysUser>().Where(a => a.Email == "287245177@qq.com").OrderBy(b => b.Email).Top(10).Select(a => a.Email).ToList();
PAGELIST
还有分页
con.QuerySet<SysUser>().Where(a => a.Email == "287245177@qq.com") .OrderBy(a => a.CreateDatetime) .Select(a => new SysUser { Email = a.Email, CreateDatetime = a.CreateDatetime, SysUserid = a.SysUserid }) .PageList(1, 10);
UPDATESELECT
先更新再把结果查询出来
con.QuerySet<SysUser>().Where(a => a.Email == "287245177@qq.com") .OrderBy(a => a.CreateDatetime) .Select(a => new SysUser { Email = a.Email }) .UpdateSelect(a => new SysUser { Email = "2530665632@qq.com" });
事务功能
con.Transaction(tc => { var sysUserid = tc.QuerySet<SysUser>().Where(a => a.Email == "287245177@qq.com").Select(a => a.SysUserid).Get(); tc.CommandSet<SysUser>().Where(a => a.SysUserid == sysUserid).Delete(); tc.CommandSet<SysUser>().Insert(new SysUser { CreateDatetime = DateTime.Now, Email = "287245177@qq.com", Mobile = "13536059332", RealName = "大笨贞", SysUserid = Guid.NewGuid().ToString("N"), UserName = "fengshuzhen", UserStatus = 1, UserType = 1, Password = "asdasdad" }); });
SQL
sql的查询方案仍然开放
using Dapper; ..... con.GetConnection().QuerySingle<SysUser>("SELECT * FROM SYS_USER");
最后来一个完整的DEMO
using (var con = new SqlConnection("Data Source=192.168.13.46;Initial Catalog=SkyChen;Persist Security Info=True;User ID=sa;Password=123456789")) { con.CommandSet<SysUser>().Insert(new SysUser { CreateDatetime = DateTime.Now, Email = "287245177@qq.com", SysUserid = Guid.NewGuid().ToString("N"), UserName = "chengong", }); var model = con.QuerySet<SysUser>().Where(a => a.Email == "287245177@qq.com").Get(); con.CommandSet<SysUser>().Where(a => a.SysUserid == model.SysUserid) .Update(a => new SysUser { Email = "2548987@qq.com" }); con.CommandSet<SysUser>().Where(a => a.SysUserid == model.SysUserid).Delete(); }
其他
除了简单的CURD还有Count、Sum、Exists
结束
第一个版本有未完善的地方,如果大家有很好的建议欢迎随时向我提,希望得到大家的建议后能良好的改善升级
作 者: 陈珙
出 处:http://www.cnblogs.com/skychen1218/
关于作者:专注于微软平台的项目开发。如有问题或建议,请多多赐教!
版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。