Sep 13, 2013

Google Book Downloader


Google Book Search is a tool from Google that searches the full text of books that Google scans, converts to text using optical character recognition, and stores in its digital database. Many popular books are available with Google Book. The books available can be read online only and cannot be downloaded for later use. You can download certain books as png that allows public-domain works and are free from copyright protection. Only few books are available with full preview based on agreements with publishers. 

Google Books Downloader Lite is a free, open-source utility that lets out download any book that's available in "full view" from Google Books. Of course, most of these books also feature download links right on the web page, but Google Books Downloader lets you queue up multiple jobs and convert all of the downloaded books to PNG files. 

You can download this tool from here. Need to install .Net Framework 3.5 SP1 before installing this tool. 

Sep 10, 2013

Interactive Extensions


LINQ provides lot of ways to query the objects or XML. But we might need some more features\extensions which will be handy in real development. Let us explore a library which provides more extensions\features to the existing framework.


Introduction

The Reactive Extensions (Rx) is a library for composing asynchronous and event-based programs using observable sequences (provides notification information) and LINQ-style query operators.

The Interactive Extensions (Ix) is a .NET library which extends LINQ to Objects to provide many of the operators available in Rx but targeted for IEnumerable. In simple terms, it provides a lot of new features to LINQ to Object which is not available in Framework. Ix is sub-set of Rx.

This will install System.Interactive.dll file. EnumerableEx class provides a set of additional static methods that allow querying enumerable sequences.


Execute below command in Nuget Package Manager Console to install:
install-package Ix-Main


Let us discuss few features:

Do: This lazily invokes an action for each value in the sequence. That means it invokes\executes an action for each value in enumeration.


Sample Code:
static void Main(string[] args)
{
      DisplayNumbersDo();
}
static void DisplayNumbersDo()
{

      int addnumber = 10;
      //Let us create a set of numbers 
      var numbers = new int[] { 6, 1, 4, 2, 6 };

      var result = numbers.Do(n => Console.WriteLine(n + addnumber));
      Console.WriteLine("Before Enumeration");
      foreach (var item in result)
      {
          //The action will be invoked when we actually enumerate 
      }
      Console.WriteLine("After Enumeration");
      Console.ReadLine();
}


ForEach: Enumerates the sequence and invokes the given action for each value in the sequence.

Sample Code:
static void Main(string[] args) { List names = new List { "ram", "nagesh" }; names.ForEach(s => Console.WriteLine(s)); ; Console.ReadLine(); }


Scan: Generates a sequence of accumulated values by scanning the source sequence and applying an accumulator function. 

Sample Code:
static void Main(string[] args) { List names = new List { "ram ", "nagesh" }; names.Scan((x,y) => x + y).ForEach(s => Console.WriteLine(s)); Console.ReadLine(); }



Below are few more methods which might be useful:

Repeat: Repeats and concatenates the source sequence infinitely.

SkipLast: Bypasses a specified number of contiguous elements from the end of the sequence and returns the remaining elements.

TakeLast: Returns a specified number of contiguous elements from the end of the sequence.


You can install Interactive Extensions in Visual Studio 2010 also.





Sep 5, 2013

Code Contracts

Code Contracts

Code Contracts are static library methods used from any .NET program to specify the code’s behavior. Runtime checking and static checking tools are both provided for taking advantage of contracts. Code Contracts is a subset of a larger Microsoft Research Project called Spec# (pronounced "Spec Sharp"). 

In simple, its add-on that you can install in Visual Studio. This library offers number of attributes and helper classes to perform various validations/conditions at various levels.

You can download add-on from here

After installing make sure you have "Code Contracts" tab in project properties.


Make changes to project properties as below:



Add Using statement as below:
using System.Diagnostics.Contracts;


Preconditions:
Preconditions are those which needs to be executed at the beginning of the method. They generally used to validate parameter values like object is not null, Argument null exception, int > 0 etc.

Normal precondition looks like this:

public static int GetEmployeeCount(int intDeptId)   {
         if (intDeptId == 0)
           throw new Exception("Invalid Department Id");
            
        //logic here
        return 10;
}

We can write the same code in a better way as below:


public static int GetEmployeeCount(int intDeptId)   {        Contract.Requires(intDeptId > 0);
       //logic here
       return 10; }




Static Checking:
Instead of raising exception after callign method, it would be much easier if we can raise exception at the time of calling method. This is static checking. For this, you need to enable "Show squigglies" option in Project properties.

Code:
public static int GetEmployeeCount(int intDeptId)
  {
      Contract.Requires(intDeptId != 0,
             "Zero is not allowed for Department Id.");
      //logic here
      return intDeptId;
}

While performing static checking, error message will be shown as below:



Post-Condition:
These conditions are nothing but validations after executing a method. These are very useful in case of validation objects, dataset from database, etc. You have option to save old values in OldValue before calling method.
In below example, we are ensuring the output of the GetEmployeeCount is greated than 5.

Code:

Contract.Ensures(Dept.GetEmployeeCount(5) > 0);


Interface Contracts:
We can write contracts for interface methods required creating contract class to hold them.

We can use below attributes:
ContractClass: Need to defined for Interface. Ex: [ContractClass(typeof(IDeptContract))]

ContractClassFor: Need to defined for class implements interface. Ex: [ContractClassFor(typeof(IDept))]

Conclusion:
Code Contracts will be very useful, if we can use it in a proper manner and follow some guidelines. This can be used in Business Layer for validating objects before passing it to Data Layer. This will reduce lines of code and errors/mistakes while writing validation logic.

For complete documentation, please refer this link



Sep 3, 2013

Performance Tuning Techniques - ASP.Net


Performance Tuning Techniques in ASP.Net

Below presentation provides few performance tuning techniques related to ASP.Net.