Mkb.DapperRepo 2.3.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Mkb.DapperRepo --version 2.3.0
                    
NuGet\Install-Package Mkb.DapperRepo -Version 2.3.0
                    
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="Mkb.DapperRepo" Version="2.3.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Mkb.DapperRepo" Version="2.3.0" />
                    
Directory.Packages.props
<PackageReference Include="Mkb.DapperRepo" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Mkb.DapperRepo --version 2.3.0
                    
#r "nuget: Mkb.DapperRepo, 2.3.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Mkb.DapperRepo@2.3.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Mkb.DapperRepo&version=2.3.0
                    
Install as a Cake Addin
#tool nuget:?package=Mkb.DapperRepo&version=2.3.0
                    
Install as a Cake Tool

DapperRepo

Simple dapper repo with async implementation.

designed to generate simple crud operations while still being lite weight

Currently supported and tested for these providers

Provider Db Connection Nuget Package
Sql SqlConnection System.Data.SqlClient
MySql MySqlConnection MySql.Data
PostgreSQL NpgsqlConnection Npgsql
Sqlite SqliteConnection Microsoft.Data.Sqlite

It may work for others.

(sql lite I recently added, I made no changes to the repo just added test setup to confirm it worked).

Please note due to wanting to support multiple Db providers certain choices have been made that are for compatibility over optimisation. The repo its self has no scope over any provider (nor should it).

Setup

All examples are done with the standard repo but will work with the async version to (will need to be awaited). Full examples can be found on the github repo with in the test project.

Initializing a new repo

  new SqlRepo(()=> new DbConnection());
  
  e.g
  new SqlRepo(()=> new SqlConnection("connection string"));     // MsSql
  new SqlRepo(()=> new MySqlConnection("connection string"));   // MySql
  new SqlRepo(()=> new NpgsqlConnection("connection string"));  // PostgreSQL
  new SqlRepo(()=> new SqliteConnection("connection string"));  // Sqlite

or of course via DI

  
  // or where ever you want to get the connection string from it from
  // you can ofc use Scoped or Single instead if you wish but depends on sql implementation and how it handles connections
  // would check dapper and sql providers docs for best practice per implementation 
  
  services.Configure<ConnectionStrings>(Configuration.GetSection(nameof(ConnectionStrings)));
  services.AddTransient(r => new SqlConnection(r.GetService<IOptions<ConnectionStrings>>().Value.SqlDb));
  services.AddScoped(r => new SqlRepoAsync(()=>r.GetService<SqlConnection>));
  

by taking a DbConnection directly it does support multiple providers. Allowing a abstract way to interact with data. Regardless of the db tech.

Usages

Creating entities

please note the primary key attribute found in DapperRepo.PrimaryKeyAttribute.cs

    [SqlTableName("Test_Table")]         // can be used to get around pluralization and also unconventional table names  
    public class TableModel
    {
        [PrimaryKey] // Required for repo to work effectively
        public Guid Id { get; set; }
// if a field is auto generated by the db you can set it to be nullable i.e int? 
// if value is null unless told repo will not try to insert null fields but update will this can be overriden using ignoreNullProperties on the update method 
   
        public string Name { get; set; }
        public string Email { get; set; }
        [SqlColumnName("some_number")]      // this can be used to map columns in sql for unconventional names
        public int SomeNumber { get; set; }
        
        [SqlIgnoreColumn]           // this will tell the dapper repo to ignore this property and not map it to the entitiy
        public string ToBeIgnored { get; set; }
    }

Count

     var count = Repo.Count<TableModel>();

Get All

Repo.GetAll<TableModel>().ToArray();

GetAllByX

Repo.GetAllByX<TableModel, int>("SomeNumber", 35);

Repo.GetAllByX<TableModel, int>(nameof(TableModel.SomeNumber), 35);

Query single

// feel free to use top 1 or limit 1 depending on implementation of the sql provider
Repo.QuerySingle<TableModel>(
                "select * from TableModel where SomeNumber = 33");
         
// or with param    
Repo.QuerySingle<TableModel>(
                "select * from TableModel where SomeNumber = @num",new {num =33});

Query Multiple

Repo.QueryMany<TableModel>(
                "select * from TableModel where SomeNumber = 33");

