Skip to content

Latest commit

 

History

History

Harbin.DataAccess.Repositories.DapperSimpleCRUD

README.md

Description

Data Access library based on Dapper, Dapper.FastCRUD, and DapperQueryBuilder.
Implements Generic Repositories (Generic Repository Pattern) but yet allows us to extend those Repositories with custom Queries and Commands.
Helps to manage connections to distributed databases and/or read-replicas.

Design Principles

Harbin Database Library was designed based on the following ideas:

  • This is a "Bare metal" library - it does not try to "hide" or abstract ADO.NET or Dapper, so you can use the full power of Dapper, IDbTransactions, etc.
  • There are classes for read-only connections (ReadOnlyDbConnection, ReadOnlyDbConnection<DB>) and for read-write connections (ReadWriteDbConnection, ReadWriteDbConnection<DB>).
  • By isolating read-only connections and read-write connections we can use read-replicas for scaling-out reads on large scale apps.
  • All connection classes are wrappers around IDbConnection but which also implement IDbConnection so they can be used as regular connections.
  • If your app uses multiple databases (e.g. distributed databases, microservices, heterogeneous databases) it's easy to manage those multiple connections using generic classes ReadOnlyDbConnection<DB> and ReadWriteDbConnection<DB>.
  • Read connections can build ReadRepository<TEntity> and read-write connections can build ReadWriteRepository<TEntity> - those are Generic Repositories (Generic Repository Pattern).
  • ReadRepository<TEntity> has methods to invoke Dapper Query extensions (Query, QuerySingle, QueryAsync, etc).
  • ReadRepository<TEntity> has methods to invoke DapperQueryBuilder methods, so you can easily add dynamic filters to your queries.
  • ReadWriteRepository<TEntity> has Insert / Update / Delete methods which use Dapper FastCRUD to automatically generate the CRUD as long as your entities are decorated with attributes like [Key] and [DatabaseGenerated(DatabaseGeneratedOption.Identity)] .
  • By isolating read-only repositories and read-write repositories we can isolate Queries and Commands (CQRS) - Queries should be added to ReadRepository and DbCommands to ReadWriteRepository.
  • Repositories are "persistence-based repositories" (as described by Vaughn Vernon in "Implementing Domain-Driven Design" book), and do NOT emulate a collection of objects (as described by Eric Evans in "Domain-Driven Design" book).
  • Repositories and Connections can be extended either through inheritance (recommended) or through method extensions.
  • It's possible to register a custom repository type (e.g. PersonRepository child of ReadWriteRepository<Person>) to be used instead of the generic repository.
  • Your Application Logic/Services can be tested by registering fake repository implementations or by using a mocking library.
  • Everything is testable: connection wrappers and their underlying connections are also mockable.

Installation

Just install nuget package Harbin.DataAccess.Repositories.DapperSimpleCRUD, add using Harbin.DataAccess.Connections, using Harbin.DataAccess.Repositories.DapperSimpleCRUD, and start using (see examples below).
See documentation below, or more examples in unit tests.

Documentation

Define your Entities

You should create classes for your Entities. Since your INSERT/UPDATE/DELETE queries are automatically generated by Dapper.FastCRUD you should use it's mapping syntax:

  • Table attribute (if class name doesn't match table name)
  • Schema attribute (if not using default schema - e.g. MSSQL "dbo")
  • [Key] on columns which are part of Primary Key
  • [DatabaseGenerated(DatabaseGeneratedOption.Identity)] on columns which are identity
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

// AdventureWorks is a sample database which uses schemas for grouping tables
// If table uses default schema and table name matches class name you don't need the [Table] attribute
[Table("ContactType", Schema = "Person")]
public partial class ContactType
{
    [Key] // column is part of primary key
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] // column is auto-increment
    public int ContactTypeId { get; set; }

    public DateTime ModifiedDate { get; set; }

    public string Name { get; set;