Archives

Archives / 2015 / May
  • New features in Dapper.Contrib

    I’ve given the official Dapper.Contrip package some love lately. So now all methods include support for lists! So instead of doing lots of insert calls in a loop, just call connection.Insert(myList); and Dapper/Contrib will interate and insert your entities effectively and fast!

    Dapper.Contrib now contains these methods:

    T Get<T>(id);
    IEnumerable<T> GetAll<T>();
    int Insert<T>(T obj);
    int Insert<T>(IEnumerable<T> list);
    bool Update<T>(T obj);
    bool Update<T>(IEnumerable<T> list);
    bool Delete<T>(T obj);
    bool Delete<T>(IEnumerable<T> list);
    bool DeleteAll<T>();

    For .NET 4.5 users there are also async methods available. For a complete and (almost) up-to-date help page on how Dapper.Contrib works, please visit https://github.com/StackExchange/dapper-dot-net/tree/master/Dapper.Contrib

    Dapper.Contrib can be installed from Nuget, read more at https://www.nuget.org/packages/Dapper.Contrib/

  • Effectively insert list of objects with Dapper

    If you are using Dapper as your sql/object mapper for .Net, you often find yourself inserting lists of objects. There are several ways of doing it, but maybe you're not doing it the fastest way possible (apart from hand-coding)?

    What most people do is iterating over the list add execute the insert for each item like this:

    foreach (var item in myList
    {
        connection.Execute("INSERT INTO MYTABLE VALUES (@A, @B)", item);
    }

    but this is not the fastest way becuse the Dapper Execute() extension takes a list as parameter and iterates for you:

    connection.Execute("INSERT INTO MYTABLE VALUES (@A, @B)", myList);

    I tested by inserting 10.000 simple objects into an SqlCe database and clocked the resuts. The first option took 1.88 seconds, and the second took 1.19 seconds! So, letting Dapper iterate and insert is not twice as fast, but almost!

    To install Dapper, run the following command in the Package Manager Console

    PM> Install-Package Dapper

  • jQuery validate and the comma decimal separator

    Oh this is such a simple and good solution that I must put it on my blog at least for my own future reference. Big THANKS to Lenard Gunda for writing this blog post

    http://blog.rebuildall.net/2011/03/02/jQuery_validate_and_the_comma_decimal_separator

    If you live outside the US and have problems with getting client side validation accepting comma decimal separator in input fields - just overwrite jQuery validation range and number methods with these javascript lines in the end of all your javascript loading.

    $.validator.methods.range = function (value, element, param) {
        var globalizedValue = value.replace(",", ".");
        return this.optional(element) || (globalizedValue >= param[0] && globalizedValue <= param[1]);
    }
     
    $.validator.methods.number = function (value, element) {
        return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:[\s\.,]\d{3})+)(?:[\.,]\d+)?$/.test(value);
    }