Module Pattern, Variations and Common Practices

In JavaScript application, the module pattern is a popular design pattern that encapsulates ‘privacy’, state and organization using closures.  A module helps keep units of code cleanly separately and organized. It provides a way of wrapping a mix of public and private methods and variables, protecting pieces from leaking into the global scope and accidentally colliding with another developer’s interface. No matter if you are using any JavaScript framework or not, module pattern is one of the best practices in JavaScript development to organize and limit code scope in any project. Module pattern helps you to structure your code base more towards a Object-Oriented fashion as we are doing in C# or Java.

  • The JavaScript language doesn’t have class, but we can emulate what classless can do with modules.

  • A module helps encapsulate data and function into a single component

  • A module limits scope so the variables you create in the module only live within it.

  • A module gives privacy by only allowing access to data and functions that the module wants to expose.

Basic Module Pattern Structure

The core of module pattern is based on IIEF (Immediate Invoked Function Expression). It is a anonymous function, execute immediately. All of the code run inside the function lives in a closure, which provide privacy and state through the life time of the application.

// All Private
(function() {
    // Detail logic goes here
}()); //calling this method.

The code snippet above hide all of your implementation from external code. The function runs once and only once.

Module Pattern and Variations

Anonymous Object Literal

A version evolved from the simple IIEF is return a anonymous object literal that would expose privilege methods

var Module = (function() {
    // private ...
    var privateVariable = "some priviate value",
        privateMethod = function() {
            //do something.
        };

    return {
        //the method inside the return object are
        //called as privileged method because it has access
        //to the private methods and variables of the module.
        privilegedMethod: function() {
            //this method can access its private variable and method by using the principle of closure.
            console.log("outputting " + privateVariable); //accessing private variable.
            privateMethod(); //calling private method
        }
    };

})();

By using the principle of closure, the privileged method can access its private variables and methods. The returned object literal will be added to Window (global) name space. It returns the only parts we needs, leaving the other code out of the global scope.

Locally scoped object literal

Another variation is to create and return an object containing all of the public members as its properties.

var Module = (function() {
    // locally scoped Object
    var myObject = {};

    // declared with 'var', must be "private"
    var privateMethod = function() {};

    myObject.someMethod = function() {
        // make it a public method
    };
    return myObject; // only the object send back, not the name

})();

Again, the private member inside the module is created using closure. The module is created by using anonymous function expression. Any declared function or variable will be private unless they are added as properties to the returned public object.

Revealing Module Pattern

Revealing module pattern is very common pattern, widely used JavaScript applications. Comparing with Locally scoped object literal version above, Revealing Module Pattern doesn’t create a separate returned object and then adding properties to it. But instead, we define all the function private first, then decide which ones should be public by return those methods as properties of an anonymous object literal.

var Module = (function() {

    var privateMethod = function() {
        // private
    };
    var someMethod = function() {
        // public
    };
    var anotherMethod = function() {
        // public
    };
    return {
        someMethod: someMethod,
        anotherMethod: anotherMethod
    };

})();

it is a very flexible variation of Module Pattern. It can better control the name of the public API. If it is required to change the name, all we need to do is to change the name from the returned object without any impact of the function name inside the module. Also, it controls what should be public by adding/removing the properties.

Common Practices

Passing External Library

Most of time, our module needs to reference other third party libraries, like jQuery or Kendo. JQuery uses ‘$’ as shortcut to reference JQuery element, but ‘$’ is not just used by jQuery. To avoild confilict, it is a common practices to pass ‘$’ as a parameter to the Module.

var Module = (function($) {
    var privateMethod = function() {
        // private
    };

    var someMethod = function(x) {
        // use jQuery function/features
        if ($.isArray(x)) {
            // do something
        }
    };

    var anotherMethod = function() {
        // public
    };

    return {
        someMethod: someMethod,
        anotherMethod: anotherMethod
    };

})(jQuery);

Reference Other Modules

Module can be referenced by other modules, which provides a compsite solution.

var Module01 = (function() {
    var sayHelloPrivate = function(name) {
        console.log("Hello, " + name);
    };

    return {
        sayHello: function(name) {
            sayHelloPrivate(name);
        }
    };
}());

var Module2 = (function(m1) {
    return {
        startThingsOff: function(name) {
            m1.sayHello(name);
        }
    };
}(Module01));

Extending Module Pattern (augmentation)

Module can be extended or overwritten by other module as well.

// extending and overwriting the module
var Module1 = (function(oldModule) {
    var
    //assigning oldmodule in to a local variable.
        parent = oldModule;
    //overriding the existing privileged method.
    parent.privilegedMethod = function() {
        //do something different by overriding the old method.
    };

    //private method accessing privileged method of parent module.
    var privateMethod2 = function() {
        //can access privileged method of Module
        parent.privilegedMethod();
        //can access public method of Module
        parent.publicMethod1();
    }
    return {
        newMethod: function() {
            ///do something for this brand new module.
            ///use some functionality of parent module.
            /// parent.privilegedMethod( );
        }
    };

})(Module); //Module object is the existing module that I want to extend.

Passing Name Space Parameter

Besides other module or library can be passed to the module as argument, it is very common practice to take name space parameter to define name space for the module.

(function(skillet, $, undefined) {
    //Private Property
    var isHot = true;

    //Public Property
    skillet.ingredient = 'Bacon Strips';

    //Public Method
    skillet.fry = function() {
        var oliveOil;

        addItem('tn Butter nt');
        addItem(oliveOil);
        console.log('Frying ' + skillet.ingredient);
    };

    //Private Method
    function addItem(item) {
        if (item !== undefined) {
            console.log('Adding ' + $.trim(item));
        }
    }

}(window.skillet = window.skillet || {}, jQuery));

Consideration of Multiple Instance

If the module will be created multiple instance, we can apply prototype to save memory. Because, instead of each instance having a copy of the member, the single prototype member is shared. So we can return a constructor function from the module.

// code from http://stackoverflow.com/questions/21909785/java-script-revealing-module-pattern-create-multiple-objects

