asp.net core series 66 Dapper introduced --Micro-ORM

One. Outline

  Currently ORM for .net data access tools a lot, EF and EF Core is a heavyweight framework. Recently built new infrastructure projects, to learn about lightweight ORM data access tool Dapper. Dapper support for SQL Server, MySQL, Sqlite, SqlCE, Firebird and other high-performance Micro-ORM (micro ORM framework). The latest version is 1.60.16.

    Dapper extends IDbConnection interface, for example to achieve a query method, a method Query query Dapper defined as follows:

  public static IEnumerable<T> Query<T>(this IDbConnection cnn, string sql, object param = null, SqlTransaction transaction = null, bool buffered = true)

    Ado.net the original ecological IDbConnection System.Data.SqlClient also implements the interface, refer to the following:

    https://docs.microsoft.com/en-us/dotnet/api/system.data.idbconnection?redirectedfrom=MSDN&view=netframework-4.8

    Dapper Open Source Address: https://github.com/StackExchange/Dapper

    Dapper Nuget package: https://www.nuget.org/packages/Dapper/

 

  The main features of Dapper comprising:

    (1) support strong typing. The Query Query <T>

    (2) support for dynamic objects. . Rainbow expansion packs have achieved in Dapper

    (3) Execute T-SQL script execution block, does not return results.

    (4) batch execute SQL statements.

    (5)     "Performance" is a bright spot of Dapper . The official list of query (select) parameter comparison with other ORM tool performance.

    (6) parameterized queries

    (7) List Support

    (8) Alternatively literally

    (9) For the query buffer and unbuffered, the default behavior is to buffer the data to the back-end List, after obtaining data, freeing up any resources (reduces database shared locks, etc.). For a large number of queries (for example:> 100), buffer behavior needs to be shut down to avoid taking up too much memory, set buffered: false's Query method. "Note that the buffer is not cached."

        dapper buffer/cache

    (10) entities and foreign key relationships table mapping.

    (11) allows a single query request, a plurality of query tables. Such as: select table1, select table2

    (12) database transaction processing support program.

 

  Dapper Note:

    (1) Dapper cache used to store information for each query, cache ConcurrentDictionaryobject, if dynamically generated sql string without arguments, you may experience memory problems.

    (2) many of the features of simplicity means ORM Dapper comes have been stripped. It worried 95% of cases, and provide you with the tools you need most of the time. It does not attempt to solve all the problems.

 

Two. Dapper source package

  2.1 Dapper.Contrib包

    Dapper.Contrib是包含许多用于CURD的Helpers key Contrib part is that it provides you with tracking entities, in order to determine whether changes have been made. GET out to achieve with EF, like when the Update method is only required to update the property has changed in SQL. For a complete list of extension methods Dapper.Contrib can be seen methods are strongly typed :

T Get<T>(id);
IEnumerable<T> GetAll<T>();
int Insert<T>(T obj);
int Insert<T>(Enumerable<T> list);
bool Update<T>(T obj);
bool Update<T>(Enumerable<T> list);
bool Delete<T>(T obj);
bool Delete<T>(Enumerable<T> list);
bool DeleteAll<T>();

    Dapper automatically named using the "  idProperties" (case-insensitive) properties as the Key (so the data tables is preferably the primary key to the name ID), if the entity does not follow this convention, use [Key]or [ExplicitKey]property modifying specific properties. [Key]Keys should be used (e.g. autoincrement column) generated by the database, but [ExplicitKey]should be used to explicitly generated key code

public class User
{
    [Key]
    int TheId { get; set; }
    string Name { get; set; }
    int Age { get; set; }
}

    Dapper.Contrib uses some optional attributes: [Table("Tablename")] - name table using the name of another class instead, the following example:

[Table ("emps")]
public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
} 

    [Write(true/false)] - This property is (not) be written
    [Computed] - this property has been calculated, not as part of the update

   具体参考 https://github.com/StackExchange/Dapper/tree/master/Dapper.Contrib

 

  2.2 Dapper.EntityFramework包

    When Dapper.EntityFramework and Dapper.EntityFramework.StrongName relied entityframework DbGeography class 6.1.3 and Microsoft.SqlServer.Types of SqlGeography class, geometry and geography to read values ​​from the database, we need to clearly specify the SRID to leave it in DbGeometry / DbGeography type.

 

  2.3 Dapper. Rainbow package

    Abstract Rainbow is a class, it can be used as a base class Dapper class, to achieve CURD的a method, in a dynamic type for Dynamic based insert, update, and query parameters to achieve the output return a strongly typed. Rainbow want all of you have a table named "Id" of the identity column, there is no Dapper in Dapper source code solutions. Test Case Rainbow assemblies .

    Contrib and the main difference between the Rainbow is: a track change your entity, and the other does not track:

    (1) If you want to be able to track changes in an entity, use Contrib.

    (1) When you want to use content more in line with standard ADO.NET method, use the Rainbow.

 

  2.4 Dapper.SqlBuilder package

    Dapper.SqlBuilder components for dynamically building SQL queries.

 

  2.5 Dapper.StrongName package with Dapper

    All class files in the package comes from StrongName Dapper package in the category, support SQL Server, MySQL, Sqlite, SqlCE, Firebird and other high-performance Micro-ORM, a strong name, use SqlMapper to achieve light of ADO.NET the magnitude of the object mapper.

 

III. Other extended package

  3.1 Dapper. SimpleCRUD

    SimpleCRUD is a simple CRUD assistant, you can choose to change the database dialect. The default is Microsoft SQL Server, but can be changed to PostgreSQL or MySQL. Abandoned .Net Core version of SQLite support (practical in the new project).

  3.2 Dapper.Extensions.NetCore

    Dapper is a simple CRUD assistant. On still other Nuget Dapper.Extensions.xx expansion pack.

 

Guess you like

Origin www.cnblogs.com/MrHSR/p/11082111.html
Recommended