首页 > 其他分享 >Welcome To Learn Dapper

Welcome To Learn Dapper

时间:2023-06-19 10:56:52浏览次数:64  
标签:Learn SQL GetOrdinal Welcome ORM reader NET Dapper

Welcome To Learn Dapper

This site is for developers who want to learn how to use Dapper - The micro ORM created by the people behind Stack Overflow.

What is Dapper? 

Dapper is an open-source object-relational mapping (ORM) library for .NET and .NET Core applications. The library allows developers quickly and easily access data from databases without the need to write tedious code. Dapper allows you to execute raw SQL queries, map the results to objects, and execute stored procedures, among other things. It is available as a NuGet package.

  • Dapper is lightweight and fast, making it an ideal choice for applications that require low latency and high performance.
  • It is a simple yet powerful object mapping tool for any .NET language, such as C#, that enables developers to quickly and easily map query results from ADO.NET data readers to instances of business objects.
  • It has excellent support for both asynchronous and synchronous database queries and batching multiple queries together into a single call.
  • Additionally, dapper supports parameterized queries to help protect against SQL injection attacks.

Does Dapper support .NET Core? 

Yes, Dapper has been supporting .NET Core since the release of version 1.50 in July 2016. Dapper is a cross-platform .NET library, which means that it can be used on any platform that supports .NET, including .NET Core.

Does Dapper support C#? 

Yes, you can use Dapper with C# as multiple other languages, such as VB.NET and F#. In fact, Dapper was written in C# and is a popular choice for data access in C# applications because of its simplicity and efficiency.

Is Dapper an ORM? 

Dapper falls into a family of tools known as micro-ORMs. These tools perform only a subset of the functionality of full-blown Object Relations Mappers, such as Entity Framework Core, but Dapper is known for its speed and simple implementation compared to others. The following table provides a general idea of the capabilities that you can expect to find in a micro ORM compared to an ORM:

 Micro ORMORM
Map queries to objects    
Caching results    
Change tracking  1  
SQL generation  2  
Identity management    
Association management    
Lazy loading    
Unit of work support    
Database migrations    
     

Dapper concentrates its efforts on the O and M of ORM - Object Mapping.

[1] Some extensions have been added to Dapper that provide the minimal change-tracking capability

[2] Dapper does generate SQL but in a limited fashion. Third-party libraries such as Dapper Plus can generate the full SQL for insertupdate, and delete statements

When Should You Use Dapper? 

When deciding whether to use Dapper or not, one should bear in mind the primary reason for its existence - performance. The original developers of Dapper were using Entity Framework Core's predecessor - the short-lived Linq to SQL. However, they found that query performance wasn't good enough for the increasing traffic that the site in question (Stack Overflow) was experiencing, so they wrote their own micro ORM.

Dapper is, therefore, a good choice in scenarios where read-only data changes frequently and is requested often. It is particularly good in stateless scenarios (e.g. the web) where there is no need to persist complex object graphs in memory for any duration.

Dapper does not translate queries written in .NET languages to SQL like a full-blown ORM. So you need to be comfortable writing queries in SQL or have someone write them for you.

Dapper has no real expectations about the schema of your database. It is not reliant on conventions in the same way as Entity Framework Core, so Dapper is also a good choice where the database structure isn't particularly normalized.

Dapper works with an ADO.NET IDbConnection object, which means that it will work with any database system for which there is an ADO.NET provider.

There is no reason why you cannot use both an ORM and a micro ORM in the same project.

What does Dapper do? 

Here is a standard ADO.NET C# code for retrieving data from a database and materializing it as a collection of Product objects:

var sql = "select * from products";
var products = new List<Product>();
using (var connection = new SqlConnection(connString))
{
    connection.Open();
    using (var command = new SqlCommand(sql, connection))
    {
        using (var reader = command.ExecuteReader())
        {
            var product = new Product
            {
                ProductId = reader.GetInt32(reader.GetOrdinal("ProductId")),
                ProductName = reader.GetString(reader.GetOrdinal("ProductName")),
                SupplierId = reader.GetInt32(reader.GetOrdinal("SupplierId")),
                CategoryId = reader.GetInt32(reader.GetOrdinal("CategoryId")),
                QuantityPerUnit = reader.GetString(reader.GetOrdinal("QuantityPerUnit")),
                UnitPrice = reader.GetDecimal(reader.GetOrdinal("UnitPrice")),
                UnitsInStock = reader.GetInt16(reader.GetOrdinal("UnitsInStock")),
                UnitsOnOrder = reader.GetInt16(reader.GetOrdinal("UnitsOnOrder")),
                ReorderLevel = reader.GetInt16(reader.GetOrdinal("ReorderLevel")),
                Discontinued = reader.GetBoolean(reader.GetOrdinal("Discontinued")),
                DiscontinuedDate = reader.GetDateTime(reader.GetOrdinal("DiscontinuedDate"))
            };
            products.Add(product);
        }
    }
}