var Slider = (function($) {

    function slider(id) {
        this.id = id;
    }

    slider.prototype = {
        init: function() {
            this.pageLoad();
        },
        pageLoad: function() {
            console.log('pageLoad called from instance with id', this.id);
        },
        getId: function() {
            return this.id; // <- 'this' refers to the current instance
        }
    };

    return slider;

})(jQuery);

var slider1 = new Slider(1);
var slider2 = new Slider(12);

Organizing Modules with different Files

We already see how to extend the module previously. In some situation when the JavaScript module is overly complicate, We can apply this approach to separate the module into multiple files. You see, in the same below, I split the entire Module implementation into two separate files. If we understand how the module references to be extended, we will have no problem understand this code structure. In both files, it take Module as parameter to extend its functions.

// Module.js
var Module = (function($, pub) {
    // jQuery will still be available via $
    var mem = new Array(); //private variable

    pub.publicMethod01 = function(val) {
        mem.push(val);
    };

    pub.publicMethod02 = function() {
        return mem.pop();
    };
    return pub;

})(jQuery, Module || {});

// Module-Additional.js
var Module = (function($, pub) {
    pub.publicMethod03 = function(val) {
        // do something
    };

    pub.publicMethod04 = function() {
        // do something else
    };
    return pub;

})(jQuery, Module || {});

Wrap up: Today I walked through the Module Pattern, it variations and some common usage. I don’t intend to give very detailed explanation of  Pro and Con of each of them. I hope we can start to use this elegant pattern. It is first step to get our JavaScript code organized.

Dapper.NET Tutorial III

In the last two posts, we discuss how to use Dapper.net to simplify the CRUD operation and how the APIs are enhanced by using Dapper Contrib project. Today, I am going to show another existing enhancement built on top of Dapper, Dapper Rainbow. Dapper Rainbow is shipped as a separate dll, you can reference it into your project by using NuGet.

The main purpose of Rainbow, I think, is to response the feedback from the users that the original APIs for Insert and update are more like raw ADO.net than a ORM. Rainbow project provides a Table class and some extension methods to encapsulate the tedious Sql statements.

  • Insert A new entity

    First we need to create a Database class inherited from Dapper.Rainbow.Database, with a property as type of Table<Supplier>. The table class includes a set of extension methods for the operations of CRUD. Another interesting feature is it can provide an anonymous object for insert.

  • public class NorthwindDatabase : Database
    {
        public Table<Supplier> Suppliers { get; set;  } 
    }
    
     using (var sqlConnection
                 = new SqlConnection(Constant.DatabaseConnection))
    {
        sqlConnection.Open();
    
        var db = NorthwindDatabase.Init(sqlConnection, commandTimeout: 2);
        int? supplierId = db.Suppliers.Insert(
                                new
                                 {
                                    CompanyName = Guid.NewGuid().ToString()
    
                                 });
    
        Console.WriteLine(string.Format("New Supplier Id is {0}", supplierId.Value));
    }
  • Update A entity The enhancement on Update function is to provide a SnapShotter class, which can help track if the any field has changes. It will generate update statement only if any field has been changed. The update Sql statement will only include the fields have been changed. Both of them optimize the overall update performance. 
  • using (var sqlConnection
                       = new SqlConnection(Constant.DatabaseConnection))
    {
        sqlConnection.Open();
        
        var db = NorthwindDatabase.Init(sqlConnection, commandTimeout: 3);
    
        var supplier = db.Suppliers.Get(9);
    
        // snapshotter tracks which fields change on the object 
    
        var s = Snapshotter.Start(supplier);
    
        supplier.CompanyName += "_" + Guid.NewGuid().ToString().Substring(1, 4);
    
        db.Suppliers.Update(9, s.Diff());
    
        // reload it from database 
        supplier = db.Suppliers.Get(9);
    
        ObjectDumper.Write(supplier);
    }
  • Select All/One entity If you like to use the Table API for query, you probably will only end up with some simple scenarios. Table provides ALL, First and Get methods, which is good enough for simple case. If you need to build complex query, you had better switch to user Query method.
using (var sqlConnection = new SqlConnection(Constant.DatabaseConnection))
{
    sqlConnection.Open();

    var db = NorthwindDatabase.Init(sqlConnection, commandTimeout: 2);
    
    var result = db.Suppliers.All();

    foreach (var supplier in result)
    {
        ObjectDumper.Write(supplier);
    }

}
    • Wrap Up

    In the those three posts, I show how to use Dapper and its related projects to create simple and elegant data access function. Well, personally I really like those approaches. In my current point of view, data access layer should be simple and flexible. Don’t get me wrong, I still like NHibernate and Linq2Sql. I just think it is not a “either/or” solution. We can have all of them working together in one single project. Recently I more and more give up the “Repository” pattern and use Query/Command instead. I think Query/Command provides and more flexible way to encapsulate the data persistence. Data access should be easier and that is what it should be.

    Again, here is the code used in those posts.

Happy Programming!

Dapper .NET Tutorial II

In previous blog, I showed some common scenario about how to use Dapper .NET. Today, I will show how to apply Dapper Contrib project to enhance the API.

Dapper Contrib is a set of extension methods built upon the core of Dapper (SqlMapper). At this moment, there is no a separate dll can be referenced into your project, which means you cannot use NuGet to add it into the project. You can open the file and put into your project.

