前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Dapper.Net 应用

Dapper.Net 应用

作者头像
用户1055830
发布2018-01-18 15:18:23
1.5K0
发布2018-01-18 15:18:23
举报
文章被收录于专栏:飞扬的花生

                                                     Dapper应用

1.Dapper是什么

    Dapper是一款轻量级ORM工具。如果你在小的项目中,使用Entity Framework、NHibernate 来处理大数据访问及关系映射,未免有点杀鸡用牛刀。你又觉得ORM省时省力,这时Dapper 将是你不二的选择。

2.为什么使用

  1. 轻量,编译完成之后只有120k(好象是变胖了)
  2. 速度快。Dapper的速度接近与IDataReader,取列表的数据超过了DataTable。
  3. 支持多种数据库。Dapper可以在所有Ado.net Providers下工作,包括sqlite, sqlce, firebird, oracle, MySQL, PostgreSQL and SQL Server
  4. 可以映射一对一,一对多,多对多等多种关系。
  5. 性能高。通过Emit反射IDataReader的序列队列,来快速的得到和产生对象,性能不错。
  6. 支持FrameWork2.0,3.0,3.5,4.0,4.5

3.使用Dapper.Net并演示

1. 使用Sqlserver创建测试表

2.创建winform应用程序,引用Dapper封装基础应用和框架

3.创建简单页面实现CRUD

4.开始实现

4.1创建表

代码语言:javascript
复制
CREATE DATABASE test
USE test
GO
CREATE TABLE Test
(
 testId INT PRIMARY KEY IDENTITY,
 testName VARCHAR(50) NULL
)

INSERT INTO Test VALUES ('CallmeYhz')

INSERT INTO Test  VALUES('周公瑾')

4.2实体

代码语言:javascript
复制
  public class Test
    {
        public int testId { get; set; }
        public string testName { get; set; }
    }

4.3 Nuget包

4.4 封装搭建

 该类实现接口

代码语言:javascript
复制
    public interface IDaoBase<T, TKey> where T : class
    {
        int Count(IPredicate predicate, bool buffered = false);
        bool Delete(IPredicate predicate);
        bool DeleteByID(TKey key);
        bool DeleteByIDList(Expression<Func<T, object>> expression, IEnumerable<TKey> idList);
        int Execute(string sql, dynamic param = null);
        bool Exists(string sql, dynamic param = null, bool buffered = false);
        IEnumerable<TReturn> Get<TFirst, TSecond, TReturn>(string sql, Func<TFirst, TSecond, TReturn> map, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null);
        IList<T> GetAll();
        T GetByID(TKey key);
        IEnumerable<T> GetByIDList(Expression<Func<T, object>> expression, IEnumerable<TKey> idList, bool buffered = false);
        T GetEntity(IPredicate predicate, bool buffered = false);
        T GetEntity(string sql, object param, bool buffered = false);
        PagedList<T> GetEntityPageList(SearchBase search);
        IList<T> GetList(IPredicate predicate = null, IList<ISort> sort = null, bool buffered = false);
        IList<T> GetList(string sql, object param = null, bool buffered = true);
        IList<TEntity> GetList<TEntity>(string sql, object param = null, bool buffered = true);
        SqlMapper.GridReader GetMultiple(string sql, dynamic param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null);
        TScale GetScale<TScale>(string sql, dynamic param = null, bool buffered = false);
        dynamic Insert(T entity);
        void InsertBatch(IEnumerable<T> entityList, IDbTransaction transaction = null);
        bool Update(T entity);
        void UpdateBatch(IEnumerable<T> entityList);
    }

数据连接工厂

代码语言:javascript
复制
  public class ConnectionFactory
    {
        /// <summary>
        /// 数据库连接
        /// </summary>
        public static string ConnectionString { set; get; }

        /// <summary>
        /// 数据库类型
        /// </summary>
        public static DatabaseType DataBaseType { set; get; }


        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="conectionString">连接串</param>
        /// <param name="dataBaseType">数据库类型</param>
        public static void Init(string conectionString, DatabaseType dataBaseType = DatabaseType.SqlServer)
        {
            ConnectionFactory.ConnectionString = conectionString;
            ConnectionFactory.DataBaseType = dataBaseType;
        }


        /// <summary>
        /// 创建数据库连接
        /// </summary>
        /// <returns></returns>
        public static IDbConnection CreateConnection()
        {
            IDbConnection connection = null;

            switch (ConnectionFactory.DataBaseType)
            {
                case DatabaseType.SqlServer:
                    connection = new SqlConnection(ConnectionFactory.ConnectionString);
                    break;

            }

            return connection;
        }
    }
代码语言:javascript
复制
 public class TestDao : DaoBase<Test,int>
    {
        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <returns></returns>
        public IList<Test> GetAllList()
        {
            string sql = @"
                SELECT*FROM Test";
            return this.GetList<Test>(sql);
        }

        
        /// <summary>
        /// 根据名称获取实体
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public Test GetByName(string name)
        {
            string sql = @"
                SELECT*FROM Test WHERE testName=@testName";
            return this.GetEntity(sql, new { testName = name });
        }
        
    }

构造一个简单Winform界面

配置数据库连接字符串并且初始化

代码语言:javascript
复制
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <connectionStrings>
    <!-- 测试 -->
    <add name="db" connectionString=" Data Source=.;Initial Catalog=test;Integrated Security=SSPI; " />

  </connectionStrings>
</configuration>
代码语言:javascript
复制
     //初始化数据库连接
            ConnectionFactory.Init(ConfigurationManager.ConnectionStrings["db"].ConnectionString);

4.5演示

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-06-14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  •                                                      Dapper应用
    • 1.Dapper是什么
      • 2.为什么使用
        • 3.使用Dapper.Net并演示
          • 4.开始实现
            • 4.1创建表
            • 4.2实体
            • 4.3 Nuget包
            • 4.4 封装搭建
          • 4.5演示
          相关产品与服务
          云数据库 MySQL
          腾讯云数据库 MySQL(TencentDB for MySQL)为用户提供安全可靠,性能卓越、易于维护的企业级云数据库服务。其具备6大企业级特性,包括企业级定制内核、企业级高可用、企业级高可靠、企业级安全、企业级扩展以及企业级智能运维。通过使用腾讯云数据库 MySQL,可实现分钟级别的数据库部署、弹性扩展以及全自动化的运维管理,不仅经济实惠,而且稳定可靠,易于运维。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档