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.
MicroOrm.Dapper.Repositories alternatives and similar packages
Based on the "ORM" category.
Alternatively, view MicroOrm.Dapper.Repositories alternatives based on common mentions on social networks and blogs.
-
Dapper
Dapper - a simple object mapper for .Net [Moved to: https://github.com/DapperLib/Dapper] -
TypeORM
ORM for TypeScript and JavaScript. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, SAP Hana, WebSQL databases. Works in NodeJS, Browser, Ionic, Cordova and Electron platforms. -
Entity Framework
EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations. -
SqlSugar
.Net ORM Fastest ORM Simple Easy Sqlite orm Oracle ORM Mysql Orm postgresql ORm SqlServer oRm 达梦 ORM 人大金仓 ORM 神通ORM C# ORM , C# ORM .NET ORM NET5 ORM .NET6 ORM ClickHouse orm QuestDb -
FreeSql
🦄 .NET orm, C# orm, VB.NET orm, Mysql orm, Postgresql orm, SqlServer orm, Oracle orm, Sqlite orm, Firebird orm, 达梦 orm, 人大金仓 orm, 神通 orm, 翰高 orm, 南大通用 orm, 虚谷 orm, 国产 orm, Clickhouse orm, QuestDB orm, MsAccess orm. -
FluentMigrator
Fluent migrations framework for .NET -
NHibernate
NHibernate Object Relational Mapper -
EFCore.BulkExtensions
Entity Framework EF Core efcore Bulk Batch Extensions with BulkCopy in .Net for Insert Update Delete Read (CRUD), Truncate and SaveChanges operations on SQL Server, PostgreSQL, MySQL, SQLite -
PetaPoco
Official PetaPoco, A tiny ORM-ish thing for your POCO's -
ServiceStack.OrmLite
Fast, Simple, Typed ORM for .NET -
Dapper Extensions
Dapper Extensions is a small library that complements Dapper by adding basic CRUD operations (Get, Insert, Update, Delete) for your POCOs. For more advanced querying scenarios, Dapper Extensions provides a predicate system. The goal of this library is to keep your POCOs pure by not requiring any attributes or base class inheritance. -
Entity Framework 6
This is the codebase for Entity Framework 6 (previously maintained at https://entityframework.codeplex.com). Entity Framework Core is maintained at https://github.com/dotnet/efcore. -
Massive
A small, happy, dynamic MicroORM for .NET that will love you forever. -
LINQKit
LINQKit is a free set of extensions for LINQ to SQL and Entity Framework power users. -
NPoco
Simple microORM that maps the results of a query onto a POCO object. Project based on Schotime's branch of PetaPoco -
SmartSql
SmartSql = MyBatis in C# + .NET Core+ Cache(Memory | Redis) + R/W Splitting + PropertyChangedTrack +Dynamic Repository + InvokeSync + Diagnostics -
SQLProvider
A general F# SQL database erasing type provider, supporting LINQ queries, schema exploration, individuals, CRUD operations and much more besides. -
LINQ to Twitter
LINQ Provider for the Twitter API (C# Twitter Library) -
Dapper.FastCRUD
fast & light .NET ORM for strongly typed people -
MongoDB Repository pattern implementation
Repository abstraction layer on top of Official MongoDB C# driver -
MongoDB.Entities
A data access library for MongoDB with an elegant api, LINQ support and built-in entity relationship management -
JsonFlatFileDataStore
Simple JSON flat file data store with support for typed and dynamic data. -
LINQtoCSV
Popular, easy to use library to read and write CSV files. -
MongoFramework
An "Entity Framework"-like interface for MongoDB -
DbExtensions
Data-access framework with a strong focus on query composition, granularity and code aesthetics. -
NReco.Data
Fast DB-independent DAL for .NET Core: abstract queries, SQL commands builder, schema-less data access, POCO mapping (micro-ORM). -
AsyncPoco
A long-"awaited" fully asynchronous PetaPoco fork -
Venflow
A brand new, fast and lightweight ORM, build for PostgreSQL. -
Linq.Expression.Optimizer
System.Linq.Expression expressions optimizer. http://thorium.github.io/Linq.Expression.Optimizer -
KonfDB
Configuration as a Service for multi-tenant, cross-platform applications -
Dashing
Dashing is a simple to use mini ORM built on top of Dapper -
MapDataReader
Super fast mapping DataReader to strongly typed object, Using AOT source generator. -
Dapper.MicroCRUD
CRUD Extensions for Dapper.Net -
ObjectStore
.Net Or-Mapper working with dynamically implemented abstract Classes -
MongoRiver.NET
A library for writing .NET MongoDB oplog tailers. -
DatabaseObjects
.NET Object Relational Mapping Tool -
AutoClutch
Generic repository and generic service. -
EntityFramework.DatabaseMigrator
EntityFramework.DatabaseMigrator is a WinForms utility to help manage Entity Framework 6.0+ migrations. -
Excel2SqlServer
Library for importing Excel spreadsheets into SQL Server tables
Access the most powerful time series database as a service
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of MicroOrm.Dapper.Repositories or a related project?
README
MicroOrm.Dapper.Repositories
I see a lot Issues, but I don't have enough time to solve them. Feel free to open Pull Request or ask to add you as Member to support project!
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. details see https://github.com/DapperLib/Dapper/issues/633 https://github.com/DapperLib/Dapper/issues/637 https://github.com/vauto/Dapper.Database/pull/1
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();
}
}
Contributors
Contribute
Contributions to the package are always welcome!
- Report any bugs or issues you find on the issue tracker.
- You can grab the source code at the package's git repository.
License
All contents of this package are licensed under the MIT license.
*Note that all licence references and agreements mentioned in the MicroOrm.Dapper.Repositories README section above
are relevant to that project's source code only.