Saturday, June 13, 2009

SQL Server 2008 – First Look (Spatial Data)

Technorati Tags: ,,

As I start exploring the latest version of SQL Server, one of the major head turners in SQL Server 2008 is the ability to store Spatial Data.

Spatial Data enables us to store and retrieve location based information in the SQL database. An example of that would be to show a user pizza joints in a 5 KM radius from his address. If you have downloaded the SQL Server 2008 training kit, you will find samples and demo of how to use spatial Data. There is also a presentation on Spatial data in the kit.

Spatial Data in SQL Server is based on the standards defined by the Open geospatial Consortium and supports two key data types; Geography and Geometry. Both these data types are also CLR data types enabling complete compatibility for use in .NET code.

Though Spatial Data sounds very useful in terms of storing location based preferences or queries, it takes time getting used to something like this. I am still trying to get my heads around how to store and retrieve data on Spatial Data Types. You can find more information on Spatial Data types in the below links.

SQL Server Spatial Data Technology Center

Whitepaper: Delivering Location Intelligence with Spatial Data

Whitepaper: What's New for XML in SQL Server 2008

Whitepaper: Managing Unstructured Data with SQL Server 2008

MSDN Webcast: Building Spatial Applications with SQL Server 2008, Event ID: 1032353123

Sunday, May 24, 2009

Axum – Next Gen Parallel Programming?

Over the past one year, Microsoft has been investing quite a lot on tools to help Parallel Programming as a part of its Parallel Computing Initiative. One of the first initiatives in this direction was the Parallel Fx library. I had the opportunity to do a session on the same and the first impressions I had on it were not bad. It relieved the developer pains of resource synchronisation and took a lot of things under the hood.

But now, Microsoft has come up with Axum. Axum is currently an incubation project but if it becomes a full release, might alter the way .NET developers do Parallel Programming. I have spent a couple of hours reading up on Axum and there are a lot of new terminologies and concepts one has to grasp, especially if somebody is not familiar with MSMQ. The first impression I have is that it is a lightweight MSMQ platform. As I play around with Axum over the next few days, I will try and post samples and articles on the same. Keep hooked onto this space for more.

Thursday, April 16, 2009

Null Object Pattern using .NET

I was doing code reviews in one of my projects and I found a pattern of code that kept repeating itself. The code went something like this

if (Quote != null)

{

//do something accessing properties of Quote

}

The above code does not seem too bad does it? But when the object linkages increases to a couple of levels; i.e. Quote has an object Vehicle which in turn has an object engine and you want to find out the make and model of the engine imagine the number of If blocks that you will end up writing.

This was precisely the developer’s problem. Something told me that there has to be a better way of doing this and a quick google lead me to the Null Object Pattern. I am not going to go into the details of what it is because I am sure google can give you umpteen number of links. What I am going to show you though is one of my approaches to implement this Null Object Pattern in your code using .NET.

I am going to use a console application and the precise example I have quoted above. Let us consider the following scenario

There is a Quote class. The getVehicle of the quote class populates a vehicle. The vehicle object in turn has an engine object. The objective is to print out the cc and the horse power of the engine.

The definition of the classes are as shown below

EngineClass VehicleClass QuoteClass

And as a developer the following is the code I write to display the cc of the engine.

 Program

When you execute the above code, you will get an exception in the Console.WriteLine statement. If I were to handle this exception, the best way is to add an if block to first check if testvehicle and engine is null and then write cc only if they are not null. the other option is to catch a NullReference exception but catching exceptions is a costly affair. So what do we do so that the code executes even if the object being accessed is null?

The first step and this is optional is that I declare a INullable interface. This is just to ensure that we are consistent with .NET’s own nullable types. This INullable interface just has one method – isNull. The interface declaration is as shown below 

 INullable 

All objects that need to expose the null representation inherit this INullable interface. To ensure my developers do not have to write a lot of code I decided to create a generic NullObject class with the code shown below

NullObject

Most of the code is self-explanatory.  In my returnNull method, I just instantiate a new type and return that type. I would just like to grab your attention to the fact that I have inherited the INullable interface and returned true. Once this class is written, all that the developer needs to do in his code is add a couple of lines. My new engine class is as below

NullEngineClass As you can see the difference now is just that I have added a static method call nullObject that will call the generic NullObject’s returnnull method. By now you have probably guessed what I am going to do in the vehicle class, so I am not going to withhold that any longer. 

NullVehicleClass Thats it!!! You run your application now and voila!!! The same line does not throw an error. It just says 0 which is perfectly acceptable. I hope you put this to the best use because personally I think this can relieve us of a lot of lines of code. Do let me know your thoughts on this. Happy Coding!!!!