23 January 2009

Solid State Drives : Slowly making its way


First of all I would like to go bake to 80's and show you a picture and guess what its a hard drive!!! And size? massive 5 MB !!! I am sure you will laugh at it now. We have TBs of HDD in our notebooks nowadays. But its only problem is it has a mechanical head that runs at 4200 to 7200 rmp. It is like a jet engine. So what? It is vulnerable to fail if you drops it and it consumes more power. It is a mechanical HDD at the end of the day. Though the technology is improved enormously and there are many protective measures to prevent it from failing we certainly need something that can replace it in the future for better performance and low power consumption. As long as you have desktop you don't bother much about power consumption but as soon as you move to notebooks you started to think how can I make my notebook run for longer without charging its battery. There comes the SSDs in picture. In simple terminology it is just like a flash drive. Why it is called SSD? Simple because it has no moving parts like mechanical HDD and that makes it more reliable and less power hungry. SSD are basically a NAND based flash drive.This means no moving parts, and thus no spin-up speed, which results in shorter boot times. It also means faster read speeds from idle, and faster write speeds for larger files.SSD can be much smaller than mechanical HDD.MicroSD cards are a good demonstration of this, as is the seemingly endless increase in the capacity of USB keys.The nature of flash memory also means that it can survive through a larger temperate range, with some flash drives operating at up to 70 degrees Celsius. Though there are many advantages it is still emerging while mechanical HDD is mature technology but it will eventually replace them. As I write this Intel has released 320GB SSD this month and size will grow and price will drop. So stay tuned and keep your eyes open for SSD.

22 January 2009

.NET 3.5 's LINQ (Language Integrated Query) feature : A case study

1. Introduction

LINQ is the abbreviation of Language integrated query, the new component added in the Microsoft’s .Net version 3.5. LINQ is available in C#3.0 and Visual Basic 9.0. It provides the unified way to query not only relational databases but also XML documents and in memory objects. It is an initiative towards providing facility to the programmers for getting away from writing database specific or XML document specific query to retrieve particular data.

In this report we will look at what is the LINQ in general, how it is different from using traditional ADO.Net concepts like DataSet, DataAdapter etc. and evaluate it for the use in the organization, and its advantages and disadvantages compared to the traditional approach of using ADO.Net and brief look at what are the pros and cons of traditional ADO.Net approach. Final section of the report will conclude the findings.

2. Overview of LINQ


Most of the enterprise level application requires some short of database access and .Net applications that access database are generally use ADO.Net to do so. If the programmer needs to access the XML document to perform some query on it the way to do that is different than to access the data from the relational database. LINQ is developed to unify this process of operating on different kind of data sources. So this will reduce the effort required by the programmer to deal with different kind of database. LINQ also support the way to query collection of objects in memory, similar to the way programmers query the database table. This new feature of LINQ to Object will provides easier way to the programmers to deal with some complex operations on collection of objects. For example, find the element whose length is 5 characters. In traditional approach the programmer may loop through the collection of objects and check that the element’s length is 5 or not. LINQ provides facility to query the collection of elements by using syntax like:

string[] names = {"John", "Peter", "Joe", "Patrick", "Donald"};
IEnumerable namesWithFiveCharacters =from name in names where name.Lenght==5 select name;


LINQ to Object can be applied to any type as long as it inherits from the IEnumerable. Previous example of using LINQ to query the array of strings is called LINQ to Object. LINQ can be classified into other categories link DLINQ (LINQ to SQL) that deals with querying to the relational database and XLINQ (LINQ to XML) that deals with querying XML document. LINQ supports different kind of query operators like aggregating the data, performing grouping or joining tables etc.

The previous example is written using simple query mechanism. There is another flavour available to write the same query using methods and lambda expressions. This way of writing query is called method-based query.

Following example shows the method-based query:

IEnumerable query = names
.Where(s => s.Length == 5)
.OrderBy(s => s)
.Select(s => s.ToUpper());


