MicroOrm.Dapper.Repositories 1.24.1

.NET 5.0 .NET Standard 2.0 .NET Framework 4.6.1
dotnet add package MicroOrm.Dapper.Repositories --version 1.24.1
NuGet\Install-Package MicroOrm.Dapper.Repositories -Version 1.24.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MicroOrm.Dapper.Repositories" Version="1.24.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MicroOrm.Dapper.Repositories --version 1.24.1
#r "nuget: MicroOrm.Dapper.Repositories, 1.24.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install MicroOrm.Dapper.Repositories as a Cake Addin
#addin nuget:?package=MicroOrm.Dapper.Repositories&version=1.24.1

// Install MicroOrm.Dapper.Repositories as a Cake Tool
#tool nuget:?package=MicroOrm.Dapper.Repositories&version=1.24.1

MicroOrm.Dapper.Repositories

ci NuGet NuGet CodeFactor License MIT

Description

If you like your code to run fast, you probably know about Micro ORMs. They are simple and one of their main goals is to be the fastest execution of your SQL sentences in you data repository. For some Micro ORM's you need to write your own SQL sentences and this is the case of the most popular Micro ORM Dapper

This tool abstracts the generation of the SQL sentence for CRUD operations based on each C# POCO class "metadata". We know there are plugins for both Micro ORMs that implement the execution of these tasks, but that's exactly where this tool is different. The "SQL Generator" is a generic component that generates all the CRUD sentences for a POCO class based on its definition and the possibility to override the SQL generator and the way it builds each sentence.

The original idea was taken from Diego García.

Installation

dotnet add package MicroOrm.Dapper.Repositories

Docs

Metadata attributes

[Key] From System.ComponentModel.DataAnnotations - Use for primary key.

[Identity] Use for identity key.

[Table] From System.ComponentModel.DataAnnotations.Schema - By default the database table name will match the model name but it can be overridden with this.

[Column] From System.ComponentModel.DataAnnotations.Schema - By default the column name will match the property name but it can be overridden with this.

[NotMapped] From System.ComponentModel.DataAnnotations.Schema - For "logical" properties that do not have a corresponding column and have to be ignored by the SQL Generator.

[Deleted], [Status] For tables that implement "logical deletes" instead of physical deletes. Use this to decorate the bool or enum.

[LeftJoin] Left join for property: Collection and object are supported.

[InnerJoin] Inner join for property: Collection and object are supported.

[RightJoin] Right join for property: Collection and object are supported.

[CrossJoin] - SQLite only Cross join for property: Collection and object are supported.

[UpdatedAt] Automatically set DataTime.UtcNow (You can use local date or define offset) for Insert and Update.

Notes

  • By default the SQL Generator is going to map the POCO name with the table name, and each public property to a column.
  • If the [Deleted] is used on a certain POCO, the sentence will be an update instead of a delete.
  • Supports complex primary keys.
  • Supports simple Joins.
  • For this moment, with MSSQL you can only use limit with offset if you call OrderBy first, otherwise limit will be ignored.
  • It has a problem when try to use GUID with dapper in Oracle. In this case it doesn't work. Look details: Dapper#633, Dapper#637

Maps

"Users" POCO:

[Table("Users")]
public class User
{
    [Key, Identity]
    public int Id { get; set; }

    public string ReadOnly => "test"; // because don't have set

    public string Name { get; set; }

    public int AddressId { get; set; }

    [LeftJoin("Cars", "Id", "UserId")]
    public List<Car> Cars { get; set; }

    [LeftJoin("Addresses", "AddressId", "Id")]
    public Address Address { get; set; }

    [Status, Deleted]
    public bool Deleted { get; set; }

    [UpdatedAt]
    public DateTime? UpdatedAt { get; set; }
}

"Cars" POCO:

[Table("Cars")]
public class Car
{
    [Key, Identity]
    public int Id { get; set; }

    public string Name { get; set; }

    public byte[] Data { get; set; }

    public int UserId { get; set; }

    [LeftJoin("Users", "UserId", "Id")]
    public User User { get; set; }

    [Status]
    public StatusCar Status { get; set; }
}

public enum StatusCar
{
    Inactive = 0,

    Active = 1,

    [Deleted]
    Deleted = -1
}

Example Implements the simple repository:

public class UserRepository : DapperRepository<User>
{
    public UserRepository(IDbConnection connection, ISqlGenerator<User> sqlGenerator)
        : base(connection, sqlGenerator)
    {

    }
}

Queries

Find by ID:

var user = await userRepository.FindAsync(x => x.Id == 5);

Query with limit:

var limit = 10u;
var users = await userRepository.SetLimit(limit).FindAllAsync();

