Skip to content

Parameter binding

Val edited this page Sep 18, 2016 · 8 revisions

Although is possible to use RedisClient composing strings dynamically, it is unrecommended. Providing command templates will increase the performance since then the command execution plan can be cached.

Table of Contents:

Simple binding

Parameter binding works passing an object which properties will be bind to the command parameters, identified by a starting '@'. Supported property types are:

  • Integral types.
  • String.
  • DateTime.
  • Enums.
  • Collections (IEnumerable<>) of the already cited.

Integral types

Values are formatted and parsed according to Redis, so for example independently of which culture is your system running, Double values will be formatted without number group separator and with dot as the decimal symbol.

channel.Execute("incrbyfloat mykey @value", new { value = 3.52D });

Strings

Strings are saved as is in UTF8.

channel.Execute("setex @url @seconds @html", 
                new { url = "cache:" + Request.Url.RawUrl, seconds = 20, html = response });

DateTime

DateTime values are serialized using .ToBinary() and deserialized using .FromBinary(). This way the Kind info is also preserved.

channel.Execute("hset mykey @field @value", new { field = "ModifiedOn", value = DateTime.Now });

Enums

When working with enums as parameters, RedisClient works with the underlying type and not with the string representation.

channel.Execute("hset mykey @field @value", new { field = "Status", value = Status.Closed });

Collections

Any IEnumerable<> of the already cited types is supported. Collection items are sequenced.

channel.Execute("sadd @setKey @data",
                new { setKey = "myset", data = new [] { "a", "b", "c" } })
                .ConfigureAwait(false);

This command will save the values "a", "b" and "c" as part of the set named mySet.

Advanced binding

The Parameter helper class, allows to sequence (ie: convert into a sequence of simple values):

  • Objects, as long the property being bound is of one of the cited types before (but not collections).
  • IEnumerable<Tuple<T1,T2>>, as long T1 and T2 are of one of the cited types before (but not collections).
  • IEnumerable<KeyValuePair<TKey, TValue>>, as long TKey and TValue are of one of the cited types before (but not collections).

Objects

Given an object of class:

public class Customer
{
    public Int32 Id { get; set; }
    public DateTime DateOfBirth { get; set; }
    public String Name { get; set; }
}

The Parameter helper is handy to save the object as hash:

channel.Execute("hmset @customerKey @data",
                new { customerKey = "customer:"+id, data = Parameter.SequenceProperties(myCustomer));

Later on, it is possible to do the reverse process (binding the hash to a class) using output binding.

IEnumerable<KeyValuePair<TKey, TValue>> and IEnumerable<Tuple<T1,T2>>

Similarly, it is possible to use a IEnumerable<KeyValuePair<TKey, TValue>> (and hence a IDictionary<TKey,TValue>) and IEnumerable<Tuple<T1,T2>> as parameters.

This is handy for example for executing ZADD with multiple values.

var values = new List<KeyValuePair<Int32, String>>();
for (int i = 0; i < 10; i++)
    values.Add(new KeyValuePair<Int32, String>(i, "member" + i));

channel.Execute(@"ZADD examplekey @values", new { values = Parameter.Sequence(values)});