Popularity
0.9
Stable
Activity
9.0
Stable
2
1
0

Description

Nuget package Dapper.CX.SqlServer makes it easy to do CRUD operations on pure POCO model classes via IDbConnection extension methods. The only model class requirement is that they have a property called Id or the class has an Identity attribute that indicates what its identity property is. int and long identity types are supported.

Programming language: C#
Tags: ORM     SqlServer     Dapper     POCO     ORM-Micro    

Dapper.CX alternatives and similar packages

Based on the "ORM" category

Do you think we are missing an alternative of Dapper.CX or a related project?

Add another 'ORM' Package

README

Build status Nuget Nuget

Nuget package Dapper.CX.SqlServer makes it easy to do CRUD operations on pure POCO model classes via IDbConnection extension methods. See method reference. The only model class requirement is that they have a property called Id or the class has an Identity attribute that indicates what its identity property is. int and long identity types are supported. So, after installing the package and you start calling the extension methods, choose either the Extensions.Int or Extensions.Long namespace:

img

Here's a simple example using GetAsync and SaveAsync methods assuming a fictional Appointment model class and fictional GetConnection method:

using (var cn = GetConnection())
{
    var appt = await cn.GetAsync<Appointment>(id);

    // make your changes

    await cn.SaveAsync(appt);
}

The SaveAsync method performs an insert or update depending on whether the model object IsNew or not. Here's a more sophisticated example showing the GetWhereAsync method and ChangeTracker object. Learn more about tracking changes.

using (var cn = GetConnection())
{
    var appt = await cn.GetWhereAsync<Appointment>(new { clientId = 4343, date = new DateTime(2020, 3, 1) });
    var ct = new ChangeTracker<Appointment>(appt);

    // make your changes

    // with a change tracker object, only modified properties are included in update statement 
    await cn.SaveAsync(appt, ct);  
}

Wiki links: Why Dapper.CX?, Reference. Note that Dapper.CX doesn't create tables. Please see my ModelSync project for info on that.

Customizing behaviors with interfaces

There are some interfaces you can use on model classes to implement validation and custom Get behavior. To use these, your model class project must add package AO.Models as a dependency. Here's a sample of things you can implement:

  • IPermission lets you check whether the current user has permission to save, delete, or get a row from the database. This is for role-based permission checks.

  • IAudit lets you standardize how user and timestamps are saved throughout your application. See also IUserBase.

  • ITenantIsolated lets you specify a tenant Id value on a model instance that can be compared with an ITenantUser TenantId to see if they have permission to that record.

  • ITrigger works much like database triggers, letting you specify events to happen after a row is saved or deleted.

  • ICustomGet lets you inject portions of the SQL statements that Dapper.CX generates. This is so you can add model properties that are populated from a custom query, but not part of your base table. See the test for an example, along with the related model class. Here's where ICustomGet is invoked here and here. In that example, the properties Balance and Whatever aren't columns in the base table, but are queried during Get operations as if they are. (Note also that such properties use the NotMapped attribute to prevent Dapper.CX from attempting to bind them.)

  • IGetRelated lets you implement navigation properties by injecting a delegate in which you can perform additional gets every time a model is queried. See the test for an example. This uses the same sample model class above. Here's where IGetRelated is invoked internally.

  • IValidate lets you perform validation on a model class prior to an insert or update. See test and related model class for example. Note that IValidate has two methods Validate and ValidateAsync. The async version passes a connection argument so you can perform that requires looking up something in the database. The sync version is for validating properties of the model that don't require any database lookup. Here's where IValidate is invoked internally.

And one other thing...

In addition to the more common strong-typed CRUD operations, Dapper.CX also offers a SqlCmdDictionary feature that gives you a clean way to build INSERT and UPDATE statements dynamically.


Please see also Dapper.QX, Dapper.CX's companion library.