Dapper MicroORM to Query SQL Databases in .net/X#

This forum is meant for anything you would like to share with other visitors
Post Reply
Posts: 99
Joined: Sun Aug 23, 2020 3:07 pm
Location: Italy

Dapper MicroORM to Query SQL Databases in .net/X#

Post by VR »


I personally use Entity Framework Core (EFCore) to access SQL Databases with .net. But sometimes, when I just need to execute some SQL selects to get some data, EFCore is overkill.

In those cases, I use a MicroORM called Dapper. Dapper is a small library, written by StackOverflow, that does one thing: It adds a generic Query method to IDBConnection, that takes a select statement and returns a list of objects as a result very fast. The field values are assigned to the object properties based on name equality.

Here is a small example that shows how to get data into a List using the "traditional" Command and Reader way and the Dapper way.

Code: Select all

class Product
    property ID as int auto
    property Name as string auto
end class

method WithQueryAndReader(connection as IDBConnection) as List<Product>
    var result := List<Product>{}
    begin using var command := connection:CreateCommand()
        command:CommandText := "SELECT ID, Name FROM Product"
        begin using var reader := command:ExecuteReader()
            do while reader:Read()
                var product := Product{}
                product:ID := reader:GetInt32(reader.GetOrdinal("ID"))
                product:Name := reader:GetString(reader.GetOrdinal("Name"))
            end do
        end using    
    end using
    return result

method WithDapper(connection as IDBConnection) as List<Product>
    return connection.Query<Product>("SELECT ID, Name FROM Product");

You can find more information on Dapper here:

Dapper can be found on Nuget

Post Reply