Dapper结合Repository模式的应用,包括如何在数据访问层(DAL)使用Dapper组件。

Dapper在真实项目中使用,扩展IDbConnection的功能,支持Oracle、MS SQL Server 2005数据库

1)定义统一的IDbConnection访问入口

  1. public class Database
  2. {
  3. /// 得到web.config里配置项的数据库连接字符串。
  4. private static readonly string ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
  5. /// 得到工厂提供器类型
  6. private static readonly string ProviderFactoryString = ConfigurationManager.AppSettings["DBProvider"].ToString();
  7. private static DbProviderFactory df = null;
  8. /// <summary>
  9. /// 创建工厂提供器并且
  10. /// </summary>
  11. public static IDbConnection DbService()
  12. {
  13. if (df == null)
  14. df = DbProviderFactories.GetFactory(ProviderFactoryString);
  15. var connection = df.CreateConnection();
  16. connection.ConnectionString = ConnectionString;
  17. connection.Open();
  18. return connection;
  19. }
  20. }

2)app.config配置

  1. <?xml version="1.0"?>
  2. <configuration>
  3. <configSections>
  4. </configSections>
  5. <appSettings>
  6. <add key="DBProvider" value="System.Data.SqlClient"/>
  7. </appSettings>
  8. <connectionStrings>
  9. <add name="ConnectionString" connectionString="Data Source=.;Initial Catalog=PlanDb;User ID=sa;Password=manager;" providerName="System.Data.SqlClient" />
  10. </connectionStrings>
  11. <startup>
  12. <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  13. </startup>
  14. </configuration>

3)Repository模式实现

  1. /// <summary>
  2. /// 产品管理
  3. /// </summary>
  4. public class ProductRepository
  5. {
  6. public static Product GetById(int id)
  7. {
  8. var sqlstr = "select * from dbo.Product where Product_ID=@id";
  9. using (var conn = Database.DbService())
  10. {
  11. return conn.Query<Product>(sqlstr, new { id }).Single();
  12. }
  13. }
  14. public static List<Product> GetByPid(int pid)
  15. {
  16. var sqlstr = "select * from dbo.Product where Parent_ID=@pid order by Product_no";
  17. using (var conn = Database.DbService())
  18. {
  19. return conn.Query<Product>(sqlstr, new { pid }).ToList();
  20. }
  21. }
  22. /// <summary>
  23. /// 获取所有产品--机型
  24. /// </summary>
  25. /// <returns></returns>
  26. public static List<Product> GetAllTop()
  27. {
  28. var sqlstr = "select * from dbo.Product where Parent_ID=0 order by Product_no";
  29. using (var conn = Database.DbService())
  30. {
  31. return conn.Query<Product>(sqlstr).ToList();
  32. }
  33. }
  34. public static void Insert(Product model)
  35. {
  36. if (model.Product_ID == 0)
  37. model.Product_ID = NextId;
  38. string sqlstr = "INSERT INTO dbo.Product" +
  39. "(Product_ID, Parent_ID, Product_No, Product_Name, Product_Type, Remark, Creater, Create_Date, Data_Availability) " +
  40. "values(@Product_ID,@Parent_ID,@Product_No,@Product_Name,@Product_Type,@Remark,@Creater,@Create_Date,@Data_Availability)";
  41. using (var conn = Database.DbService())
  42. {
  43. conn.Execute(sqlstr, model);
  44. }
  45. }
  46. public static void Delete(int id)
  47. {
  48. var sqlstr = "delete from dbo.Product where Product_ID=@id";
  49. using (var conn = Database.DbService())
  50. {
  51. conn.Execute(sqlstr, new { id });
  52. }
  53. }
  54. public static void Update(Product model)
  55. {
  56. string sqlstr = "UPDATE dbo.Product " +
  57. "SET Product_No = @Product_No," +
  58. "    Product_Name = @Product_Name, " +
  59. "    Product_Type = @Product_Type, " +
  60. "    Remark = @Remark" +
  61. " WHERE Product_ID = @Product_ID";
  62. using (var conn = Database.DbService())
  63. {
  64. conn.Execute(sqlstr,  model);
  65. }
  66. }
  67. /// <summary>
  68. /// 下一个ID
  69. /// </summary>
  70. public static int NextId
  71. {
  72. get
  73. {
  74. return Database.NextId("Product");
  75. }
  76. }
  77. public static bool Exists(string no)
  78. {
  79. var sqlstr = "select count(*) from dbo.Product where Product_No=@no";
  80. using (var conn = Database.DbService())
  81. {
  82. return conn.Query<int>(sqlstr, new { no }).Single() > 0;
  83. }
  84. }
  85. }

