Implementing Continuations in a Generic Way – The Usage of the Option Type in C# – Make Code More Clear

August 31, 2011 § Leave a comment

Implementing Continuations in a Generic Way – The Usage of the Option Type in C# – Make Code More Clear

(source : Code Project)

Continuations in C# – Make Code More Clear

Problem to solve: If you look at a lot of code in C#, the code must be read from top to bottom, following all the If, Else, Try and other branches to really understand what is happening. Furthermore a lot of this code is basically technically, such as checking on null-values. I wondered if you could write code that reads from left to right, and is in principle not-technical. It reads like a series of operations that makes sense as such. A ‘similar’ concept is known in F# as pipelining. In F# it looks like

Let squared =
 |> List.filter isOdd
 |> List map square;;

The object Numbers are pipelined through the functions that follows. In C# I wanted to do something like this:

  . AndThen(Save)
  . AndThenIfElse(Do_this_If_ThisIsTrue, Do_this_IfOtherwise)

Etc. In this case the SomeObject is passed to function IsValid and then to Save and then to theDo_this_If_ThisIsTrue, which function can return ‘false’, in which case SomeObject is passed toDo_this_ifOtherWise. The beauty of this chaining, is that we can add any number of AndThen,AndThenIfElse as we want.

You could do this years ago using delegates. Delegates are eventually not generic. Now we can use these, but we also want to handle Error handling and null-alike code. Tomas Petricek and John Skeet Real-World Functional Programming comes with the solution. The secret is in essential two things: the Option type and the using of essentially only one fixed interface: (func of Option of T, Option of T), meaning you pass in a function that receives an Option and returns one. This interface can be chained or pipelined as much as you want.

The Option Type is a well-known Type in F#. An Option can be None or Some. Some wraps an actual business object. Continuations is the concept in which you pass the result of a function to another function.

What if you combine those two concepts? There is a lot theory involved, but I won’t bother you with it. If you wrap an object in an Option and then use Continuation, you can keep on chaining as shown in the above AndThen code. The Option object hides the actual type of the object inside it.

Inside the Option can be anything. Using the Option-concept we can let behave a strict typed language in a generic way.

For continuation you can use Lambda’s, but then the typing won’t let you implement these in a generic way.

This article is about implementing Continuation in a generic way.

The Option type is an abstract class with two concrete implementations: None and Some. None ends the chaining, Some encapsulates the [SomeObject]. A function must first get the encapsulated object, do some processing on it and return either a None or a Some with the encapsulated object. The Option Some and None classes are generic!

To this Option type I added some functions, such as the above AndThenIfElse. If you look at the parameters you see that you pass the following two parameters: Func of Option of T, Option of T tryFirst, Func of Option of T, Option of T tryNext, meaning you pass in two functions.

I added

  1. AndThen (continuation). If the previous function returns a Some then continue with the following function
  2. AndThenCheck. You pass in as the first parameter a function that takes an Option and returns a Boolean. If this Boolean is true, the second parameter is executed, which is a continuation. If not true return None.
  3. AndThenCaseCheck. You pass in a list of AndThenCheck. If a check returns true, the accompanying continuation is executed. If none of the checks returns true a None is returned. In the accompanying example I use lambda’s to compose this list. Func’s can be replaced by lambda’s and in this case I thought it was a good idea.
  4. AndThenIfElse. You pass in two functions. If the first return None, the second continuation is executed.

The other interesting thing to note, is that the above pipeline of continuations is not executed until a call to Exec(). The reason to do this, is because Exceptions are handled in the Exec() call only, and the continuations stop with the first None returns. The pipeline is in fact an arraylist of MultiCastDelgates that can be serialized. The option/Continuation concept can be used in other scenario’s as well such as postponing execution by saving until it’s triggered in time.

I added a concrete example how to use this in the real world. The code is not doing much, but gives you a good idea how you can use it. Looking at the code below (ClientController), you see the real benefit:

    .AndThenIfElse(ClientBusiness.CheckOnDustin, ClientBusiness.CheckOnNotDustin)
    .AndThenCheck(ClientBusiness.ANameLengthCheck, ClientBusiness.GetLastOneInLocality)

Does the code read from left to right and does it read as a series of chained operations? Does it ‘hide’ technical handlings in favor for business handlings? What do you think. Let me know.

What Developers Should Know About Microsoft Visual Studio LightSwitch

August 9, 2011 § Leave a comment

What Developers Should Know About Microsoft Visual Studio LightSwitch

The first mention of Microsoft Visual Studio LightSwitch was at the VSLive! event in August of 2010. It’s gone through two beta releases with an announced availability for version 1.0 to MSDN subscribers on July 26. LightSwitch is an intriguing product focused on building lightweightapplications that access a data source and present results through a variety of different screens. You can build fully functional applications without writing any code, although if you need any customization you’ll need to use either Visual Basic or C#.

