Dapper结合Repository模式的应用
Dapper在真实项目中使用,扩展IDbConnection的功能,支持Oracle、MS SQL Server 2005数据库
1)定义统一的IDbConnection访问入口
- public class Database
- {
- /// 得到web.config里配置项的数据库连接字符串。
- private static readonly string ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
- /// 得到工厂提供器类型
- private static readonly string ProviderFactoryString = ConfigurationManager.AppSettings["DBProvider"].ToString();
- private static DbProviderFactory df = null;
- /// <summary>
- /// 创建工厂提供器并且
- /// </summary>
- public static IDbConnection DbService()
- {
- if (df == null)
- df = DbProviderFactories.GetFactory(ProviderFactoryString);
- var connection = df.CreateConnection();
- connection.ConnectionString = ConnectionString;
- connection.Open();
- return connection;
- }
- }
2)app.config配置
- <?xml version="1.0"?>
- <configuration>
- <configSections>
- </configSections>
- <appSettings>
- <add key="DBProvider" value="System.Data.SqlClient"/>
- </appSettings>
- <connectionStrings>
- <add name="ConnectionString" connectionString="Data Source=.;Initial Catalog=PlanDb;User ID=sa;Password=manager;" providerName="System.Data.SqlClient" />
- </connectionStrings>
- <startup>
- <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
- </startup>
- </configuration>
3)Repository模式实现
- /// <summary>
- /// 产品管理
- /// </summary>
- public class ProductRepository
- {
- public static Product GetById(int id)
- {
- var sqlstr = "select * from dbo.Product where Product_ID=@id";
- using (var conn = Database.DbService())
- {
- return conn.Query<Product>(sqlstr, new { id }).Single();
- }
- }
- public static List<Product> GetByPid(int pid)
- {
- var sqlstr = "select * from dbo.Product where Parent_ID=@pid order by Product_no";
- using (var conn = Database.DbService())
- {
- return conn.Query<Product>(sqlstr, new { pid }).ToList();
- }
- }
- /// <summary>
- /// 获取所有产品--机型
- /// </summary>
- /// <returns></returns>
- public static List<Product> GetAllTop()
- {
- var sqlstr = "select * from dbo.Product where Parent_ID=0 order by Product_no";
- using (var conn = Database.DbService())
- {
- return conn.Query<Product>(sqlstr).ToList();
- }
- }
- public static void Insert(Product model)
- {
- if (model.Product_ID == 0)
- model.Product_ID = NextId;
- string sqlstr = "INSERT INTO dbo.Product" +
- "(Product_ID, Parent_ID, Product_No, Product_Name, Product_Type, Remark, Creater, Create_Date, Data_Availability) " +
- "values(@Product_ID,@Parent_ID,@Product_No,@Product_Name,@Product_Type,@Remark,@Creater,@Create_Date,@Data_Availability)";
- using (var conn = Database.DbService())
- {
- conn.Execute(sqlstr, model);
- }
- }
- public static void Delete(int id)
- {
- var sqlstr = "delete from dbo.Product where Product_ID=@id";
- using (var conn = Database.DbService())
- {
- conn.Execute(sqlstr, new { id });
- }
- }
- public static void Update(Product model)
- {
- string sqlstr = "UPDATE dbo.Product " +
- "SET Product_No = @Product_No," +
- " Product_Name = @Product_Name, " +
- " Product_Type = @Product_Type, " +
- " Remark = @Remark" +
- " WHERE Product_ID = @Product_ID";
- using (var conn = Database.DbService())
- {
- conn.Execute(sqlstr, model);
- }
- }
- /// <summary>
- /// 下一个ID
- /// </summary>
- public static int NextId
- {
- get
- {
- return Database.NextId("Product");
- }
- }
- public static bool Exists(string no)
- {
- var sqlstr = "select count(*) from dbo.Product where Product_No=@no";
- using (var conn = Database.DbService())
- {
- return conn.Query<int>(sqlstr, new { no }).Single() > 0;
- }
- }
- }
http://blog.csdn.net/dacong 转载请注明出处
- public class Product
- {
- #region Fields
- private int _product_id;
- private int _parent_id;
- private string _product_no = "";
- private string _product_name = "";
- private string _product_type = "";
- private string _remark = "";
- private string _creater = "";
- private DateTime _create_date;
- private string _data_availability = "";
- #endregion
- public Product()
- {
- _parent_id = 0;
- _data_availability = "Y";
- }
- #region Public Properties
- public int Product_ID
- {
- get { return _product_id; }
- set
- {
- _product_id = value;
- }
- }
- /// <summary>
- /// 父产品ID,0为最顶层产品
- /// </summary>
- public int Parent_ID
- {
- get { return _parent_id; }
- set
- {
- _parent_id = value;
- }
- }
- public string Product_No
- {
- get { return _product_no; }
- set
- {
- _product_no = value;
- }
- }
- public string Product_Name
- {
- get { return _product_name; }
- set
- {
- _product_name = value;
- }
- }
- public string Product_Type
- {
- get { return _product_type; }
- set
- {
- _product_type = value;
- }
- }
- public string Remark
- {
- get { return _remark; }
- set
- {
- _remark = value;
- }
- }
- public string Creater
- {
- get { return _creater; }
- set
- {
- _creater = value;
- }
- }
- public DateTime Create_Date
- {
- get { return _create_date; }
- set
- {
- _create_date = value;
- }
- }
- public string Data_Availability
- {
- get { return _data_availability; }
- set
- {
- _data_availability = value;
- }
- }
- #endregion
- }
Dapper结合Repository模式的应用的更多相关文章
- Asp.Net Core + Dapper + Repository 模式 + TDD 学习笔记
0x00 前言 之前一直使用的是 EF ,做了一个简单的小项目后发现 EF 的表现并不是很好,就比如联表查询,因为现在的 EF Core 也没有啥好用的分析工具,所以也不知道该怎么写 Linq 生成出 ...
- Dapper and Repository Pattern in MVC
大家好,首先原谅我标题是英文的,因为我想不出好的中文标题. 这里我个人写了一个Dapper.net 的Repository模式的底层基础框架. 涉及内容: Dapper.net结合Repository ...
- 分享基于Entity Framework的Repository模式设计(附源码)
关于Repository模式,在这篇文章中有介绍,Entity Framework返回IEnumerable还是IQueryable? 这篇文章介绍的是使用Entity Framework实现的Rep ...
- 关于MVC EF架构及Repository模式的一点心得
一直都想写博客,可惜真的太懒了或者对自己的描述水平不太自信,所以...一直都是不想写的状态,关于领域驱动的东西看了不少,但是由于自己水平太差加上工作中实在用不到,所以一直处于搁置状态,最近心血来潮突然 ...
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【二】——使用Repository模式构建数据库访问层
系列导航地址http://www.cnblogs.com/fzrain/p/3490137.html 前言 在数据访问层应用Repository模式来隔离对领域对象的细节操作是很有意义的.它位于映射层 ...
- (转)MVC中的Repository模式
1.首先创建一个空的MVC3应用程序,命名为MyRepository.Web,解决方案命名为MyRepository. 2.添加一个类库项目,命名为MyRepository.DAL,添加一个文件夹命名 ...
- 关于Repository模式
定义(来自Martin Fowler的<企业应用架构模式>): Mediates between the domain and data mapping layers using a co ...
- 探究Repository模式的两种写法与疑惑
现如今DDD越来越流行,园子里漫天都是介绍关于它的文章.说到DDD就不能不提Repository模式了,有的地方也叫它仓储模式. 很多时候我们对Repository都还停留在Copy然后使用的阶段, ...
- LCLFramework框架之Repository模式
Respository模式在示例中的实际目的小结一下 Repository模式是架构模式,在设计架构时,才有参考价值: Repository模式主要是封装数据查询和存储逻辑: Repository模式 ...
随机推荐
- MySQL聚簇索引和非聚簇索引的对比
首先要清楚:聚簇索引并不是一种单独的索引类型,而是一种存储数据的方式. 聚簇索引在实际中用的很多,Innodb就是聚簇索引,Myisam 是非聚簇索引. 在之前我想插入一段关于innodb和myisa ...
- 原生JS的Ajax技术
1.同步和异步 同步现象:客户端发送请求到服务器端,当服务器返回响应之前,客户端都处于等待 卡死状态 异步现象:客户端发送请求到服务器端,无论服务器是否返回响应,客户端都可以随意做其他事情,不会被卡 ...
- 【数学建模】MatLab 数据读写方法汇总
1.读入 txt 文件数据. load xxx.txt A=load(‘xxx.txt’) A=dlmread(‘xxx.txt’) A=importdata(‘xxx.txt’) 例:将身高体重的 ...
- linux 定时下载github最新代码
场景:网站的代码在github上托管,静态网站部署在服务器上,每次自己修改完本地代码后,提交到github上,需要自己去服务器上执行git pull 拉取最新代码, 为了解决这种操作,自己再服务器上 ...
- 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 ...
- logstash/conf.d文件编写
logstash-01.conf input { beats { port => 5044 host => "0.0.0.0" type => "log ...
- CentOS配置history记录每个用户执行过的命令
一个偶然的机会,看到了这个文档,先存下来,后续使用的话直接就加进去了 要记录登录者的用户名.IP.操作记录,在/etc/bashrc末尾加入几个环境变量,用于history命令显示用户ip等内容,完成 ...
- 20175209 《Java程序设计》第三周学习总结
20175209 <Java程序设计>第三周学习总结 教材学习内容总结 第四章知识点 1.发展阶段: 面向机器——面向过程——面向对象(特点:封装性,继承性,多态性) 2.类: 类 声明变 ...
- C++线程同步的四种方式(Windows)
为什么要进行线程同步? 在程序中使用多线程时,一般很少有多个线程能在其生命期内进行完全独立的操作.更多的情况是一些线程进行某些处理操作,而其他的线程必须对其处理结果进行了解.正常情况下对这种处理结果的 ...
- RTC子系统
目录 RTC子系统 引入 hctosys.c interface.c class.c 小结 流程一览 框架分析 rtc_init rtc_device_register s3c_rtc_probe o ...