// or with param                
Repo.QueryMany<TableModel>(
                "select * from TableModel where SomeNumber = @num",new {num=33});

Get By Id

Repo.GetById(new TableModel {Id = Guid.Parse("....")});
Repo.GetById(new TableModelWithIntId {Id =  325});

GetExactMatches

Know details of a Entity but not its id (like one you have just added)

  // false as 2nd param indicates to not to ignore nulls i.e some number has to be null for its model to be returned
  var results = Repo.GetExactMatches(new TableModel {Name = "Michale", SomeNumber = null}, false); 
  
  // true as 2nd param indicates to ignore nulls i.e some number can be anything in this case
  var results = Repo.GetExactMatches(new TableModel {Name = "Michale", SomeNumber = null}, true); 

Delete

var item =Repo.GetById(new TableModel {Id = Guid.Parse("....")});
Repo.Delete(item);

Add

  var testTableItem = new TableModel() {Name = "Michael", SomeNumber = 44};
  Repo.Add(testTableItem);

Update

update command is using only primary key in the where clause. so strictly speaking if you wish to update all the values on a row and know its primary key a get is not required


 var item = Repo.GetById(new TableModel {Id = Guid.Parse("....")});
 item.Name = "mike"
 item.Email = null
 Repo.Update(testTableItem); // will update properites and also null properties so db will set email to null

 // Example 2
 var item = Repo.GetById(new TableModel {Id = Guid.Parse("....")});
 item.Name = "mike"
 item.Email = null
 
 Repo.Update(testTableItem, true);   // will ignore null properties and only update name in db

Search And Search Count

Search will bring you back the entities and search count will give you the number of records.

Count Is more light weight if you just need a any equivalent.

Repo.Search<TableModel>(nameof(TableModel.Name), "%cha%");    // recommended 

// or for specific types
Repo.Search<TableModel, int>(
                    nameof(TableModel.SomeNumber), 33, SearchType.Equals);

// and search multiple criteria

 Repo.Search(new TableModel
            {
                Name = "%hae%",
                SomeNumber = 9
            }, new[]
            {
                SearchCriteria.Create(nameof(TableModel.NameTest), SearchType.Like),
                SearchCriteria.Create(nameof(TableModel.SomeNumber), SearchType.Equals)
            });
            
  Repo.SearchCount(new TableModel
            {
                Name = "%hae%",
                SomeNumber = 9
            }, new[]
            {
                SearchCriteria.Create(nameof(TableModel.NameTest), SearchType.Like),
                SearchCriteria.Create(nameof(TableModel.SomeNumber), SearchType.Equals)
            });
            
           // this will allow you to search where name like "%hae%" 
           // and SomeNumber less than equal to 9
            

Getting Tests working

Connection.cs contains connection config these are not unit tests they do require a db and will spin one up on fly but for a repo seems better than mocking

   public static string MasterConnectionString = "Server=localhost;Database=master;Trusted_Connection=True";

also please note there are separate getall and insert methods that are raw in DataBaseScriptRunnerAndBuilder.cs as the tests don't use the implemenation they are are testing for setups or verifies.

Located in CreateDbWithTestTable.Sql you will find the create database scripts.

Special Thanks to armanx

Notes:

V2.3.0

Brings with it a new exception PrimaryKeyNotFoundException. You should get this when trying to do updates or deletes entities that are missing a primary key attributes.

This should not be breaking as currently it will throw a null ref exception which is not very descriptive

SqlColumnName:

Uses simple reflection on the class looking for both class property names and sql col names (set via attribute) and sets up mapping to both. Check query tests for more details.

Please note as most sql servers implementations are not case sensitive nor is the mapper. Meaning having 2 fields named the same thing with different casing in the same class this may result in undesirable results.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.5.0 608 3/25/2025
2.4.0 211 1/23/2025
2.3.2 271 4/12/2024
2.3.1 193 4/10/2024
2.3.0 996 5/14/2023
2.2.8 670 5/1/2023
2.2.7 745 3/16/2023
2.2.5 909 11/25/2022
2.2.0 1,100 9/19/2022
2.1.0 1,069 9/3/2022
2.0.0 1,567 3/31/2022
1.0.4 1,019 3/12/2022
1.0.3 1,394 4/17/2021
1.0.2 1,690 4/16/2021