Dapper入门

Dapper简介:Dapper是.NET下一个micro的ORM,它和Entity Framework或Nhibnate不同,属于轻量级的,并且是半自动的。也就是说实体类都要自己写。它没有复杂的配置文件,一个单文件就可以了。

GIT地址:https://github.com/StackExchange/dapper-dot-net

Dapper扩展:https://github.com/tmsmith/Dapper-Extensions

一、Dapper优点

1.轻量级,单文件。

2.支持多数据库。

3.支持多表关联。

二、Dapper使用

在NuGet中搜索Dapper添加安装。如图:

三、Daaper实战

1.数据库连接

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//引用
using Dapper;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using MySql.Data.MySqlClient;

namespace CA.Dapper.Base
{
    public class SQLConnection
    {
        private static string sqlConnection = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        public static IDbConnection conn = new MySqlConnection(sqlConnection);
    }
}
View Code

2.Models

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CA.Dapper.Models
{
    [Serializable]
    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public string Address { get; set; }
        public DateTime CreateTime { get; set; }
    }
}
View Code

3.Dal

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//引用
using Dapper;
using CA.Dapper.Base;
using System.Data;

namespace CA.Dapper.Dal
{
    public class PersonDal
    {
        public void Query(int rows = 0)
        {
            string sql = string.Format("select id,name,age,address,createtime from person limit {0}", rows);
            var list = SQLConnection.conn.Query(sql);
        }
    }
}
View Code

4.Dto

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//引用
using CA.Dapper.Dal;
using CA.Dapper.Models;

namespace CA.Dapper.Dto
{
    public class PersonDto : PersonDal
    {
        public Person PersonInfo { get; set; }
        public List<Person> PersonList { get; set; }
    }
}
View Code

5.BO

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CA.Dapper.Base
{
    // 摘要: 
    //     单例模式抽象基础类,要求派生类有一个无参公共构造函数 public class SignBo : Base<SignBo>
    //     { public Singleton():base(){} }
    //
    // 类型参数: 
    //   T:
    public abstract class TBase<T> where T : new()
    {
        private static T instance;
        protected static readonly object syncRoot = new object();
        public static T Instance
        {
            get
            {
                if (TBase<T>.instance == null)
                {
                    lock (TBase<T>.syncRoot)
                    {
                        if (TBase<T>.instance == null)
                        {
                            TBase<T>.instance = ((default(T) == null) ? Activator.CreateInstance<T>() : default(T));
                        }
                    }
                }
                return TBase<T>.instance;
            }
        }
    }
}
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//引用
using CA.Dapper.Base;
using CA.Dapper.Dto;
using CA.Dapper.Models;

namespace CA.Dapper.Bo
{
    public sealed partial class PersonBO : TBase<PersonBO>
    {
        PersonDto dto = new PersonDto();
        public PersonBO() : base() { }
        public void Query(int rows)
        {
            dto.Query(rows);
        }
    }
}
View Code

6.调用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
//引用
using CA.Dapper.Bo;

namespace CA.Dapper
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            PersonBO.Instance.Query(50*10000);
            sw.Stop();
            Console.WriteLine(sw.ElapsedMilliseconds);
        }
    }
}
View Code

测试用的数据是百万级的,效果还是很ok的,跟Daaper一样高效率ORM还有一款 Chloe,Git地址:https://github.com/shuxinqin/Chloe

跟Dapper、Chloe、EF上着之间有一个性能比较的测试,http://blog.jobbole.com/104442/,各有特色,希望能对您有帮助。

 

转载于:https://www.cnblogs.com/wuxiaohui/p/5772109.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值