Here every clause is used as a method and the arguments that are passed to the methods are called lambda expressions.

2.1 LINQ to SQL (DLINQ):

LINQ to SQL let the programmer write the query in their preferred language and leave to convert it into the database specific query to the .Net compiler. Another advantage that LINQ provides over traditional way of using ADO.Net is that if there is an error in the query it will be reported during the compilation of the code and not during the execution. This will definitely save time in writing and debugging code. LINQ can be used to perform any kind of query to select the data or to manipulate the data in the database. Following example shows how the LINQ to SQL simplifies the process of writing query.

HookedOnLINQ db = new HookedOnLINQ("Data Source=(local);Initial Catalog=HookedOnLINQ");

var q = from c in db.Contact
where c.DateOfBirth.AddYears(35) > DateTime.Now
orderby c.DateOfBirth descending
select c;

Once the value is retrieved in variable q you can iterate thorough it to get the results that you wants.
2.2 LINQ to XML (XLINQ):

Many times XML is used to store data for the portability reasons. This method become increasingly popular and that leads to many techniques to deal with XML documents like XQuery, XPath, XSLT, XSL etc. XLINQ is yet another addition to those list but it is more flexible and powerful then those other techniques. XLINQ not only helps to query the XML documents but also supports generation of the XML document. This will eliminates the need for using DOM or XML APIs to work with it. Similar to DLINQ, XLINQ provides easier way to deal with generation and manipulation of XML documents that are used to store data. In the recent version of .Net, XQuery is used to query the XML document while SQL is used to retrieve data from the databases. LINQ unifies this process of retrieving data from different data sources like XML document or relational databases. XLINQ is more flexible to work with then XML DOM because it does not require whole document in the memory to work with it. XLINQ provides all functionalities of DOM, XQuery, XPath, XSLT, and MSXML.

Following code shows how easy to generate XML document using XLINQ.

XDocument bookStoreXml =new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XComment("Bookstore XML Example"),
new XElement("bookstore",
new XElement("genre",
new XAttribute("name",
"Fiction"),));

Following code shows how to query the XML file to retrieve data from it using XLINQ:

XElement allData = XElement.Load("Authors.xml");
if (allData != null)
{
IEnumerable authors = allData.Descendants("Author");
foreach(XElement author in authors)
Console.WriteLine((string)author);
}

These codes indicate the new method of using LINQ to generate and query XML document. We no longer needs any old XML APIs .

3. Comparing Usage of LINQ to Current ADO.Net Approach

Vary first observation from above examples is that the way of writing query is different for traditional ADO.Net and LINQ. For example, In ADO.Net the query to retrieve data from the database starts with select while in LINQ it starts with from clause. So the programmers who are willing to use LINQ will needs to keep this basic thing in mind and it will initially takes a while to get away from the old habit of writing query in ADO.Net manner. Another important change in the query compared to the traditional approach is that you do not needs to specify how the query should execute and in which order. The LINQ takes care of forming the proper query by supplied values. So the programmer just needs to specify what is expected from the query and provide appropriate parameters to the clauses or to the methods in method-based query. LINQ provides many extension methods to deal with complex operations like joining the tables or grouping the data by particular condition. Writing such queries in traditional ADO.Net approach is fairly complex and takes longer to understand and debug as well. LINQ makes writing such queries simple by use of extended methods. This will make code more readable as well. All the extension methods that are used by LINQ are defined in System. LINQ Enumerable class. Example of the extensions methods are Any, All, Max, Min, Join, GroupBy, GroupJoin, OrderBy, ThenBy, Select, SelectMany ,Where etc. Most of the time the programmer needs to type cast the different data type from the database to the programming language format and vice versa. This job is tedious as well, for example, type casting VARCHAR into the integer data type to use that data in the code to get the desired output. This short of conversion requires handling the exceptions programmatically and that leads to more code. Another issue with using this traditional approach of ADO.Net is that the database code get scattered in many places. This is not a problem for the small projects but in the large project this will leads to more code management issues later. All those issues can be resolved by using LINQ that provides one layer of abstraction then directly using ADO.Net call level interfaces (CLI). ADO.Net provides data in DataSet or DataReader and the programmer has to iterate through it to get the data back into the objects while LINQ directly offers the data as objects.

