Opinionated API

time to read 5 min | 871 words

Recently I found myself doing a lot more API design that use the following method. A method that takes a delegate which accepts a parameter that allow you to perform the actual operations that are required.

This is very good for things like handling semantics, and since we only allow access to the desired operation through the parameter to the delegate, we can be pretty sure that we will have a consistent operation experience. Let us take the following example:

   1: table.Batch(actions =>
   2: {
   3:     actions.Put("test", new int[0], new byte[] { 1 });
   4:     actions.Put("test", new int[0], new byte[] { 2 });
   5:     var values = actions.Get("test");
   6:     Assert.Equal(2, values.Length);
   7:  
   8:     Assert.Equal(1, values[0].Version);
   9:     Assert.Equal(new byte[] { 1 }, values[0].Data);
  10:  
  11:     Assert.Equal(2, values[1].Version);
  12:     Assert.Equal(new byte[] { 2 }, values[1].Data);
  13: });

As an aside, it is 3 AM now and I have been coding almost non stop since 9AM yesterday. I hate it when I have idea rush.