轻型的ORM类Dapper

Dapper是一个轻型的ORM类。代码就一个SqlMapper.cs文件,主要是IDbConnection的扩大方法,编译后哪怕40K的一个那个有些之dll。官方站点http://code.google.com/p/dapper-dot-net/
,也可以由此Nuget进行设置

Oracle 1

  • Dapper很快。Dapper的快接近和IDataReader。
  • Dapper支持主流数据库
    Mysql,SqlLite,Mssql2000,Mssql2005,Oracle等一样名目繁多的数据库
  • 支持多表并联的靶子。支持一对多 多对大多的涉及,并且没有侵入性。
  • 规律通过Emit反射IDataReader的队排,来快速的博与发对象
  • Dapper语法十分简单易行。并且不要迁就数据库的计划

Query()方法
Query()是IDbConnection扩展方法而重载了,从数据库里提取信息,并因而来填充我们的事体对象模型。

                var counters = new List<Tuple<int,
PerformanceCounter>>();
                using (var conn = new
SqlConnection(ConfigurationManager.ConnectionStrings[“SqlDiagnosticsDb”].ConnectionString))
                {
                    conn.Open();
                    string sql = string.Format(“select
Id,ServiceName,CategoryName,CounterName,InstanceName from
service_counters where MachineName='{0}'”,machineName);
                    foreach (var counter in conn.Query<ServiceCounter>(sql))
                    {
                        logger.InfoFormat(@”Creating performance
counter: {0}\{1}\{2}\{3}”, counter.MachineName ?? “.”,
counter.CategoryName,
                                            counter.CounterName,
counter.InstanceName);
                        var perfCounter = new
PerformanceCounter(counter.CategoryName, counter.CounterName,
counter.InstanceName, counter.MachineName ?? “.”);
                        counters.Add(new Tuple<int,
PerformanceCounter>(counter.Id, perfCounter));
                        // first value doesn’t matter so we should call
the counter at least once
                        try { perfCounter.NextValue(); }
                        catch { }
                    }
                }

下面是ServiceCounter的定义

public class ServiceCounter
{
    public int Id { get; set; }

    public String ServiceName { get; set; }

    public String MachineName { get; set; }
    public String CategoryName { get; set; }
    public String CounterName { get; set; }
    public String InstanceName { get; set; }

    public String DisplayName { get; set; }
   
    public String DisplayType { get; set; }

    public override String ToString()
    {
        return String.Format(@”{0}\{1}\{2}\{3}”, MachineName ?? “.”,
CategoryName, CounterName, InstanceName);
    }
}

Dapper也可以加载填充嵌套对象,考虑这么平等种情景,考虑到消息的品类属性,返回路对象。

1,在填写嵌套对象的时光,只好执行ToList<>方法,否则回报ExecuteReader
要求曾打开都可用之总是。连接的时状态呢已关闭,而单个对象非会见报错,估计是using结束后关闭了连,而嵌套对象在map的时节以行了ExecuteReader,只好以using结束之前返回list集合。
2,嵌套对象的参数是较多的,主要是眼前片个参数,其它参数没因此好装也null。特别而顾的凡splitOn,这个参数不能够也空,否则会报对象也援的不当。【splitOn参数的意思是读取第二只目标的之分割列,从哪个列起开始读取第二个目标,如果表里的于增长列都为Id,可以安装这个参数为”Id”】.

Execute方法
碰巧使Query方法是寻找数据的,Execute方法无会见招来数据,它与Query方法很相似,但其毕竟返回总数(受影响之行数),而非是一个靶集合【如:insert
update和delete】.

private void
SaveServiceSnapshots(IEnumerable<ServiceCounterSnapshot>
snapshots)
        {
            using (var conn = new
SqlConnection(ConfigurationManager.ConnectionStrings[“SqlDiagnosticsDb”].ConnectionString))
            {
                conn.Open();
                foreach (var snapshot in snapshots)
                {
                    // insert new snapshot to the database
                   conn.Execute(
    @”insert into
service_counter_snapshots(ServiceCounterId,SnapshotMachineName,CreationTimeUtc,ServiceCounterValue)
values (
       
@ServiceCounterId,@SnapshotMachineName,@CreationTimeUtc,@ServiceCounterValue)”,
snapshot);
                }

            }
        }

ServiceCounterSnapshot的定义如下:

public class ServiceCounterSnapshot
{
    public int Id { get; set; }

    public int ServiceCounterId { get; set; }

    /// <summary>
    /// Machine on which the snapshot was taken.
    /// </summary>
    public String SnapshotMachineName { get; set; }

    public DateTime CreationTimeUtc { get; set; }

    public float? ServiceCounterValue { get; set; }
}

A Look at
Dapper.NET

至于Dapper的一部分用到和扩大的事例

给力分享新的ORM =>
Dapper

相关文章