At its most basic level, Dapper replaces the highlighted block of assignment code in the example above with the following:

products = connection.Query<Product>(sql).ToList();

Dapper also creates the command and opens the connection if needed. If you use Dapper to manage basic assignments like this, it will save you hours. However, Dapper is capable of doing quite a bit more.

 

标签:Learn,SQL,GetOrdinal,Welcome,ORM,reader,NET,Dapper
From: https://www.cnblogs.com/ioriwellings/p/17490556.html

相关文章

  • COMP9417 - Machine Learning
    COMP9417-MachineLearningHomework1:RegularizedRegression&NumericalOptimizationIntroductionInthishomeworkwewillexploresomealgorithmsforgradientbasedoptimization.Thesealgorithmshavebeencrucialtothedevelopmentofmachinelearnin......
  • joint_learners_next用例
    【leave-joint处理前】 【leave-joint处理结果】voters=(2)&&(1)learners_next=(1)处理后voters=(2)learners=(1)【逻辑】 ......
  • 选修-4-Optimization for Deep Learning
    1.SomeNitations  在本小节开始之前,需要知道的符号含义.2.WhatisOptimizationabout?  找到一组参数使得\(L\)尽可能地小.......
  • Zero-Shot, One-Shot, and Few-Shot Learning概念介绍
    导语本文将介绍零样本学习、一次样本学习和少样本学习的概念,它们使得机器学习模型能够在仅有有限数量的示例情况下对对象或模式进行分类和识别。在机器学习中,我们通常需要大量的训练数据来训练模型,以便它能够准确地识别和分类新的输入。然而,在现实世界中,获取大规模标记数据集可能是......
  • 【笔记】learning git branching
    git图是由子节点指向父节点(可能有多个父节点)gitcommitgitbranchgitmergegitrebase......
  • 了解基于模型的元学习:Learning to Learn优化策略和Meta-Learner LSTM
    摘要:本文主要为大家讲解基于模型的元学习中的LearningtoLearn优化策略和Meta-LearnerLSTM。本文分享自华为云社区《深度学习应用篇-元学习[16]:基于模型的元学习-LearningtoLearn优化策略、Meta-LearnerLSTM》,作者:汀丶。1.LearningtoLearnLearningtoLearnbyGradien......
  • Learn UML with JUDE(一)
    我希望你能够使用JUDE去学习和体验UML,JUDE是一个建模工具,你可以用它去画UML。下面我会指导你通过一些实例去学习使用JUDE来画UML。一、Overview    UMLandUMLtools l    DescriptionofJUDE l    InstallationofJUDE l    FundamentalComponentsandB......
  • English Learning Articles 2022-06-11 Your teen wants to get in shape this summer
    Yourteenwantstogetinshapethissummer?Whattosayandwhentoworry|CNN Ifyourchildrensaytheywanttostartexercisingorworkingoutmorethissummer,don’tcelebratejustyet.Iknowmostparentswouldbethrilledtoseetheirteenstakin......
  • 强化学习Q-learning实践
    1.引言前篇文章介绍了强化学习系统红的基本概念和重要组成部分,并解释了Q-learning算法相关的理论知识。本文的目标是在Python3中实现该算法,并将其应用于实际的实验中。闲话少说,我们直接开始吧!2.Taxi-v3Env为了使本文具有实际具体的意义,特意选择了一个简单而基本的环境,可以让大......
  • Python+sklearn使用DBSCAN聚类算法案例一则
    DBSCAN聚类算法概述:DBSCAN属于密度聚类算法,把类定义为密度相连对象的最大集合,通过在样本空间中不断搜索最大集合完成聚类。DBSCAN能够在带有噪点的样本空间中发现任意形状的聚类并排除噪点。DBSCAN算法不需要预先指定聚类数量,但对用户设定的参数非常敏感。当空间聚类的密度不均匀、......