4. Advantages of LINQ

The aim of LINQ is to solve the problem that we are currently facing to use different techniques for manipulating and selecting data from databases versus XML or versus object collections. The purpose of LINQ is to make our life easier by giving us a consistent and efficient syntax from our development environment. Below are some advantages of using LINQ:


• The main advantage of using LINQ is that it works with any data source. Users can put very less effort to change between data sources if necessary. LINQ enable languages have a compile time checking of query, good intelligence, and debugging support.

• One of the C#.NET features that LINQ can support is the “Anonymous Types”. This allows programmer to create and use the type structures as is without having to formally declare their object model. This is really useful when working with custom data type using LINQ queries. For example, if you working against a database or strongly-typed collection that has many properties, but you only need a few of them, the rest of the types are useless to you. Without using LINQ, you need to create and work against the full type. However, LINQ is useful to return those properties that you need.

• LINQ can be used with objects, collections, XML, database etc. Searching a collection or a node in XML is fairly easy to implement using XLINQ. LINQ uses queries similar to SQL, which is rather easy compared to querying an XML through old XML APIs.

• LINQ queries can allow users to create, read, and update a database and it can support the relationships that are defined in the database. For example, It allows the use of same syntax as general SQL syntax that is used to join tables based on the keys.

• Another advantage is availability of LINQ to DataSet. LINQ can make it fast and easy to query over data cached into a DataSet objects. This is because LINQ is simplifies the querying mechanism by allowing users to writes their own queries, instead using the separate query languages.


5. Disadvantages of LINQ

Although LINQ come with such advantage to the developers, just like other techniques there are some disadvantages for using LINQ as well. some issues or disadvantages of using LINQ are as follow:

• Stored procedures can be used with LINQ but it is not recommended. LINQ generates T-SQL query dynamically to operate on tables or views. It leads to the always debated question of whether the use of stored procedure is good or not? Currently there is no wizard available that can generate the stored procedure supported by LINQ.

• LINQ adds a layer that encapsulates the use of call level interfaces directly on the database or XML document. During the execution of the code the query is generated dynamically by the .Net. This will leads to slight performance degradation as the time is required to convert method calls in LINQ to appropriate query.

• LINQ to SQL (DLINQ) select statement is generally “select * from…” kind that will leads to too much or too little data in a single query. Too little data in a query is bad because then there is a need for many trips to the database to retrieve data. Furthermore, There are some concerned when using LINQ to SQL, such as security, entities with no primary key, entities with composite primary key, many-to-many relationships and globalization, etc.

• Evan though you find a performance issue with the LINQ it is not possible to tune it as it requires too much effort to change the underlying APIs that work for LINQ.

• LINQ can generate entity classes and even can extend them with partial classes and inheritance. However, the control over the classes is still limited. Developers don’t have much control as other language, such as NHibernate.

• LINQ generates query dynamically and that leads to another issue of finding problem if something goes wrong. In the traditional approach of ADO.Net the programmer can look at the query that is written in the programme and can able to find some fault. In LINQ this process of finding problems in the query is hard if something goes wrong. This situation becomes worst in the case of large application and if the application stopes working in a way it suppose to after it is deployed. There may not be any syntax error but it can be a logical error or may be the side effect of some change in the database schema itself.

• In large organization generally DBA provides the access to the database through stored procedures. While in LINQ the query is generated dynamically and that will leads to the issue of giving access of database to the developers or just use stored procedure facility of LINQ. Particularly when dealing with some critical databases there is more concern on what kind of access is to be provided to the developers. The queries generated by developers also raise the concern of tuning it as the DBA cannot look at each and every piece of code to fine tune it. It will be easier to DBA to look at stored procedures and make the changes in it.