Query with limit and offset:

var limit = 10u;
var offset = 5u;
var users = await userRepository.SetLimit(limit, offset).FindAllAsync();

Query with OrderBy:

var users = await userRepository.SetOrderBy(OrderInfo.SortDirection.DESC, x => x.CreatedAt).FindAllAsync();

Query with SetSelect:

var users = await userRepository.SetSelect(x => new {x.Id, x.Name}).FindAllAsync();

Find all users for AccountId equals to 3 and not logical deleted:

var allUsers = await userRepository.FindAllAsync(x => x.AccountId == 3 && x.Deleted != false);

Example with Asp.Net Core and D.I

Configure Services
//Your DB Provider
MicroOrmConfig.SqlProvider = SqlProvider.MySQL;
//Not required
MicroOrmConfig.TablePrefix = "db1_";
//Add generic SqlGenerator as singleton
services.AddSingleton(typeof(ISqlGenerator<>), typeof(SqlGenerator<>));
//Your db factory
services.AddSingleton<IDbConnectionFactory, DbFactory>(x => new DbFactory(appSettings.DbConnectionString));
Example implements BaseRepository with IDbConnectionFactory
public class BaseRepository<T> : DapperRepository<T> where T : class
{
    private readonly IDbConnectionFactory _factory;
    public BaseRepository(IDbConnectionFactory factory, ISqlGenerator<T> generator)
        : base(factory.OpenDbConnection(), generator)
    {
        _factory = factory;
    }

    protected IDbConnection GetConnection()
    {
        return _factory.OpenDbConnection();
    }
 }

License

All contents of this package are licensed under the MIT license.

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48 net481
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on MicroOrm.Dapper.Repositories:

Package Downloads
Crystal.Dapper

This package contains the classes and methods to work with Dapper

GenericCRUD

Don't repeat yourself! GenericCrud is a highly flexible library that provides your API with a skeleton for CRUD controllers without using EF. You can configure model validation, user permission validation etc.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.24.1 545 1/4/2023
1.24.0 1,034 11/21/2022
1.23.2 4,923 10/16/2022
1.23.1 3,407 9/8/2022
1.23.0 6,166 7/14/2022
1.22.0 9,290 5/1/2022
1.21.0 16,209 2/9/2022
1.20.1 333 2/9/2022
1.20.0 1,535 1/29/2022
1.19.1 13,529 9/19/2021
1.19.0 5,484 8/26/2021
1.18.1 17,948 6/24/2021
1.18.0 4,741 5/21/2021
1.17.0 3,641 4/29/2021
1.16.2 13,774 3/29/2021
1.16.1 2,861 3/12/2021
1.16.0 7,888 2/18/2021
1.15.0 28,273 8/28/2020
1.14.1 95,200 3/1/2020
1.14.0 528 2/27/2020
1.13.0 799 2/18/2020
1.12.0 22,257 1/11/2020
1.11.0 24,454 11/14/2019
1.11.0-beta1 1,598 10/4/2019
1.10.0-beta3 12,804 4/26/2019
1.10.0-beta2 453 4/10/2019
1.10.0-beta1 15,859 8/25/2018
1.8.3 112,805 10/26/2017
1.8.2 1,931 9/16/2017
1.8.1 1,605 8/20/2017
1.8.0 1,325 7/18/2017
1.7.3 1,932 5/30/2017
1.7.2 1,031 5/25/2017
1.7.1 2,523 5/9/2017
1.7.0 1,157 5/8/2017
1.6.6 1,373 4/21/2017
1.6.5 988 4/19/2017
1.6.4 971 4/16/2017
1.6.3 1,106 4/5/2017
1.6.2 4,695 3/28/2017
1.6.1 7,303 3/14/2017
1.6.0 5,738 3/6/2017
1.6.0-beta5 913 2/18/2017
1.6.0-beta4 1,222 12/15/2016
1.6.0-beta3 769 12/14/2016
1.6.0-beta2 790 12/2/2016
1.6.0-beta1 751 12/1/2016
1.5.0 5,105 8/18/2016
1.4.2 3,422 7/20/2016
1.4.1 959 7/19/2016
1.4.0 1,209 7/11/2016
1.4.0-beta3 820 7/7/2016
1.4.0-beta1 1,414 2/23/2016
1.3.3 1,485 11/3/2015
1.3.2 987 10/22/2015
1.3.1 976 10/13/2015
1.2.3 1,077 9/25/2015
1.2.1 1,373 9/9/2015
1.2.0 1,331 8/31/2015
1.0.1 1,369 7/24/2015
1.0.0 1,230 7/7/2015