http://blog.csdn.net/dacong 转载请注明出处

  1. public class Product
  2. {
  3. #region Fields
  4. private int _product_id;
  5. private int _parent_id;
  6. private string _product_no = "";
  7. private string _product_name = "";
  8. private string _product_type = "";
  9. private string _remark = "";
  10. private string _creater = "";
  11. private DateTime _create_date;
  12. private string _data_availability = "";
  13. #endregion
  14. public Product()
  15. {
  16. _parent_id = 0;
  17. _data_availability = "Y";
  18. }
  19. #region Public Properties
  20. public int Product_ID
  21. {
  22. get { return _product_id; }
  23. set
  24. {
  25. _product_id = value;
  26. }
  27. }
  28. /// <summary>
  29. /// 父产品ID,0为最顶层产品
  30. /// </summary>
  31. public int Parent_ID
  32. {
  33. get { return _parent_id; }
  34. set
  35. {
  36. _parent_id = value;
  37. }
  38. }
  39. public string Product_No
  40. {
  41. get { return _product_no; }
  42. set
  43. {
  44. _product_no = value;
  45. }
  46. }
  47. public string Product_Name
  48. {
  49. get { return _product_name; }
  50. set
  51. {
  52. _product_name = value;
  53. }
  54. }
  55. public string Product_Type
  56. {
  57. get { return _product_type; }
  58. set
  59. {
  60. _product_type = value;
  61. }
  62. }
  63. public string Remark
  64. {
  65. get { return _remark; }
  66. set
  67. {
  68. _remark = value;
  69. }
  70. }
  71. public string Creater
  72. {
  73. get { return _creater; }
  74. set
  75. {
  76. _creater = value;
  77. }
  78. }
  79. public DateTime Create_Date
  80. {
  81. get { return _create_date; }
  82. set
  83. {
  84. _create_date = value;
  85. }
  86. }
  87. public string Data_Availability
  88. {
  89. get { return _data_availability; }
  90. set
  91. {
  92. _data_availability = value;
  93. }
  94. }
  95. #endregion
  96. }
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dacong/article/details/7300046