6. Advantages of ADO.Net

In this section we will look briefly at the advantages of using current approach of using ADO.Net for data access. It was the main data access component in the .Net framework prior to introduction of LINQ. According to the MSDN Library (2005) some of the benefits offered by ADO.Net are:

• ADO.Net provides interoperability between databases because it can operate on XML documents. If the destination database is capable of reading XML document then the data can be passed between different databases. ADO.Net provides facility to work with any database and XML documents.
• ADO.Net provides easier maintainability of application by passing data between tiers as DataSet rather than any other object type. So if in future there is a need to add a tier to the deployed application it can be done easily.
• Programming ADO.Net to access and manipulate data is easy compared to prior versions of ADO like OLEDB.
• ADO.Net DataSet provides better performance compared to older version of ADO RecordSet as well as LINQ. LINQ takes time to generate query dynamically while in ADO.Net query is passed as a string object. In ADO use of COM marshalling to transfer data as RecordSet between tiers takes time to marshal and unmarshal it to convert into understandable data type of COM.
• ADO.Net is capable of working with data in a disconnected way.

7. Disadvantages of ADO.Net

Some of the disadvantages of using ADO.Net for accessing database or XML document are described briefly as below:
• There is no interoperability between COM and ADO.Net. ADO.Net only runs under CLR referred as managed only access. So if there is a need to use facilities like DataSet or XML internal data storage the programmer must need CLR.
• If the programmer needs to access data that requires a driver that cannot be used through either an OLEDB provider or the SQL Server Data Provider then there is no other alternative. Although, there is an OLEDB provider for ODBC is available for download from Microsoft but if there is no bridge available then there is no other way to use that database.
• ADO.Net supports both database and XML documents but it does not unify the process of dealing with them. LINQ is better in that aspect. ADO.Net uses different APIs and methods to deal with database and XML documents.

8. Conclusion

LINQ is certainly a technology to look after to deal with different kind of data sources. LINQ unifies the process of dealing with different kind of databases and XML documents to retrieve and manipulate the data. LINQ query is more readable and it is easier to write complex queries compared to the traditional approach of using ADO.Net. There is an initial learning curve for the programmers who want to adopt the LINQ as their preferred way of interacting with database. As every technique has its pros and cons and LINQ is no exception to it. There is already concern about performance issue and maintenance of the code. Still, LINQ is a useful technique and it will change the way programmers write their code to deal with databases and XML documents in the future.

21 January 2009

Intel Core i7 Processor


Technology is changing rapidly and particularly in the arena of processors there are changes beyond imagination. I still remember the days when we were programming on Intel's 8085 processor boards !! It really sounds ages old now with emergence of core architecture and now moving towards tile architecture. I should mention that a small firm called Tilera came up with Tile 64 architecture. As simple as it sounds 64 processors in a single chip. Sorry for going bit off track here we are talking about Intel's core i7 the much anticipated and talked about processor.

It has 4 cores in a single chip sharing a queue to work alongside one another. Core i7's design is based on Core 2 processors but it is changed a lot the way interfaces are provided for memory and I/O. There is a three layers of catch shared between cores.One mechanism Intel uses to make its caches more effective is prefetching, in which the hardware examines memory access patterns and attempts to fill the caches speculatively with data that's likely to be requested soon.There is an integrated memory controller in the processor itself to access main memory very fast.

With the memory controller onboard and the front-side bus gone, the Core i7 communicates with the rest of the system via the QuickPath interconnect, or QPI. QuickPath is Intel's answer to HyperTransport, a high-speed, narrow, packet-based, point-to-point interconnect between the processor and the I/O chip.

You can read more technical specs about it on:
Wikipedia: Core i7

20 January 2009

Application Architecture Guide 2.0


A really good book for Developers and architects to read about Design Patterns and practices. It is particularly targeted at .NET framework and covers all kind of applications from web development to RIA to N-tier enterprise grade architecture. Above all its free to download from Here. It gives a fairly good idea about design principles.