Dapper Contrib, not only provide intuitive APIs for CRUD, but also add couple interesting features, like SqlBuilder and “Dirty” Tracking.

  • Get a item

    Instead of using Query method, it provides a Get method to take the value of Id

    using (var sqlConnection = new SqlConnection(Constant.DatabaseConnection)) { sqlConnection.Open(); var result = sqlConnection.Get(9); ObjectDumper.Write(result); }

  • Insert a item

    Compare the Sql-like syntax, this Insert method has improved a lot from code readable perspective.

    using (var sqlConnection
                = new SqlConnection(Constant.DatabaseConnection))
    {
        sqlConnection.Open();
    
        var supplier = new Supplier()
                           {
                               Address = "10 Main Street",
                               CompanyName = "ABC Corporation"
                           };
    
        var supplierId = sqlConnection.Insert(supplier); 
    
        sqlConnection.Close();
    
        Console.WriteLine(string.Format("New Supplier Id is {0}", supplierId));
    
        Console.WriteLine("Done. ");
    }
  • Update a item

    Similar as inserting item, Dapper Contrib allows to update a entity object, instead of using raw Sql-like syntax.

    using (var sqlConnection = new SqlConnection(Constant.DatabaseConnection))
    {
        sqlConnection.Open();
    
        var entity = sqlConnection.Get(9);
        entity.ContactName = "John Smith"; 
    
        sqlConnection.Update(entity);
    
        var result = sqlConnection.Get(9);
    
        ObjectDumper.Write(result.ContactName);
    }
  • Update a item by using tracking feature

    Dapper .net provides a nice feature to determine if the update statement is really required. If the value does not change, it won’t generate the Sql statement, which is very handy performance optimization. The only requirement is we need to declare a interface for the object. Here is how to use:

// declare a interface for the object
public interface ISupplier
{
    
    int Id { get; set; }

    string CompanyName { get; set; }
    string ContactName { get; set; }
    string ContactTitle { get; set; }
    string Address { get; set; }
    string City { get; set; }
    string PostalCode { get; set; }
    string Country { get; set; }
    IEnumerable Products { get; set; } 
}

public class Supplier : ISupplier
{
    public int Id { get; set; }
    public string CompanyName { get; set; }
    public string ContactName { get; set; }
    public string ContactTitle { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string PostalCode { get; set; }
    public string Country { get; set; }
    public IEnumerable Products { get; set;  }  
}
using (var sqlConnection = new SqlConnection(Constant.DatabaseConnection))
{
    sqlConnection.Open();

    // using interface to track "Isdirty"
    var supplier = sqlConnection.Get(9);
    //supplier.CompanyName = "Manning"; 

    // should return false, becasue there is no change. 
    ObjectDumper.Write(string.Format("Is Update {0}", sqlConnection.Update(supplier)));
    
    supplier.CompanyName = "Manning";

    // should return true
    ObjectDumper.Write(string.Format("Is Update {0}", sqlConnection.Update(supplier)));

}
  • Sql Builder

      Dapper Contrib includes a SqlBuilder class, which can be used to create a dynamic Sql statement. It will be very helpful to be used in for a customized searching scenario with multiple options. It is very common need when developing application. From the example below, you see, a template Sql statement is redefined. In this case, I need to set up the “Select” and “Where” clause dynamically. We define the place holder as “/**select**/” and “/**where**/” respectively. Then we can add code to define what we need.

    using (var sqlConnection = new SqlConnection(Constant.DatabaseConnection))
    {
        sqlConnection.Open();
        var builder = new SqlBuilder();
    
        // /**select**/  -- has to be low case
        var selectSupplierIdBuilder = builder.AddTemplate("Select /**select**/ from Suppliers /**where**/ ");
        builder.Select("Id");
        builder.Where("City = @City", new { City = "Tokyo"}); // pass an anonymous object 
    
        var supplierIds = sqlConnection.Query(selectSupplierIdBuilder.RawSql,
                                                   selectSupplierIdBuilder.Parameters);
        
        ObjectDumper.Write(supplierIds);
    
        sqlConnection.Close();
        
    }

Wrap up

In this post, I show how to improve the API by using Dapper Contrib project. Also SqlBuilder class is handy utility to help build dynamic Sql. In the next post, I will show some example using Dapper RainBow. Stay tuned! Again, you can get the source code from here.

Happy Programming!

Dapper .NET Tutorial I

Introduction

One of the common operations when developing a business application is to access the data from relational database. As a .NET developer, we have gone through a very interesting journey to learn how to apply the “best” approach for the data access layer.

Not long ago, “stored procedure” is still the default approach. Until .NET 3.5 was released, more and more .NET developers started to use Linq2Sql, then recently Entity Framework. Some other ALT .Net developers might like to use open source ORM like NHibernate. There is nothing wrong with any of the options there, as long as you know how to use it correctly.

But things don’t go well some time. It requires developers have good knowledge of the ORM tools they are using. It is so easy to get wrong. The well-known “Select + 1” issue is just one of them. Besides that, the sql code generated by ORM, like Linq2Sql and Entity Framework is too complicate, far from optimized. It can make DBA cry.

Both of the framework heavily utilize Linq Expression, which cause big performance overhead when translating to underneath native SQL statements. Fortunately, some smart developers have already realized that. They take a different approach to create so-called micro-ORM. Those frameworks are built directly upon ADO.NET. They are using reflection (dynamic from .NET 4.0) to generate object from the result of data reader. They are simple and perform well. Some of them include:

How to get Dapper

Today, I like to talk about Dapper .net, which is part of the building blocks used in Stackoverflow. This framework is original created by Sam Saffron who is generous to offer it as open source for the whole .NET community. Dapper perform very well because it uses dynamic method generation (MSIL) to assign column values to properties.

There have two ways to reference Dapper in your project.

  1. Since the core of Dapper.net is only one file. It can be easily added to the project. You can directly access its repository and grab the file
  2. In the meanwhile, it is can be obtained from Nuget. If you already installed Nuget. You can install it directly downloads it.  Dapper-Nuget

Using Dapper

Dapper is really simple to use. In this post, I will demonstrate some common usage. In the following blogs, I will show how to use Dapper Contrib and Dapper Rain bow.

Before we start access the database, let’s build our domain objects. Using Northwind as the backend database, I mainly use Product and Supplier table to demonstrate.

First let’s create the POCO objects for Product and Supplier:

public class Product { public int Id { get; set; } public string ProductName { get; set; } public int SupplierID { get; set; } public int CategoryID { get; set; } public string QuantityPerUnit { get; set; } public decimal UnitPrice { get; set; } public short? UnitsInStock { get; set; } public short? UnitsOnOrder { get; set; } public short? ReorderLevel { get; set; } public bool Discontinued { get; set; } // reference public Supplier Supplier { get; set; } }

// Supplier public class Supplier { public int Id { get; set; } public string CompanyName { get; set; } public string ContactName { get; set; } public string ContactTitle { get; set; } public string Address { get; set; } public string City { get; set; } public string PostalCode { get; set; } public string Country { get; set; } public IEnumerable<Product> Products { get; set; } }