Dapper结合Repository模式的应用的更多相关文章

  1. Asp.Net Core + Dapper + Repository 模式 + TDD 学习笔记

    0x00 前言 之前一直使用的是 EF ,做了一个简单的小项目后发现 EF 的表现并不是很好,就比如联表查询,因为现在的 EF Core 也没有啥好用的分析工具,所以也不知道该怎么写 Linq 生成出 ...

  2. Dapper and Repository Pattern in MVC

    大家好,首先原谅我标题是英文的,因为我想不出好的中文标题. 这里我个人写了一个Dapper.net 的Repository模式的底层基础框架. 涉及内容: Dapper.net结合Repository ...

  3. 分享基于Entity Framework的Repository模式设计(附源码)

    关于Repository模式,在这篇文章中有介绍,Entity Framework返回IEnumerable还是IQueryable? 这篇文章介绍的是使用Entity Framework实现的Rep ...

  4. 关于MVC EF架构及Repository模式的一点心得

    一直都想写博客,可惜真的太懒了或者对自己的描述水平不太自信,所以...一直都是不想写的状态,关于领域驱动的东西看了不少,但是由于自己水平太差加上工作中实在用不到,所以一直处于搁置状态,最近心血来潮突然 ...

  5. 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【二】——使用Repository模式构建数据库访问层

    系列导航地址http://www.cnblogs.com/fzrain/p/3490137.html 前言 在数据访问层应用Repository模式来隔离对领域对象的细节操作是很有意义的.它位于映射层 ...

  6. (转)MVC中的Repository模式

    1.首先创建一个空的MVC3应用程序,命名为MyRepository.Web,解决方案命名为MyRepository. 2.添加一个类库项目,命名为MyRepository.DAL,添加一个文件夹命名 ...

  7. 关于Repository模式

    定义(来自Martin Fowler的<企业应用架构模式>): Mediates between the domain and data mapping layers using a co ...

  8. 探究Repository模式的两种写法与疑惑

    现如今DDD越来越流行,园子里漫天都是介绍关于它的文章.说到DDD就不能不提Repository模式了,有的地方也叫它仓储模式. 很多时候我们对Repository都还停留在Copy然后使用的阶段, ...

  9. LCLFramework框架之Repository模式

    Respository模式在示例中的实际目的小结一下 Repository模式是架构模式,在设计架构时,才有参考价值: Repository模式主要是封装数据查询和存储逻辑: Repository模式 ...

随机推荐

  1. 作业二:分布式版本控制系统Git的安装与使用

    作业要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2097 1.下载安装配置用户名和邮箱. (1)下载安装Github配置 ...

  2. C. Multiplicity 简单数论+dp(dp[i][j]=dp[i-1][j-1]+dp[i-1][j] 前面序列要满足才能构成后面序列)+sort

    题意:给出n 个数 的序列 问 从n个数删去任意个数  删去的数后的序列b1 b2 b3 ......bk  k|bk 思路: 这种题目都有一个特性 就是取到bk 的时候 需要前面有个bk-1的序列前 ...

  3. ☆ [NOI2014] 魔法森林 「LCT动态维护最小生成树」

    题目类型:\(LCT\)动态维护最小生成树 传送门:>Here< 题意:带权无向图,每条边有权值\(a[i],b[i]\).要求一条从\(1\)到\(N\)的路径,使得这条路径上的\(Ma ...

  4. windows 下搭建 git 服务器 gogs

    本文基于 windows7 64位 搭建 gogs gogs 官方文档地址:https://gogs.io/docs软件下载地址:https://dl.gogs.io/ 环境要求 数据库(选择以下一项 ...

  5. To the moon HDU - 4348 (主席树,区间修改)

    Background To The Moon is a independent game released in November 2011, it is a role-playing adventu ...

  6. Luogu P2057 [SHOI2007]善意的投票

    题目链接 \(Click\) \(Here\) 考虑模型转换.变成文理分科二选一带收益模型,就一波带走了. 如果没有见过这个模型的话,这里讲的很详细. #include <bits/stdc++ ...

  7. lombok系列(一)

    如果在类上面使用@Builder注解, @Builder public class A { } controller中使用: public String test(@RequestBody A a){ ...

  8. CMDB服务器管理系统【s5day90】:创建资产更新服务器硬盘信息

    1.创建硬件资产信息 import json from django.shortcuts import render,HttpResponse from django.views.decorators ...

  9. canvas绘图工具

    关于canvas绘图,在html页面上太方便了.当然刚开始还是入了不少坑,用了比如jcanvascript等三方插件.真实效果反而不理想,后来就没用插件. 下面是实现效果,可以在线绘制工地图然后传给后 ...

  10. MySQL学习笔记(六)MySQL8.0 配置笔记

    今天把数据库配置文件修改了,结果重启不了了 需要使用 mysqld --initialize 或 mysqld --initialize-insecure 命令来初始化数据库 1.mysqld --i ...