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. MySQL聚簇索引和非聚簇索引的对比

    首先要清楚:聚簇索引并不是一种单独的索引类型,而是一种存储数据的方式. 聚簇索引在实际中用的很多,Innodb就是聚簇索引,Myisam 是非聚簇索引. 在之前我想插入一段关于innodb和myisa ...

  2. 原生JS的Ajax技术

    1.同步和异步 同步现象:客户端发送请求到服务器端,当服务器返回响应之前,客户端都处于等待  卡死状态 异步现象:客户端发送请求到服务器端,无论服务器是否返回响应,客户端都可以随意做其他事情,不会被卡 ...

  3. 【数学建模】MatLab 数据读写方法汇总

    1.读入 txt 文件数据. load xxx.txt A=load(‘xxx.txt’) A=dlmread(‘xxx.txt’) A=importdata(‘xxx.txt’) 例:将身高体重的 ...

  4. linux 定时下载github最新代码

    场景:网站的代码在github上托管,静态网站部署在服务器上,每次自己修改完本地代码后,提交到github上,需要自己去服务器上执行git pull 拉取最新代码, 为了解决这种操作,自己再服务器上  ...

  5. magento 2 Check https://getcomposer.org/doc/articles/troubleshooting.md#memory-limit-errors for more info on how to handle out of memory errors.%

    Check https://getcomposer.org/doc/articles/troubleshooting.md#memory-limit-errors for more info on h ...

  6. logstash/conf.d文件编写

    logstash-01.conf input { beats { port => 5044 host => "0.0.0.0" type => "log ...

  7. CentOS配置history记录每个用户执行过的命令

    一个偶然的机会,看到了这个文档,先存下来,后续使用的话直接就加进去了 要记录登录者的用户名.IP.操作记录,在/etc/bashrc末尾加入几个环境变量,用于history命令显示用户ip等内容,完成 ...

  8. 20175209 《Java程序设计》第三周学习总结

    20175209 <Java程序设计>第三周学习总结 教材学习内容总结 第四章知识点 1.发展阶段: 面向机器——面向过程——面向对象(特点:封装性,继承性,多态性) 2.类: 类 声明变 ...

  9. C++线程同步的四种方式(Windows)

    为什么要进行线程同步? 在程序中使用多线程时,一般很少有多个线程能在其生命期内进行完全独立的操作.更多的情况是一些线程进行某些处理操作,而其他的线程必须对其处理结果进行了解.正常情况下对这种处理结果的 ...

  10. RTC子系统

    目录 RTC子系统 引入 hctosys.c interface.c class.c 小结 流程一览 框架分析 rtc_init rtc_device_register s3c_rtc_probe o ...