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模式 ...
随机推荐
- Linux 学习 (八) Shell
Linux达人养成计划 I 学习笔记 Shell 是什么: Shell 是一个命令解释器 Shell 还是一个功能相当强大的编程语言,易编写,易调试,灵活性较强 Shell 的分类: Bourne S ...
- nginx之安装、多虚拟主机、反向代理和负载均衡
一.web服务器与web框架 1.web服务器简介 Web 网络服务是一种被动访问的服务程序,即只有接收到互联网中其他主机发出的请求后才会响应,最终用于提供服务程序的Web服务器会通过 HTTP(超文 ...
- mpvue——引入iconfont字体图标
前言 有问题可以随时提问,评论私信,只要我有时间我都会第一时间回复.当大家发现这篇文章不适用的时候烦请告知下,我好做好更改! 放置 下载好的字体图标放在static目录下,我是自己又创建了一个icon ...
- [HackerRank]New Year Chaos[UNDONE]
Input (stdin)Download 2 8 5 1 2 3 7 8 6 4 8 1 2 5 3 7 8 6 4 Your Output (stdout) Too chaotic Too cha ...
- linux服务器间文件夹拷贝
要求,在A机器执行脚本,把A机器的某个目录文件拷贝到B机器. 第一版ftp实现: 1.A 机器先安装 ftp 客户端 $ sudo yum install ftp 2.B机器安装ftp服务端 $ su ...
- Security+认证812分轻松考过(备战分享)
2019.02.12,开工第一天,我参加了security+考试并顺利通过了考试,812分的成绩有点出乎我的意料,据我所知我周围还没有人考过800分的.怀着愉悦的心态分享下我的备考经历和考试经验. 备 ...
- Java转换流、缓冲流、流操作规律整理
转换流 1.1 OutputStreamWriter类 OutputStreamWriter 是字符流通向字节流的桥梁:可使用指定的字符编码表,将要写入流中的字符编码成字 ...
- jenkins持续集成原理
转载: 原文地址:http://www.2cto.com/kf/201609/544550.html 持续集成 开发中,我们经常遇到一些奇怪问题,比如本地可以编译成功的代码但是同事们更新代码后编译出错 ...
- saltstack主机管理项目:计主机管理项目命令分发器(三)
一.开发目标命令格式如下: 二.目录结构 三.代码注解 01.salt.py 只是一个入口,没干什么事情 #!/usr/bin/env python # -*- coding:utf-8 -*- # ...
- 分布式监控系统开发【day37】:需求讨论(一)
本节内容 为什么要做监控? 常用监控系统设计讨论 监控需求讨论 如何实现监控服务器的水平扩展? 监控系统架构设计 一.为什么要做监控? 熟悉IT监控系统的设计原理 开发一个简版的类Zabbix监控系统 ...