Dapper.Rainbow A Getting Started Tutorial for CRUD Operations
Overview
Dapper.Rainbow is a small library that contains an abstract class that you can add to your project and use as a base class for your Dapper classes to provide CRUD operations, such as inserting, deleting, updating, and getting records.
- Get
- Insert
- Update
- Delete
- All
Dapper.Rainbow is a wrapper for database interactions and will create SQL based on property names and type constraints.
- It is built on top of Dapper and makes use of the existing dapper methods.
- The main goal is to make working with Dapper even easier by providing a simpler and more consistent API.
NuGet Installation
Dapper.Rainbow is available through NuGet: https://www.nuget.org/packages/Dapper.Rainbow/
You can easily install this library by running the following command:
PM> Install-Package Dapper.Rainbow
More information and documentation can be found at: https://github.com/StackExchange/Dapper/tree/master/Dapper.Contrib
APIs
Once you installed this library, you will need to create a new class derived from the Database
class MyDatabase : Database<MyDatabase> { public Table<Invoice> Invoices { get; set; } }
You can now use CRUD methods easily in your code.
using (var connection = My.ConnectionFactory()) { connection.Open(); var db = MyDatabase.Init(connection, commandTimeout: 2); int? invoiceId = db.Invoices.Insert(new { Kind = InvoiceKind.WebInvoice, Code = "Insert_Single_1" }); var invoice = db.Invoices.Get((int)invoiceId); db.Invoices.Update(invoice.Id, new { Kind = InvoiceKind.StoreInvoice, Code = "Update_Single_1" }); invoice = db.Invoices.Get(invoice.Id); var status = db.Invoices.Delete(invoice.Id); var invoices = db.Invoices.All(); }
Unfortunately, there is no proper documentation available for this library.
Limitations
Current limitations:
- There is no support for composite key mapping.
- The identity column name for all tables must be called Id.
Support
This library is supported regularly and you will get your answers within the next few days. https://github.com/StackExchange/Dapper/issues
ZZZ Projects