MicroOrm.Dapper.Repositories 1.8.3

CRUD for Dapper

There is a newer prerelease version of this package available.
See the version list below for details.
Install-Package MicroOrm.Dapper.Repositories -Version 1.8.3
dotnet add package MicroOrm.Dapper.Repositories --version 1.8.3
paket add MicroOrm.Dapper.Repositories --version 1.8.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

MyGet
NuGet
NuGet

codecov
Codacy Badge
GitHub contributors
Build status
License MIT

GitHub stars
GitHub forks
GitHub watchers

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 generatorand the way it builds each sentence.

The original idea was taken from Yoinbol.

I tested this with MSSQL, PostgreSQL and MySQL.

PM> Install-Package MicroOrm.Dapper.Repositories

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]

[InnerJoin]

[RightJoin]

[UpdatedAt]

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.

Examples

"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
}

Implements the repository:

public class UserRepository : DapperRepository<User>
{

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


    }
}

Query:

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

var allUsers = await userRepository.FindAllAsync(x => x.AccountId == 3 && x.Deleted != false); // all users for account id 3 and not deleted

MyGet
NuGet
NuGet

codecov
Codacy Badge
GitHub contributors
Build status
License MIT

GitHub stars
GitHub forks
GitHub watchers

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 generatorand the way it builds each sentence.

The original idea was taken from Yoinbol.

I tested this with MSSQL, PostgreSQL and MySQL.

PM> Install-Package MicroOrm.Dapper.Repositories

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]

[InnerJoin]

[RightJoin]

[UpdatedAt]

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.

Examples

"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
}

Implements the repository:

public class UserRepository : DapperRepository<User>
{

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


    }
}

Query:

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

var allUsers = await userRepository.FindAllAsync(x => x.AccountId == 3 && x.Deleted != false); // all users for account id 3 and not deleted

Release Notes

https://github.com/phnx47/MicroOrm.Dapper.Repositories/releases

Version History

Version Downloads Last updated
1.10.0-beta2 44 4/10/2019
1.10.0-beta1 10,605 8/25/2018
1.8.3 25,413 10/26/2017
1.8.2 1,031 9/16/2017
1.8.1 498 8/20/2017
1.8.0 555 7/18/2017
1.7.3 873 5/30/2017
1.7.2 295 5/25/2017
1.7.1 415 5/9/2017
1.7.0 304 5/8/2017
1.6.6 604 4/21/2017
1.6.5 292 4/19/2017
1.6.4 267 4/16/2017
1.6.3 422 4/5/2017
1.6.2 3,275 3/28/2017
1.6.1 2,533 3/14/2017
1.6.0 2,268 3/6/2017
1.6.0-beta5 272 2/18/2017
1.6.0-beta4 433 12/15/2016
1.6.0-beta3 219 12/14/2016
1.6.0-beta2 275 12/2/2016
1.6.0-beta1 204 12/1/2016
1.5.0 2,906 8/18/2016
1.4.2 1,225 7/20/2016
1.4.1 249 7/19/2016
1.4.0 464 7/11/2016
1.4.0-beta3 247 7/7/2016
1.4.0-beta1 758 2/23/2016
1.3.3 718 11/3/2015
1.3.2 300 10/22/2015
1.3.1 292 10/13/2015
1.2.3 291 9/25/2015
1.2.1 484 9/9/2015
1.2.0 486 8/31/2015
1.0.1 370 7/24/2015
1.0.0 384 7/7/2015