Business developers are a prime target for LightSwitch. From an application perspective, LightSwitch is aimed at minimally complex applications with straightforward requirements. In many situations the basic need is to grab some data and throw it up on the screen. You might need some simple filter or query capability, but that’s just about it. You might call LightSwitch the ad hoc developer’s tool, although that’s probably not what Microsoft had in mind.

Data is the Key

Accessing multiple kinds of data is, without a doubt, the single common thread amongst most business developers. The data might be stored in a SQL server or even an Access database. Another common task is converting data from one format to another and then storing it in a database. The obvious data sources for a Microsoft shop are SQL Server and Microsoft Access. Other possibilities include SharePoint, SQL Azure and a Windows Communication Foundation (WCF) Rich Internet Application (RIA) Service.

That last one (WCF RIA) is essentially the glue between the presentation tier and data access layer. Creating one of these things is way beyond casual programmer knowledge and would be a potential opportunity for enterprising ISVs. It also highlights the fact that you will need some additional tools if the data you wish to access is stored in something other than a Microsoft product.

Presentation Layer Magic

Creating quick-and-dirty applications is another stated goal of LightSwitch. Visual Basic has filled this role for many years but doesn’t really meet the needs of the casual developer. The biggest issue for creating “custom” business applications is almost always how the information is presented to the user. While Visual Basic does make it possible to build simple applications, it does require a certain amount of coding using the language to add the necessary functionality.

LightSwitch takes care of many of the rudimentary tasks for which you have traditionally had to write code, such as querying a database and filling a grid with the results. It provides a nice visual designer to create your forms and reports. LightSwitch also adds a number of new capabilities for data validation, such as recognizing an e-mail or telephone number field and validating it for you. Be aware that LightSwitch refers to unique data input or output forms as screens.


Independent Software Vendors (ISVs) should take a hard look at LightSwitch from the perspective of potential add-on features. LightSwitch has basic database connectivity capabilities out of the box but doesn’t even begin to address all of the data sources businesses need to service. It’s also somewhat limited in terms of the customized screen components although the usual vendors here have already announced support.

The good news for programmers already familiar with Visual Basic or C# is you don’t have to learn anything new. In fact, you can take your VB or C# skills and put them to work right away with LightSwitch. If you want to get started now, you’ll find an Extensions Cookbook and sample project available on the MSDN site.

The Rum | Sailor Jerry

August 5, 2011 § Leave a comment

The Rum | Sailor Jerry

Sailor Jerry is a straight-up, no-nonsense rum.

We craft the spirit from a selection of rums distilled in the Caribbean. Our master blenders “marry” the rums to our exacting recipe, then infuse it with our one-of-kind mix of spices and other natural flavours, most notably vanilla and a touch of cinnamon.

The result is high-quality, old-school spiced rum. An enduring classic, not a fly-by-night fancy.

Intense vanilla, dry buttery toffee and subtle cinnamon notes

Warm spices of cinnamon & nutmeg with rich vanilla. A long dry finish balanced with a subdued sweetness and a hint of burnt toffee

Smoothness is the key to Sailor Jerry’s versatility. Enjoy it how you like it — straight, mixed or in a cocktail.

How it was Born:
Simply put, the guys who started the company were Sailor Jerry fans. They created Sailor Jerry Limited to keep alive the legacy of the man, Norman ‘Sailor Jerry’ Collins.

They started it as a small mom and pop clothing brand in Philadelphia, collaborating with local artists to make leather goods, sneakers, t-shirts and other products featuring Sailor Jerry’s flash. Since Sailor Jerry spent half his life as a sailor (and the other half tattooing sailors) it was a natural move to create a spiced rum in his name.

A cult started to grow around the rum, and pretty soon, it started to outsell anything else they made.

Background and History:
Maybe because it was hauled around the globe by sailors or maybe because it’s just good, rum has traditionally been the most prolific of the world’s spirits. Although a few other places lay claim to their own varieties, the Caribbean is generally accepted as the heart of the rum world (which is why it’s where Sailor Jerry is and will always be distilled!)

Rum has been intertwined with Caribbean culture for nearly 400 years, coming into existence alongside the sugar industry. The 1600s were the coming-out century for rum. Quality grew by leaps and bounds and so did people’s taste for it. By 1789, a hogshead of the finest Caribbean rum was chosen as fit for the first presidential inauguration of an upstart nation called the United States of America. Today’s Caribbean rum distillers have well over 200 years of experience perfecting the art of distillation, aging and blending.

Adding spices to rum is an old sailor’s tradition. Those first rums were crude and harsh, so seamen would blend and mix in any spices they had on hand to improve the rum’s flavour. Today the harshness is no more, but the art of spicing rum is alive and well.

Where Am I?

You are currently viewing the archives for August, 2011 at Naik Vinay.