Skip to content

scmarinelli/Dommel

 
 

Repository files navigation

Dommel

Simple CRUD operations for Dapper.


Windows Linux OS X
Build status Build Status Build Status

--

Dommel provides a convenient API for CRUD operations using extension methods on the IDbConnection interface. The SQL queries are generated based on your POCO entities. Dommel also supports LINQ expressions which are being translated to SQL expressions. Dapper is used for query execution and object mapping.

Dommel also provides extensibility points to change the bevahior of resolving table names, column names, the key property and POCO properties. See Extensibility for more details.


Download

Download Dommel on NuGet


The API

Retrieving entities by id
using (var con = new SqlConnection())
{
   var product = con.Get<Product>(1);
}
Retrieving all entities in a table
using (var con = new SqlConnection())
{
   var products = con.GetAll<Product>().ToList();
}
Selecting entities using a predicate

Dommel allows you to specify a predicate which is being translated into a SQL expression. The arguments in the lambda expression are added as parameters to the command.

using (var con = new SqlConnection())
{
   var products = con.Select<Product>(p => p.Name == "Awesome bike");
   
   var products = con.Select<Product>(p => p.Created < new DateTime(2014, 12, 31) && p.InStock > 5);
}
Inserting entities
using (var con = new SqlConnection())
{
   var product = new Product { Name = "Awesome bike", InStock = 4 };
   int id = con.Insert(product);
}
Updating entities
using (var con = new SqlConnection())
{
   var product = con.Get<Product>(1);
   product.LastUpdate = DateTime.Now;
   con.Update(product);
}
Removing entities
using (var con = new SqlConnection())
{
   var product = con.Get<Product>(1);
   con.Delete(product);
}

Query builders

Dommel supports building specialized queries for a certain RDBMS. By default, query builders for the following RDMBS are included: SQL Server, SQL Server CE, SQLite, MySQL and Postgres. The query builder to be used is determined by the connection type. To add or overwrite an existing query builder, use the AddSqlBuilder() method:

DommelMapper.AddSqlBuilder(typeof(SqlConnection), new CustomSqlBuilder());

Extensibility

ITableNameResolver

Implement this interface if you want to customize the resolving of table names when building SQL queries.

public class CustomTableNameResolver : DommelMapper.ITableNameResolver
{
    public string ResolveTableName(Type type)
    {
        // Every table has prefix 'tbl'.
        return "tbl" + type.Name;
    }
}

Use the SetTableNameResolver() method to register the custom implementation:

DommelMapper.SetTableNameResolver(new CustomTableNameResolver());
IKeyPropertyResolver

Implement this interface if you want to customize the resolving of the key property of an entity. By default, Dommel will search for a property with the [Key] attribute, or a column with the name 'Id'.

If you, for example, have the naming convention of {TypeName}Id for key properties, you would implement the IKeyPropertyResolver like this:

public class CustomKeyPropertyResolver : DommelMapper.IKeyPropertyResolver
{
    public PropertyInfo ResolveKeyProperty(Type type)
    {
        return type.GetProperties().Single(p => p.Name == string.Format("{0}Id", type.Name));
    }
}

Use the SetKeyPropertyResolver() method to register the custom implementation:

DommelMapper.SetKeyPropertyResolver(new CustomKeyPropertyResolver());
IColumnNameResolver

Implement this interface if you want to customize the resolving of column names for when building SQL queries. This is useful when your naming conventions for database columns are different than your POCO properties.

public class CustomColumnNameResolver : DommelMapper.IColumnNameResolver
{
    public string ResolveColumnName(PropertyInfo propertyInfo)
    {
        // Every column has prefix 'fld' and is uppercase.
        return "fld" + propertyInfo.Name.ToUpper();
    }
}

Use the SetColumnNameResolver() method to register the custom implementation:

DommelMapper.SetColumnNameResolver(new CustomColumnNameResolver());

The Dapper.FluentMap.Dommel extension implements these interfaces using the configured mapping. Also see: Dapper.FluentMap.

About

Simple CRUD operations for Dapper.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 97.0%
  • PowerShell 1.7%
  • Shell 1.1%
  • Batchfile 0.2%