  • Select List

    using (var sqlConnection = new SqlConnection(Constant.DatabaseConnection)) { sqlConnection.Open(); IEnumerable products = sqlConnection.Query("Select * from Products"); foreach (Product product in products) { ObjectDumper.Write(product); }

    sqlConnection.Close(); }

    When you run the sample, you can see a list of POCO product objects returned from a ADO-like command. How simple is that! Internally, Dapper .net use MSIL to access data reader then convert to the domain object.

    • Select items by applying parameters To prevent SQL injection, Dapper provide the parameters, similar as using ADO.NET stored procedure.  It is convenient to take an anonymous object as the parameter

    using (var sqlConnection = new SqlConnection(Constant.DatabaseConnection)) { sqlConnection.Open(); IEnumerable products = sqlConnection .Query("Select * from Products where Id = @ProductId", new {ProductID = 2}); foreach (Product product in products) { ObjectDumper.Write(product); }

    sqlConnection.Close(); }

    • Return dynamic object You can return a list of dynamic object (.NET 4.0), which simplified  the code a lot by removing the code to define customized DTO objects if you need to build a object by using the data from different tables.

    using (var sqlConnection = new SqlConnection(connectionString)) { sqlConnection.Open(); IEnumerable products = sqlConnection .Query("Select * from Products where Id = @Id", new {Id = 2}); foreach (dynamic product in products) { ObjectDumper.Write(string.Format("{0}-{1}", product.Id, product.ProductName)); }

    sqlConnection.Close(); }

    • Retrieve multiple objects with one query

      With the feature as QueryMutiple, dapper can return multiple object/list in one query. It definitely improve the performance.

    using (var sqlConnection = new SqlConnection(Constant.DatabaseConnection))
    {
        sqlConnection.Open();
    
        var query = @"
                   SELECT * FROM dbo.Suppliers WHERE Id = @Id
    
                   SELECT * FROM dbo.Products WHERE SupplierID = @Id
    
                    ";
    
        // return a GridReader
        using (var result = sqlConnection.QueryMultiple(query, new {Id = 1}))
        {
            var supplier = result.Read().Single();
            var products = result.Read().ToList();
    
            ObjectDumper.Write(supplier);
    
            Console.WriteLine(string.Format("Total Products {0}", products.Count));
            
            ObjectDumper.Write(products);
        }   sqlConnection.Close(); 
    }
    • Retrieve object with referenced object

      Dapper not only can get the domain directly, but also can retrieve the reference objects together.

    using (var sqlConnection = new SqlConnection(Constant.DatabaseConnection)) { sqlConnection.Open(); IEnumerable products = sqlConnection .Query( @"select Products.*, Suppliers.* from Products join Suppliers on Products.SupplierId = Suppliers.Id and suppliers.Id = 2", (a, s) => { a.Supplier = s; return a; }); // use splitOn, if the id field is not Id or ID foreach (Product product in products) { ObjectDumper.Write(product.Supplier); }

    sqlConnection.Close(); }

    • Select one object with collection of child object Although, by default, dapper .net does not support this feature. But you can find the code posted by Sam from Statckoverflow. It uses QueryMutiple to return multiple lines, then use a dictionary to group the related key (product.SupplierId, in the sample above) together.

     

    private static IEnumerable QuerySupplier()
    {
       using (var sqlConnection = new SqlConnection(Constant.DatabaseConnection))
        {
            sqlConnection.Open();
    
            var query =
                @"
                   SELECT * FROM dbo.Suppliers WHERE ContactName = 'Charlotte Cooper'
    
                   SELECT * FROM dbo.Products WHERE SupplierID 
                IN (SELECT Id FROM dbo.Suppliers WHERE ContactName = 'Charlotte Cooper')
                 ";
    
            return sqlConnection
                .QueryMultiple(query)
                .Map(supplier => supplier.Id,
                                             product => product.SupplierID,
                                            (supplier, products) => 
                                            { supplier.Products = products; });
        }
        sqlConnection.Close(); 
    }
    public static IEnumerable Map
            (this SqlMapper.GridReader reader,
             Func firstKey, 
             Func secondKey, 
             Action> addChildren    
           )
    {
                   var first = reader.Read().ToList();
                   var childMap = reader
                       .Read()
                       .GroupBy(s => secondKey(s))
                       .ToDictionary(g => g.Key, g => g.AsEnumerable());
    
                   foreach (var item in first)
                   {
                       IEnumerable children;
                       if(childMap.TryGetValue(firstKey(item), out children))
                       {
                           addChildren(item,children);
                       }
                   }
    
           return first;
    }
    • Insert using Query interface

      If we like to retrieve the new Id after insertion, we can use Query interface to retrieve the new identifier directly

