Wednesday, April 25, 2012

How to execute other methods while selecting in LINQ

Well, because I'm just still trying to master myself in LINQ, and i'm still working on pretty basic stuffs, I was looking at ways where I can do a select an array of BusinessDocument type without resorting to for-loop and still log the each of the document that I'm iterating to. Well, the below code is fantastic :

            // Modified to use LINQ for readability.
            IEnumerable<string> documents = oioublDocumentTypes.Select(documentName =>
                                                                               LogHandler.LogInfo("Requesting profile info for doc type: " + documentName.ToString());
                                                                               return documentName.ToString();

The above code is equaivalent to looping each of the documents from the oioublDocumentType array and then returning each of them as an IEnumerable of string. While doing that, log each to the file while iterating. This is just a single line of code which saves me from doing for-loop... Look's pretty cool...

So this means that you can execute a lot of method within the select before returning what you want to return as a result... you can even do calculation inside and return the correct calculation... pretty impressive..

Monday, April 23, 2012

LINQ Overview

I'm actually trying to understand more about LINQ and I find that LINQ is clever solution. What LINQ is trying to address is the conceptual and technical difficulties when using different data sources with .NET Programming language.Microsoft’s intention was to provide a solution for the problem of object-relational mapping, as well as to simplify the interaction between objects and data sources.

LINQ unifies data access, whatever the source of data, and allows mixing data from different kind of sources. Before LINQ, we had to juggle different languages like SQL, XML, or XPath along with various technologies and APIs like ADO.NET or System.Xml in every application written using general-purpose languages such as C# or VB.NET. With LINQ, we can use a single language to query SQL, XML and other data sources without re-learning different techniques for drawing data from it's sources.

From my notes,

LINQ is :
  1. All about searching efficiently and consistently
  2. Can be used with different data source. Does not need to know the underlying data source being queried from
  3. No longer matters where the data is coming from
4 basic patterns for LINQ :
  1. Linq to Object
  2. Linq to DataSet
  3. Linq to SQL
  4. Linq to XML

Different Linq providers are shown here :

Note though that because LINQ exists, everything must go with it. LINQ has performance overhead, so be careful. Basic rule is that LINQ is used mostly on a little more complex lists.

LINQ Namespaces :

System.Linq namespace contains all basic classes and interfaces that you use to work with LINQ.

System.Linq.Expressions namespace contains the classes, interfaces, and enumerations used to create expressions. Expressions is essentially a tree of nodes that define how a query works. For example, you can create a binary expression that defines how to subtract one number from another. Essential expression types are :

  • BinaryExpression
  • ConditionalExpression
  • ConstantExpression
  • InvocationExpression
  • LambdaExpression
  • ListInitExpression
  • MemberExpression
  • MemberInitExpression
  • MethodCallExpression
  • NewArrayExpression
  • NewExpression
  • ParameterExpression
  • TypeBinaryExpression
  • UnaryExpression
System.Data.Linq namespace contains the classes, structures, interfaces, and enumerations used for SQL database interactions. 

System.Data.Linq.Mapping namespace contains the classes and enumerations to map data between an imperative language such as C# or Visual Basic .NET and a declarative language such as SQL. It also comes into play when working with technologies such as XML. In short, you'll use this class when working with any external data source that has a different representation from the standard object-oriented view of data found in the .NET Framework. More about this in : 

System.Data.SqlClient namespace contains the classes used to create a basic connection with SQL Server. Although you might use this namespace in a number of scenarios, you'll generally use it exclusively with SQL Server.

System.Xml.Linq namespace contains classes and enumerations used to interact with XML data of all type. When you think about the number of ways in which modern computer systems use XML data, this namespace covers a significant amount of ground.

Sunday, April 22, 2012

Test Driven Development

I just had an interview with an applicant from our company and one of the questions that we had to him is if he has done Test Driven Development. For most part, I would say, most of the applicants that I have interviewed does not have a clear grasp of test driven development. They thought or most of them thought that just because you have a unit test, means it is a Test Driven Development. One of them was even describing Integration Testing rather than Unit Testing. So I thought of publishing a short and concise understanding of what really is Test Driven Development..

What is test driven development ?

Test driven development is a software development process of which for most part, the test has more importance or if not has the same importance with the code. That is, test are first created even before the actual coding/logical coding is done. It is difficult to understand this concept but in essence, it is a development style of which codes undergo exhaustive testing.

These are the concepts of TDD according to my notes based on a TDD book I've read way back.:

  1. You maintain an exhaustive suite of Programmer Tests. - You have a programmer's test that tests your classes exhibit the proper behaviour.
  2. No code goes into production unless it has associated tests, - All codes must have an associated test before it goes to the next environment for integration testing. This means that if there's a change of code, there must be a test for it or you don't propagate the change. Similarly, this is applied to new functions.
  3. You write the tests first - You write the test first. Write a test that will fail initially, and then test. Pass at the minimum of 2nd time then refactor or keep on testing until it passes then refactor.
  4. The tests determine what code you need to write. - This helps limit your code to the actual functionality that needs to be implemented and nothing more.
  5. Let the computer tell you your errors.

The following are the simple rules of applying TDD as per my notes based on a TDD book I've read way back :

  1. Think about what you want to do.
  2. Think about how to test it.
  3. Write a small test.
  4. Think about the desired API.
  5. Write just enough code to fail the test.
  6.  Run and watch the test fail. (The test-runner, if you're using something like JUnit, shows the "Red Bar"). Now you know that your test is going to be executed.
  7. Write just enough code to pass the test (and pass all your previous tests). Run and watch all of the tests pass. (The test-runner, if you're using JUnit, etc., shows the "Green Bar"). If it doesn't pass, you did something wrong, fix it now since it's got to be something you just wrote.
  8. If you have any duplicate logic, or inexpressive code, refactor to remove duplication and increase expressiveness -- this includes reducing coupling and increasing cohesion. Run the tests again, you should still have the Green Bar. If you get the Red Bar, then you made a mistake in your refactoring.
  9. Fix it now and re-run. Repeat the steps above until you can't find any more tests that drive writing new code.

Now the general concept and reason of TDD is actually to : Force programmers to think before they act.

Fundamentals of C#

C# is a modern programming languages that is a descendant of C++ but is grouped together with Java. Some people says that C# is actually a competing technology created by Microsoft to go against Java. True or False, I'm not here to defend which one is correct, but am here to share my notes on C#.

The family tree of C# is shown below :


So C# is actually on the same level as Java. You can think of it as brothers, sisters, or cousins. But both are descended from C++

C# and the .NET Framework

The C# LanguageC# is a new and modern programming language provided as part of the .NET Framework. It is an Object Oriented Programming language and has it's core many similarities with Java and C++. Some of the attributes by this language includes :

  • C# does not allow multiple inheritance but allows multiple implementation of interfaces
  • Provides Garbage Collection so that programmers does not need to worry of memory allocations and pointers.
  • It maintains unique operations of C++ like operator overloading, enumerations, pre-processor directives, delegates (or function pointers in C++)
  • It supports concepts of properties, reflections, attributes, marshalling, remoting, threads, streams, data access etc.
Next we will discuss .NET Framework.

The .NET Framework
.NET framework is a new framework which includes large libraries called Framework Class Library, allows and it supports several programming languages which allows language interoperability (each language can use code written in other languages). The .NET library is available to all the programming languages that .NET supports. Some of the languages it supports includes C#, F#, VB.NET, C++ among others.

The most important piece of this framework is the CLR or Common Language Runtime. It is a framework that sits on top of the OS and allows execution of the language developed for .NET framework. We can show it by the below diagram.


*From Programmers Heaven C# School Book

Common Language Runtime (CLR)CLR or Common Language Runtime is an environment at which we can run our .NET Applications that have been compiled by IL Compiler. If you are familiar with Java, consider CLR as JRE.

To further breakup CLR, a diagram is shown below : (1)


*From CodeProject

  • Common Type System- It is responsible for interpreting data types for .NET framework. E.g. how many bytes there is in a String object. CTS defines the basic data types to what an IL can understand. Each language should map it's data types to these standard data types. This is quite powerful and allows 2 languages to understand each other such as passing and receiving parameters to each other.
  • IL Compiler - IL Compiler compiles the IL Code to the Machine Language. IL actually means Intermediate Language. When you compile your code, the code gets compiled to an Intermediate Language, much like a Java is compiled to a Java Byte Code. However, afterwards, this IL will be fed to the CLR so that it gets compiled to a machine language code.
  • Execution Support - It is similar to the language runtime (e.g. VBRunxxx.dl in VB however in .NET there's no individual language runtimes anymore).
  • Security - Provides the security context on the application, and ensures the application has permission to certain functions.
  • Garbage Collector - It is similar to the garbage collector found in Java, which collects stale objects or unused/unreferenced objects, and releases the allocation to the heap memory.
  • Class Loader - The purpose is to load the classes needed by the application.

To understand, here's a sample :
  1. Programmer writes the source code and compiles it.
  2. Language compiler compiles it to IL. ( This is called Assembly )
  3. WHen the applciation is run the following happens :
  • It checks the assembly's security characteristics.
  • It allocates space in memory.
  • It sends the assembly to the IL Compiler (also called Just-in-time compiler) which compiles portions of it to native machine code. Only portions of the code is compiled as it is needed. If it's not needed during execution, it is not compiled.

The diagram below from the book Beginning ASP.NET in C# 2010 shows how the compilation works.


.Net Framework also provides a large and extensive library called Framework Class Library (or Base Class Library). It's sole purpose is to provide easy to use API's that are available to your application, such as :
  • General Base Classes - Classes that provides set of tools for programming tasks such as file manipulation, string manipulation, security, encryption, etc.
  • Collection Classes - Classes that implements lists, hashtables, etc.
  • Threading and Synchronization classes - Classes for building multi-threaded programs
  • XML Classes - Classes for XML manipulation
  • Data Classes - Classes used for Data Access and manipulation.
So there, I've discussed about .NET framework and C# as an overview. Next, we will start taking a look at C# in-depth.