Giter Site home page Giter Site logo

dapper.nona's Introduction

Nona

Micro ORM that runs on Dapper. Thanks to https://github.com/henkmollema/Dommel for the initial idea. We started this project since we don't want to use Entity Framework, we had a look on the market and decided to improve Dommel. We decided to create a new repository because we are thinking a different approach and these repositories will be different.


Nona provides a convenient API for CRUD operations using extension methods on the `IDbConnection` interface. The SQL queries are generated based on your POCO entities. Nona also supports LINQ expressions which are being translated to SQL expressions. [Dapper](https://github.com/StackExchange/dapper-dot-net) is used for query execution and object mapping.

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

Windows NuGet MyGet
AppVeyor NuGet MyGet Pre Release

Download

Dapper.Nona is available on NuGet:


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

Nona 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);
}
using (var con = new SqlConnection())
{
   var products =new List<Product>
   {
        new Product { Name = "Awesome bike", InStock = 4 },
        new Product { Name = "Awesome bike 2", InStock = 5 }
   };

   int count = con.Insert(products);
}

Updating entities

using (var con = new SqlConnection())
{
   var product = con.Get<Product>(1);
   product.LastUpdate = DateTime.Now;
   con.Update(product);
}
using (var con = new SqlConnection())
{
   var products = con.Select<Product>(p=>p.Id==1 || p.Id==2).ToList();
   products[0].LastUpdate = DateTime.Now;
   products[1].LastUpdate = DateTime.Now;
   con.Update(products);
}

Removing entities

using (var con = new SqlConnection())
{
   var product = con.Get<Product>(1);
   con.Delete(product);
}
using (var con = new SqlConnection())
{
   var products = con.Select<Product>(p=>p.Id==1 || p.Id==2);
   con.Delete(products);
}

Query builders

Nona 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:

NonaMapper.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 : ITableNameResolver
{
    public string ResolveTableName(Type type)
    {
        // Every table has prefix 'tbl'.
        return $"tbl{type.Name}";
    }
}

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

NonaMapper.SetTableNameResolver(new CustomTableNameResolver());

IKeyPropertyResolver

Implement this interface if you want to customize the resolving of the key property of an entity. By default, Nona 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 : IKeyPropertyResolver
{
    public PropertyInfo ResolveKeyProperty(Type type)
    {
        return type.GetProperties().Single(p => p.Name == $"{type.Name}Id");
    }
}

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

NonaMapper.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 : IColumnNameResolver
{
    public string ResolveColumnName(NonaProperty property)
    {
        // Every column has prefix 'fld' and is uppercase.
        return $"fld{property.Name.ToUpper()}";
    }
}

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

NonaMapper.SetColumnNameResolver(new CustomColumnNameResolver());

```csharp
public class ProductMap : NonaEntityMap<T>
{
    public ProductMap()
    {
        ToTable("tblProduct");
        
        // ...
    }
}
NonaPropertyMap<T>

This class derives PropertyMap<T> and allows you to specify the key property of an entity using the IsKey method:

public class ProductMap : NonaEntityMap<T>
{
    public ProductMap()
    {
        Map(p => p.Id).IsKey();
    }
}

In the FluentMapper.Initialize() method you have to call ApplyToNona() in order to tell Nona to use fluent mapping:

FluentMapper.Initialize(config =>
    {
        config.AddMap(new ProductMap());
        config.ApplyToNona();
    });

dapper.nona's People

Contributors

edmondshtogu avatar getson avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.