Stackoverflow/dapper的Dapper-Extensions用法(一)

http://www.cnblogs.com/Sinte-Beuve/p/4612971.html

Dapper-Extensions

Dapper Extensions is a small library that complements Dapper by adding basic CRUD operations (Get, Insert, Update, Delete) for your POCOs. For more advanced querying scenarios, Dapper Extensions provides a predicate system. The goal of this library is to keep your POCOs pure by not requiring any attributes or base class inheritance.

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

  • Dapper是一個開源輕的量級的orm,他的優點和用法在之前寫的博客中有提到。可是它只支持帶sql語句的CRUD。
  • Dapper-Extensions也是一個開源庫,他在Dapper的基礎上封裝了基本的CRUD操作,使得一些簡單的數據庫操作可以不用自己寫sql語句。使用起來更方面。

若不熟悉Dapper的,請移步:

後續文檔翻譯

下面是對他的用法的描述,也就是對項目文檔的翻譯。如果讀者英文不錯可以直接看原版文檔,見github。

Introduction

Dapper Extensions是github上的一個開源庫是對StackOVerflow開發的Dapper ORM的一個擴展。它增加了基礎的CRUD操作((Get, Insert, Update, Delete)),對更高級的查詢場景,該類庫還提供了一套謂詞系統。它的目標是保持POCOs的純淨,不需要額外的attributes和類的繼承。

自定義映射請參見 ClassMapper

Features

  • 零配置
  • 自動映射POCOs的CRUD操作
  • GetList, Count等方法可以用於更高級的場景。
  • GetPage for returning paged result sets.支持分頁
  • 自動支持Guid和Integer類型的主鍵,(也可以手動指定其他鍵類型)
  • 通過使用ClassMapper可以使保證POCOs純淨。 (Attribute Free!)
  • 可以通過使用ClassMapper來自定義entity-table映射
  • 支持聯合主鍵
  • POCO類名默認與數據表名相匹配,也可以自定義
  • 易於使用的 Predicate System適用於高級場合
  • 在生成SQL語句時正確轉義表名和類名 (Ex: SELECT FirstName FROM Users WHERE Users.UserId = @ UserId_0)
  • 覆蓋單元測試(覆蓋了150+個單元測試)

Naming Conventions(命名約定)

  • POCO類名與數據庫中表名匹配,多元化(Pluralized)的表名(暫時理解爲別名吧)可以通過PlurizedAutoClassMapper來自定義。
  • POCO類中屬性名和數據庫表中的類名匹配。
  • 暫約定主鍵被命名爲Id.使用其他主鍵需要自定義映射(ClassMapper)。

Installation

Nuget:
http://nuget.org/List/Packages/DapperExtensions

PM> Install-Package DapperExtensions

Examples

pernson POCO的定義

public class Person
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public bool Active { get; set; }
    public DateTime DateCreated { get; set; }
}

Get Operation

using (SqlConnection cn = new SqlConnection(_connectionString))
{
    cn.Open();
    int personId = 1;
    Person person = cn.Get<Person>(personId);
    cn.Close();
}

Simple Insert Operation

using (SqlConnection cn = new SqlConnection(_connectionString))
{
    cn.Open();
    Person person = new Person { FirstName = "Foo", LastName = "Bar" };
    int id = cn.Insert(person);
    cn.Close();
}

Advanced Insert Operation (Composite Key)複合主鍵

//返回dynamic類型,若主鍵爲單,返回主鍵值,若主鍵爲複合的,返回IDictionary<string,object>
 public static dynamic Insert<T>(this IDbConnection connection, T entity, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
public class Car
{
    public int ModelId { get; set; }
    public int Year { get; set; }
    public string Color { get; set; }
}

using (SqlConnection cn = new SqlConnection(_connectionString))
{
    cn.Open();
    Car car = new Car { Color = "Red" };
   //返回o
    var multiKey = cn.Insert(car);
    cn.Close();

    int modelId = multiKey.ModelId;
    int year = multiKey.Year;
}

Simple Update Operation

using (SqlConnection cn = new SqlConnection(_connectionString))
{
    cn.Open();
    int personId = 1;
    Person person = _connection.Get<Person>(personId);
    person.LastName = "Baz";
    cn.Update(person);
    cn.Close();
}

Simple Delete Operation

using (SqlConnection cn = new SqlConnection(_connectionString))
{
    cn.Open();
    Person person = _connection.Get<Person>(1);
    cn.Delete(person);
    cn.Close();
}

GetList Operation (with Predicates)

using (SqlConnection cn = new SqlConnection(_connectionString))
{
    cn.Open();
    var predicate = Predicates.Field<Person>(f => f.Active, Operator.Eq, true);
    IEnumerable<Person> list = cn.GetList<Person>(predicate);
    cn.Close();
}

Generated SQL

SELECT 
   [Person].[Id]
 , [Person].[FirstName]
 , [Person].[LastName]
 , [Person].[Active]
 , [Person].[DateCreated] 
FROM [Person] 
WHERE ([Person].[Active] = @Active_0)

Count Operation (with Predicates)

using (SqlConnection cn = new SqlConnection(_connectionString))
{
    cn.Open();
    var predicate = Predicates.Field<Person>(f => f.DateCreated, Operator.Lt, DateTime.UtcNow.AddDays(-5));
    int count = cn.Count<Person>(predicate);
    cn.Close();
}      

Generated SQL

SELECT 
   COUNT(*) Total 
FROM [Person] 
WHERE ([Person].[DateCreated] < @DateCreated_0)

作者:Sinte-Beuve

出處:http://www.cnblogs.com/Sinte-Beuve/

本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章