    using (var sqlConnection = new SqlConnection(Constant.DatabaseConnection))
    {
        sqlConnection.Open();
    
        var supplier = new Supplier()
        {
            Address = "10 Main Street",
            CompanyName = "ABC Corporation"
        };
    
        supplier.Id = sqlConnection.Query(
                            @"
                                insert Suppliers(CompanyName, Address)
                                values (@CompanyName, @Address)
                                select cast(scope_identity() as int)
                            ", supplier).First(); 
    
        sqlConnection.Close();
    
        Console.WriteLine(supplier.Id);
    
        Console.WriteLine("Done. ");
    }
    • Insert using Sql Extension . Insertion can be applied by using a simple Insert statement.  
    using (var sqlConnection
                = new SqlConnection(Constant.DatabaseConnection))
    {
        sqlConnection.Open();
    
        var supplier = new Supplier()
        {
            Address = "10 Main Street",
            CompanyName = "DEF Corporation"
        };
    
        sqlConnection.Execute(
                            @"
                               insert Suppliers(CompanyName, Address)
                               values (@CompanyName, @Address)
                            ",  supplier); 
    
        sqlConnection.Close();
    
        Console.WriteLine("Done. ");
    }
    • Update Item Updating a item has similar syntax as inserting. It uses update Sql statement with an anonymous object as the parameter.
    using (var sqlConnection = new SqlConnection(Constant.DatabaseConnection))
    {
        sqlConnection.Open();
    
        var updateStatement = @"Update Products Set UnitPrice = @UnitPrice
                                Where Id = @ProductId
                                ";
    
        sqlConnection.Execute(updateStatement, new
                                                   {
                                                       UnitPrice = 100.0m,
                                                       ProductId = 50
                                                   });
        sqlConnection.Close();
    }

      Wrap up

      In this post, we show some common data access scenarios by applying the “Core” implementation of Dapper.NET. Besides that, Dapper.NET has provided some other different API to make the data operation more intuitively. In the next post, we will show how to use Dapper Contri

      You can get the source code from here.

      Happy Programming!

    • The Basic of Recursive Function in F#

      Coming from imperative paradigm, I have to admin I rarely use recursion in my code design. Because it is very natural way to use loop or to encapsulate the business logic in "Iterator" (even one of the patterns from GOF).

      But in functional language, like F#, it is very natural to design application using recursion, which make the code more concise, succinct and expressive. It tells “What”, instead of “How”.

      Let’s take a simple Factorial function as an example to explain how recursion works.

      let rec factorial n =
           if n = 0 then 1
           else
              n * factorial (n -1)

       

      The function looks simple, which is exactly the same way as its mathematical expression.

      But if we provide a very large number such as 1,000,000, it might raise StackOverFlow exception, because the compiler use stack to store the functions. The stack space is very precious, only 1 M. It sounds like the recursion is not applicable for processing large data. Fortunately, F# compiler can optimize recursion by applying “Tail Recursion”.

      “Tail Recursion” is a special recursive function which does not including any other execution after the recursive call, meaning there is no “pending operation”

      Obviously, the factorial function above is not “Tail Recursion” because it has to multiply n. How do we solve it? A common approach is to use accumulator pattern. Here is the code:

      let rec factorialTR n a =
          if n = 0 then a
          else
             factorialTR (n - 1) (n * a)

       

      The idea is, instead of accumulating the functions in the stack, the code use a “accumulator” type to get the temporal value, which is (n * a) in the function. When it function reaches its base, it only needs to return the “accumulator” value. You see, in this case, compiler does not need to store additional additional functions in the stack any more. Makes sense, right?

      Is Factorial function too simple? OK, let’s take some “real world” samples.

      1. Sum a List (Just be aware the the list in F# is linked list)

      let tempList = [1 .. 1000000]
      
      // Non Tail Recursion Version
      let rec sumList lst =
          match lst with
          | [] -> 0
          | hd :: tl -> hd + sumList tl 
      
      sumList tempList
      
      // Tail recursion version
      let sumListTR list  =
          let rec innerSum list total =
               match list with
               | [] -> 0 // initial value
               | hd :: tl ->
                  let ntotal = hd + total
                  innerSum tl ntotal // don't need to store in stack
          innerSum list 0

       

      The first one is not “Tail Recursion” because the recursion function is not the “last” thing, it has to append the value with hd.

      The second one is “Tail Recursion” since it calculates the “AsOf” value and the temporal value is one of the parameter passed in the function. There is nothing needed to be stored in the stack.

      2. Map function (Note: Map is one of high order functions common in Functional Language)

      // Non-Tail Recursive version map  function
      let rec map f list =
          match list with
          | [] -> []
          | hd :: tl -> (f hd) :: (map f tl)
      // Tail Recursion Version
      
      let mapTR f list =
          let rec loop f list acc =
              match list with
              | [] -> acc
              | hd :: tl -> loop f tl (f hd :: acc)      loop f (List.rev list) []

       

      “Tail Recursion” makes it a very useful in our tool box. But it might reach its own limitation that “Tail Recursion” is allow one recursion call in a function. If there have more, we cannot covert it to “Tail Recursion”, so we cannot take the optimization offered by F# compiler.

      Take a sample when we process a Tree:

      type Tree<'a> =
        | Node of 'a * Tree<'a> * Tree<'a>
        | Leaf of 'a
      
      // Non Trail Recursion version
      let rec treeSize = function
        | Leaf _ -> 1
        | Node(_, left, right) -> treeSize  left + treeSize  right
      
      // Trail Recursion version
      let  treeSizeTR tree =
        let rec size_acc tree acc =
          match tree with
          | Leaf _ -> 1 + acc
          | Node(_, left, right) ->
              let acc = size_acc left acc // Non Tail Recursion on left
              size_acc right acc          // Tail Recursion on right
        size_acc tree 0

       

      You can see we provide two version of codes to process a tree. The first one is a normal approach by calling two recursive functions. It is interesting to see the second one. As you can see we only have one “Tail Recursion” when processing right side tree. So the whole solution still cannot be optimized by F# compiler.  Now what we should do?

      Let’s think about how we apply “accumulator” pattern to solve “Trail Recursion” issue again. We pass a temporal value as an argument to the function. Just be ware, in functional language like F#, function is value too. It can be passed and returned! It is a fundamental in functional programming, but it can help us to solve very tricky issue. OK, let me tell you the answer first, then explain.

      The answer is to applying continuous passing style (CPS) by passing another function as the “remaining” of the recursive function call.  So what does it mean? Still confused? Well, actually it is really confusing. It is not very easy to understand when you see it first time. Still, let’s use Fibonacci function we used in the early to illustrate how continuations works.

      let  fibonacciCPS n =
        let rec fibonacci_cont a cont =
          if a <= 2 then cont 1
          else
            fibonacci_cont (a - 2) (fun x ->
              fibonacci_cont (a - 1) (fun y ->
                cont(x + y)))
      
        fibonacci_cont n (fun x -> x)

       

      This implementation takes a inner recursive function by passing the number and a continuation function. Actually the continuation function is a  closure function.

      1. when a <=2, it returns by applying the identifier function (fun x –> x)

      2. When a > 2, it recursively calls to reach its base case by calling fibonacci_cont (a – 2), and in the same time, it builds up the closure functions. x is the result of fibonacci_cont (a –  2), and y is the result of fibonacci_cont (a –1). The add x and y.

      3. Just be aware we use closure here, which use heap, instead of stack.

      It really makes the logic inside out. It does not seem a very natural way when we design the code. It made my head hurt when first seeing this code. But later, I think it is very smart to pass closure function like that. It is really the beautiful side of functional programming.

      Well, this blog might be too length. I will continue discussing recursive function later. Now let me finish this blog by putting the code to process a tress using continuation at the end.

      let  treeSizeCont tree =
        let rec size_acc tree acc cont =
          match tree with
          | Leaf _ -> cont (1 + acc)
          | Node(_, left, right) ->
              size_acc left acc (fun left_size ->
                size_acc right left_size cont)
      
        size_acc tree 0 (fun x -> x)

       

       

      Happy programming!

      Really Useful Windows Shortcuts

      KeyboardOne the of the tips I heard to become a productive developer is to use mouse as few as possible, put the fingers on the keyboard.

      Mouse clicking is sort of distraction from we are doing. Combining with any of the launcher tool, such as Slickrun, you will find you can definitely accomplish your work faster, more effectively and a lot fun!

      Don’t try to memorize all of them at once. When you feel you need to use mouse, just do some search to see if any shortcut available. Here is the list I am using a lot as a .NET developer (visual studio and Resharper, of course!)

      Here are some of the window-based shortcuts I collected and use very often. Hope it will help.

      Visual Studio Shortcut Keys

      Shortcut Keys Description
      Ctrl + X Cut the current selected item to clipboard
      Ctrl + C Copies the current selected item to the clipboard
      Ctrl + V Pastes the item in the clipboard at the cursor
      Ctrl + Z Undo previous editing action
      Ctrl + Y Redo the previous undo action
      Esc Closes a menu or dialog, cancels an operation in progress, or places focus in the current document window
      Ctrl + S Saves the selected files in the current project (usually the file that is being edited)
      Ctrl + Shift + S Saves all documents and projects
      Shift + F12 Finds a reference to the selected item or the item under the cursor
      Ctrl + End Moves the cursor to the end of the document
      Ctrl + Home Moves the cursor to the start of the document
      Ctrl + G Displays the Go to Line dialog
      Ctrl + Right Arrow Moves the cursor one word to the right
      Ctrl + Left Arrow Moves the cursor one word to the left
      Ctrl + K, Ctrl + C Marks the current line or selected lines of code as a comment
      Ctrl + K, Ctrl + U Removes the comment syntax from the current line or currently selected lines of code
      Ctl + J Lists members for statement completion when editing code
      Ctrl + R, Ctrl + W Shows or hides spaces and tab marks
      Shift-Left Arrow Moves the cursor to the left one character, extending the selection
      Ctrl + Shift + End Moves the cursor to the end of the document, extending the selection
      Ctrl + Shift + Home Moves the cursor to the start of the document, extending the selection
      Shift + Down Arrow Moves the cursor down one line, extending the selection
      Shift + End Moves the cursor to the end of the current line, extending the selection
      Shift + Home Moves the cursor to the start of the line, extending the selection
      Shift + Up Arrow Moves the cursor up one line, extending the selection
      Shift + Page Down Extends selection down one page
      Shift + Page Up Extends selection up one page
      Ctrl + A Selects everything in the current document
      Ctrl + Shift + Page Down Moves the cursor to the last line in view, extending the selection
      Ctrl + Shift + Page Up Moves the cursor to the top of the current window,
      Ctrl + Shift + Alt + Right Arrow Moves the cursor to the right one word, extending the column selection
      Ctrl + Shift + Left + Arrow Moves the cursor one word to the left, extending the selection
      Ctrl + Shift + B Build the solution
      Ctrl + Shift + N Display the New Project dialog
      Ctrl + O Display open the file dialog
      Ctrl + Shift + O Display open project dialog
      Shift + Alt + A Display the Add Existing Item dialog
      Ctrl + Shift + A Display the Add New Item dialog
      Shift + Alt + Enter Toggles full screen mode
      Ctrl + Tab Cycles through the MDI child windows one by one
      Ctrl + F Display the Find dialog
      Ctrl + Shift + F Display the Find in Files dialog
      F3 Find Next
      Ctrl + H Display the Replace dialog
      Ctrl + Shift + H Display the Replace in Files dialog


      Basic Windows shortcut keys

      Shortcut Keys Description
      Alt+F File menu options in current program.
      Alt+E Edit options in current program
      F1 Universal Help in almost every Windows program.
      Ctrl+A Select all text.
      Ctrl+X Cut selected item.
      Shift+Del Cut selected item.
      Ctrl+C Copy selected item.
      Ctrl+Ins Copy selected item
      Ctrl+V Paste
      Shift+Ins Paste
      Home Goes to beginning of current line.
      Ctrl+Home Goes to beginning of document.
      End Goes to end of current line.
      Ctrl+End Goes to end of document.
      Shift+Home Highlights from current position to beginning of line.
      Shift+End Highlights from current position to end of line.
      Ctrl+Left arrow Moves one word to the left at a time.
      Ctrl+Right arrow Moves one word to the right at a time.
      Alt+Tab Switch between open applications.
      Alt+Shift+Tab Switch backwards between open applications.
      Alt+double-click Display the properties of the object you double-click on.
      Ctrl+Tab Switches between program groups or document windows in applications
      Ctrl+Shift+Tab Same as above but backwards.
      Alt+Print Screen Create a screen shot only for the program you are currently in.
      Ctrl+Alt+Del Reboot the computer and/or bring up the Windows task manager.
      Ctrl+Esc Bring up the Windows Start menu.
      Alt+Esc Switch Between open applications on taskbar.
      Alt+F4 Closes Current open program.
      Ctrl+F4 Closes Window in Program.
      Shift + Del Delete programs/files without throwing them into the recycle bin.
      Holding Shift Boot Safe Mode or by pass system files as the computer is booting
      Holding Shift When putting in an audio CD, will prevent CD Player from playing.
      Ctrl+F9 Minimize current window.
      Ctrl+F10 Maximize currently selected window.
      Windows Logo Start menu
      Windows Logo+R Run dialog box
      Windows Logo+M Minimize all
      SHIFT+Windows Logo+M Undo minimize all
      Windows Logo+F1 Help
      Windows Logo+E Windows Explorer
      Windows Logo+F Find files or folders
      Windows Logo+D Minimizes all open windows and displays the desktop
      CTRL+Windows Logo+F Find computer
      CTRL+Windows Logo+TAB Moves focus from Start, to the Quick Launch toolbar, to the system tray
      Windows Logo+TAB Cycle through taskbar buttons
      Windows Logo+Break System Properties dialog box
      Windows Logo+L Log off Windows
      Windows Logo+P Starts Print Manager
      Windows Logo+C Opens Control Panel
      Windows Logo+V Starts Clipboard
      Windows Logo+R Open and Run Window


      Web Browser common shortcut Keys

      Shortcut Key Description
      Ctrl + N Open a new window
      Ctrl + T Open a new tab
      Ctrl + W Close the current window
      Ctrl + R Refresh
      Esc Stop
      Alt + <- Back
      Alt + -> Forward
      Alt + Home Go to your homepage
      Alt + D Move focus to the address bar to type URL
      Ctrl + Enter Add “http://www.” and “.com” around an address


      Mozilla Firefox shortcut Keys

      Shortcut Keys Description
      Alt + Home Home
      Alt + Left Arrow Back a page.
      Backspace Back a page.
      Alt + Right Arrow Forward a page.
      F5 Refresh current page, frame, or tab.
      F11 Display the current website in full screen mode. Pressing F11 again will exit this mode.
      F3 Find Again
      Esc Stop page or download from loading.
      Ctrl + (- or +) Increase or decrease the font size, pressing ‘-‘ will decrease and ‘+’ will increase.
      Ctrl + Enter Quickly complete an address. For example, type computerhope in the address bar and press CTRL + ENTER to get http://www.computerhope.com
      Shift + Enter Complete a .net instead of a .com address.
      Ctrl + Shift + Enter Complete a .org address.
      Ctrl + Shift + Del Open the Clear Data window to quickly clear private data.
      Ctrl + W Close the current window or tab
      Ctrl + D Add a bookmark for the page currentlys opened.
      Ctrl + I Display available bookmarks.
      Ctrl + J Display the download window.
      Ctrl + N Open New browser window.
      Ctrl + P Print current page / frame.
      Ctrl + T Opens a new tab.
      Ctrl + F4 Closes the currently selected tab.
      Ctrl + Shift + T Undo the close of a window.
      Ctrl + Tab Moves through each of the open tabs.
      Ctrl + Shift + Tab Swich to the previous tab
      Ctrl + G Find the previous occurrence of a search phrase
      Ctrl + K Move cursor to the Web Search Widget (top right of the screen)
      Spacebar Moves down a page at a time.
      Shift + Spacebar Moves up a page at a time.
      Alt + Down arrow Display all previous text entered in a text box and/or available options on drop down menu.


      Internet Explorer Shortcut

      Shortcut Keys Description
      Alt + Left Arrow Back a page
      Backspace Back a page
      Alt + Right Arrow Forward a page.
      F5 Refresh current page, frame, or tab.
      F11 Display the current website in full screen mode
      Esc Stop page or download from loading.
      Ctrl + (- or +) Increase or decrease the font size, pressing ‘-‘ will decrease and ‘+’ will increase.
      Ctrl + Enter Quickly complete an address. For example, type computerhope in the address bar and press CTRL + ENTER to get http://www.computerhope.com.
      Ctrl + D Add a Favorite for the page currently opened.
      Ctrl + I Display available bookmarks.
      Ctrl + N Open New browser window.
      Ctrl + P Print current page / frame.
      Ctrl + T Opens a new tab.
      Ctrl + F4 Closes the currently selected tab.
      Ctrl + Tab Moves through each of the open tabs.
      Spacebar Moves down a page at a time.
      Shift + Spacebar Moves up a page at a time.
      Alt + Down arrow Display all previous text entered in a text box and/or available options on drop down menu.

      Microsoft Excel

      Shortcut Keys Description
      F2 Edit the selected cell.
      F5 Goto a specific cell. For example, C6.
      F7 Spell check selected text and/or document.
      F11 Create chart.
      Ctrl + Shift + ; Enter the current time.
      Ctrl + ; Enter the current date.
      Alt + Shift + F1 Insert New Worksheet.
      Shift + F3 Open the Excel formula window.
      Shift + F5 Bring up search box.
      Ctrl + A Select all contents of the worksheet.
      Ctrl + B Bold highlighted selection.
      Ctrl + I Italic highlighted selection.
      Ctrl + K Insert link.
      Ctrl + U Underline highlighted selection.
      Ctrl + 5 Strikethrough highlighted selection.
      Ctrl + P Bring up the print dialog box to begin printing.
      Ctrl + Z Undo last action.
      Ctrl + F6 Switch between open workbooks / windows.
      Ctrl + Page up Move between Excel work sheets in the same Excel document.
      Ctrl + Page down Move between Excel work sheets in the same Excel document.
      Ctrl + Tab Move between Two or more open Excel files.
      Alt + = Create a formula to sum all of the above cells
      Ctrl + ‘ Insert the value of the above cell into cell currently selected.
      Ctrl + Shift + ! Format number in comma format.
      Ctrl + Shift + $ Format number in currency format.
      Ctrl + Shift + # Format number in date format.
      Ctrl + Shift + % Format number in percentage format.
      Ctrl + Shift + ^ Format number in scientific format.
      Ctrl + Shift + @ Format number in time format.
      Ctrl + Arrow key Move to next section of text.
      Ctrl + Space Select entire column.
      Shift + Space Select entire row.

      Microsoft Word

      Shortcut Keys Description
      Ctrl + A Select all contents of the page.
      Ctrl + B Bold highlighted selection.
      Ctrl + C Copy selected text.
      Ctrl + X Cut selected text.
      Ctrl + P Open the print window.
      Ctrl + F Open find box.
      Ctrl + I Italic highlighted selection.
      Ctrl + K Insert link.
      Ctrl + U Underline highlighted selection.
      Ctrl + V Paste.
      Ctrl + Y Redo the last action performed.
      Ctrl + Z Undo last action.

      Functional programming in C# 3.0 – (1)

      I had a lot of fun to read Tomas Petricek’s article, “Concepts behind the C# 3.0 language“. Instead of listing all of the new syntax changes as most of other tutorials, Tomas shed some lights on the details about influence of the development of C# 3.0 from other languages, especially how Functional programming plays in C# 3.0. Actually, Lambda Expression and LINQ are really part of that.

      So what is functional programming? Quote from Wikipedia:

      Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast with the imperative programming style that emphasizes changes in state.[1]

      So what it really tells us? I think the main point is “function is data” which means function can be use to be passed as an parameter or returned argument or modified as any other data in our application. Do we gain benefit for that? Sure, we do.

      1. Functional programming can help model data operation. Here is the sample:

      public static long Watch(Action act, T arg) 
      { 
              Stopwatch watch = new Stopwatch(); 
              watch.Start(); act(arg); 
              watch.Stop(); 
              return watch.ElapsedMilliseconds; 
      }
       

      This Watch function is totally decoupled with the Action, which could be “reading the data from database”, or “processing the data from a file”. It leads a clean design that Watch function and the Action function can be tested separately.

      2. Parallel programming. If you have been worked on Multi-thread programming. You know how hard it is. The limitation of imperative language like C# focuses on the state change. But functional programming is passing function as argument as we mentioned above. The state actually is only closed in the function boundary. So there is no other thread can access that data. Microsoft has released PLINQ, a parallel programming add-on to current .NET framework 3.5. It will be a huge benefit for any .NET who will take advantage of multi-processor computers.

      So far, so good, right? from next one, I will illustrate the functional programming features in C# 3.0. Definitely will show more code. Please stay tune.

      Create DTO with NHibernate

      My previous blog is about using DTO in UI/Presenter layers. Just studying Nhibernate source code now, I find a very interesting approach to populate DTO object directly from Nhibernate with Criteria and Projections.
      Here is the code:

      IList resultWithAliasedBean = s.CreateCriteria(typeof(Enrolment)) .CreateAlias("Student", "st") .CreateAlias("Course", "co") .SetProjection(Projections.ProjectionList() .Add(Projections.Property("st.Name"), "studentName") .Add(Projections.Property("co.Description"), "courseDescription")) .AddOrder(Order.Desc("studentName")) .SetResultTransformer(NHibernate.Transform.Transformers .AliasToBean(typeof(StudentDTO))) .List();

       
      [Serializable]
      public class Enrolment
      {
         private Student student;
         public virtual Student Student
         {
              get { return student; }
              set { student = value; }
         }
        
         private Course course;
      
         public virtual Course Course
         {
             get { return course; }
             set { course = value; }
         }
         
         private long studentNumber;
      
         public virtual long StudentNumber
         {
           get { return studentNumber; }
           set { studentNumber = value; }
          }
        
          private string courseCode = string.Empty;
      
          public virtual string CourseCode
          {
            get { return courseCode; }
            set { courseCode = value; }
          }
      
        private short year;
      
        public virtual short Year
        {
         get { return year; }
         set { year = value; }
        }
      
        private short semester;
        public virtual short Semester
        {
         get { return semester; }
         set { semester = value; }
        }
      
       public override bool Equals(object obj)
       {
         Enrolment that = obj as Enrolment;
         if (that == null)
                return false;
         return studentNumber == that.StudentNumber && courseCode.Equals(that.CourseCode);
      }
       public override int GetHashCode()
       {
           return courseCode.GetHashCode();
       }
      
      }
      public class StudentDTO
      {
          private string studentName;
          private string courseDescription;
          public StudentDTO() { }
          public string Name
          {
              get { return studentName; }
           }
          public string Description
          {
              get { return courseDescription; }
          }
      }

      DTO (Data Transfer Object) in Domain Driven Design

      DTO (Data Transfer Object)is a very common used pattern in enterprise applications. Martin Fowler also documented it in his class book “Pattern Enterprise Application Architecture“. In the meanwhile, DTO is also a very famous “Anti-Pattern”. The pure OO purist think DTO is a devil. It is so easy to build. The object become nothing but a simple data container, which represent the data in the tables instead of the real domain objects. The business logic embedded in the application is going to shift from middle domain logic tier to tables, most of time spreaded among the stored procedures, which is leading to a unmaintainable solution.

      So should we give up DTO totally in our application design? Absolutely not, in the other end, I find DTO still useful especially in UI and UI process layers. For example, we have a complex searching page to find customer by first name, last name, SSN, address, phone, email, and so on. those information may stay in different domain objects. It is also very hard to design a method with a very long list of parameters, not just ugly but also unmaintainable. My approach is to design DTO object pass data between UI and UI process (Presenter or Controller). Then access the service objects, and domain objects from presenter/controller. So I can design domain entity with the interfere from the special requirement of UI. Again it is important to implement MVP pattern to protect the domain object, which is also one of the important principles I heard from DDD: don’t leak business logic out of the domain object layer.