Saturday, January 24, 2009

SDS Data Model

SQL Data Services is often referred to as “SQL Server in the cloud”. Despite this designation it actually operates quite a bit differently then a traditional relational database. In this post I will describe the SDS data model. There are three parts to the data model, Authorities, Containers and Entities.

Authorities are the highest level of organization in SDS. An authority corresponds to a specific SQL Server instance in one of Microsoft’s data centers. The authority name will be the first part of the DNS address used to access SDS.

The next lower level of the hierarchy is a container. Depending on how it’s used a container is akin to either an entire database, or a single table in a database. We will talk more about that when we get to Entities. You can have multiple containers in a single authority, and containers can contain zero or more entities, but cannot contain other containers. At present queries are restricted to a single container, you cannot query across multiple containers.

The lowest level in the hierarchy is the entity where your actual data is stored. Entities are akin to records in a traditional database, but unlike traditional databases SDS doesn’t use schemas. Each entity you create in a container can potentially have a different set of fields (called properties in SDS). If you choose to have every entity in a container have the same properties, then the container behaves like a table, but if you mix different entities in a container then it’s behaving more like a complete database. This is one of the areas where SDS diverges quite a bit from the operation of a traditional relational database.

There are two types of entities in SDS, non-blob which you will use most often, and blob entities used to store binary objects. We will just talk about non-blob entities here. Each entity contains a series of properties of which there are two types, metadata properties and flexible properties.

There are three predefined metadata properties. The first is ID which must contain a unique value for each entity in a container. This is a string value that can have up to 64 characters. The second property is a numeric value called Version. Version is automatically assigned by the server when the entity is created and a new version number is assigned each time the entity is updated. Version can be used to handle optimistic concurrency. The final metadata property is a Kind. Kind is an optional string value that can be used to identify the type of each entity. For example if you were doing an order entry system you could have kinds like “Invoice”, “Sales Order”, etc.

Finally an entity can have zero or more flexible properties. These properties contain the actual data that you want to store in the entity. Each property will have one of the following data types, string, binary, boolean, decimal or dateTime.

In my next post we will start working with the SSDS Explorer tool.

Monday, January 19, 2009

SQL Data Services Getting Started

In my last post I talked about Windows Azure. One of the components of Azure is SQL Data Services (SDS), formally known as SQL Server Data Services (SSDS), which is Microsoft’s “database in the cloud”. If you want to start learning about Azure, SDS is a good place to start since you can use it without having to setup a full Azure development environment. In this posting I will discuss how to get setup to work with SDS.

The first step is to sign up for Azure Service Platform invitation codes, you can do that on this page http://www.microsoft.com/azure/register.mspx. Microsoft is trying to regulate how many developers get on the service so you may not be able to access all the parts of Azure immediately. Once you have applied you will receive a series of e-mails with the codes. The e-mails come in pairs, the first one gives you the code and the second lets you know that is has been activated. There are three different codes and you probably won’t get them all at the same time. The one needed to access SDS is the “Microsoft .NET Services and Microsoft SQL Services” code. I received my code for this service within 24 hours of signing up, but it may take longer. At the time of this writing I haven’t received codes for any of the other services.

Once you receive the code you will then have to sign up for the actual service. In the activation confirmation e-mail there will be a link to the page where you can enter the invitation code. At this point you will be asked to create a new solution. You just have to provide a name for the solution which will also become the username for logging into the service. You can only have one solution per invitation code. Once you have created the solution you will be provided a password for that solution.

The final step in setting up for SDS is to download and install the SDS SDK. Unlike the Azure SDK which requires Vista or Server 2008, the SDS SDK works under XP and even Windows 2000.

Once you have the SDK installed you can test things out. In the SDK folder on the Start Menu you will find a tool called SSDS explorer. When you start the tool you should see “https://data.database.windows.net/v1/” in the address bar and “from e in entities select e” in the query box. Click the Query button and a box will pop up allowing you to enter your username and password. The username is the name of the solution you created and the password is the one you received when you created the solution. The query should run without returning any errors.

Now we are finally ready to start working with SDS. I will start getting into the details of how SDS works in my next posting.

Sunday, January 18, 2009

Windows Azure

I have recently started working with the Microsoft Windows Azure service. Announced at last years PDC, Azure is Microsoft’s “cloud computing” platform akin to Amazon’s Elastic Computer Cloud. Windows Azure will run in Microsoft data centers and provide a hosting platform to run applications either completely in the cloud, or run services that can interface with on-premise applications.

The big advantage this will provide over traditional web hosting is the ability to scale on demand. At a moments notice you will easily be able to scale a service from running on one server to running on 10, at a price of course, and then scale back to one when you no longer need the extra capacity. This will be very useful for businesses that have cyclic capacity needs, like a flower shop that needs considerably more capacity around Valentines Day then at other times of the year. It’s also great for independent developers allowing them to quickly and cheaply set up a new application and then easily scale it as new customers come along and demand increases.

For .NET developers Azure supports the familiar .NET languages like C# and VB.NET (although Microsoft has indicated that support for languages like PHP may come in the future) and provides tools that integrate into Visual Studio. All of this makes for an easier learning curve for existing .NET developers.

I will be posting more information on Azure as I started getting familiar with the platform.

You can get more information on azure on Microsoft’s Azure Service Platform page.

Saturday, January 17, 2009

C# Type Inference

C# 3.0 introduced a new language concept called Type Inference. Here is an example:

var n = 3;

Instead of specifying a type in this variable declaration the keyword var is used. When this line is compiled the compiler will determine the appropriate data type for the variable based on the initial value assigned to it. In this case ‘n’ will be an Int32. I think ‘var’ was an unfortunate choice for this keyword since it brings to mind Variants from VB6. Variants changed type based on what was assigned to them, but this is not the case with var. Variables declared with var are strongly typed but the compiler determines the type, not the programmer. The following code will not compile:

var n = 3;

n = “test”;

This will produce the error “Cannot implicitly convert type 'string' to 'int'”.

You can also use var to declare arrays like this:

var nums = new[] {0, 1, 2};

This will result in an array of Int32. Note that when arrays are declared every element has to be of the same type. For example this line will produce a compiler error:

var nums = new[] { 0, "test", 2 };

There are a couple limitations to using var.

- var can only be used for local variables and cannot be used at the class level.

- The variable has to be initialized in the same line as it is declared. You cannot do:

var n;

n = 1;

-You cannot define multiple variables at one time using var. This is not legal:

var n = 3, x = 2;

When you look at this feature and some of the other new features introduced in C# 3.0 they may appear of limited use and pretty random. Although these features do have their uses the main purpose is to support a major new feature in 3.0, Language Integrated Query (LINQ). I will talk about this in a later blog post.

Friday, January 16, 2009

Programming Visual Basic applications?

Typemock have released a new version of their unit testing tool, Typemock Isolator 5.2.This version includes a new friendly VB.NET API which makes Isolator the best Isolation tool for unit testing A Visual Basic (VB) .NET application.
Isolator now allows unit testing in VB or C# for many ‘hard to test’ technologies such as SharePoint, ASP.NET MVC, partial support for Silverlight, WPF, LINQ, WF, Entity Framework, WCF unit testing and more.

Note that the first 25 bloggers who blog this text in their blog and tell us about it, will get a Free Full Isolator license (C#, VB, and Sharepoint included - worth $139 !!!). If you post this in a VB.NET dedicated blog, you'll get a license automatically (even if more than 25 submit) during the first week of this announcement.

Go ahead, click the following link for more information on how to get your free license.

Sunday, January 11, 2009

Tuesday, January 6, 2009

SetValue/GetValue

Occasionally you will run into a situation where you want to reference the property of an object by its name instead of directly accessing the property. Using the SetValue and GetValue functions you can actually access a property where the property name is stored in a string. Let’s start with a simple object to demonstrate this

public class Test
{
 private string name;

 public string Name
 {
 get { return name; }
 set { name = value; }
 }
}

Next you will need to import the Reflection namespace:

using System.Reflection;

Finally here is the code that will write and then read back the Name property:
Test testObj = new Test();
PropertyInfo pi = testObj.GetType().GetProperty("Name");
pi.SetValue(testObj, "Dan", null);
Console.WriteLine(pi.GetValue(testObj, null));

We start out by creating an instance of the Test object and then use the GetProperty function to get the PropertyInfo for the property “Name”. Property info allows you to access various information about a property and also provides access to the GetValue and SetValue functions. If the property can’t be found, GetProperty will return a null, so if there is any chance that the property name you are trying to access may not exist you would want to check if pi is null and handle that error appropriately.

Now that we have the PropertyInfo for the Name property we can write to it with SetValue. SetValue takes three parameters. The first is the object whose property we want to set, in this case the test object. The second parameter is the value we want to set the property to. The third is use for indexed properties which we will talk about next, for this example we will set that null.
Reading the value back is just as easy, we just call the GetValue function. Like SetValue, GetValue takes the object to get the property from as the first parameter, and the index as the second, again null in this example.

As I mentioned you can also use SetValue/GetValue to access an indexed property. Here is another sample object to work with:

public class Test
{
 private int[] num = new int[5];

 public int this[int index] 
 {
 get { return num[index]; }
 set { num[index] = value; }
 }
}

The code to read and write this property would look like this:

pi = testObj.GetType().GetProperty("Item");
pi.SetValue(testObj, 1, new object[] { (int)1 });
Console.WriteLine (pi.GetValue(testObj, new object[] { (int)1 }));

There are a couple differences to note here. First, in C# the default property of an object is the only one that can be indexed, that’s why the test object declares the property name as ‘this’. By default the name of the default property is ‘Item’ so that is what we pass to the GetProperty function. If you want you can change the name of this property by putting the following attribute before the property declaration in the object:

[IndexerNameAttribute("IndexedInstanceProperty")]

This will change the name of the property from Item to IndexedInstanceProperty. Note that you will have to import the System.Runtime.CompilerServices namespace to us this attribute.
The second difference is the index property that is passed to the SetValue and GetValue functions. This parameter is an array of objects. In this example we are using a static index of 1 but it takes a little extra code to turn this single value into an array.

You are probably not going to use SetValue and GetValue a lot but there are definitely situations where it really comes in handy.

Friday, January 2, 2009

Unused Local Variable

I recently ran into an interesting quirk (not sure if I even want to call it that) in Visual Studio. Here is a piece of code that demonstrates the quirk:

Sub Main()
Dim a As Integer
a = 1

Exit Sub

Dim b As Integer
b = 1
End Sub

If you paste this into Visual Studio you will notice that you get a squiggly under the ‘b’ variable with an warning that says Unused Local Variable. I first encountered this in a much large function I was developing and it drove me crazy for a while until I realized that it was the Exit Sub earlier in the code that was causing it. Obviously you would never do this in production code but I had inserted the Exit Sub for debugging purposes.
This quirk was interesting to me because VS was obviously smart enough to know the b=1 would never be executed but didn’t take into account the fact that it would never even be declared. You can get some more insight into this by looking at the IL disassembly for the code:
.method public static void  Main() cil managed
{
  .entrypoint
  .custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 ) 
  // Code size       3 (0x3)
  .maxstack  1
  .locals init ([0] int32 a,
           [1] int32 b)
  IL_0000:  ldc.i4.1
  IL_0001:  stloc.0
  IL_0002:  ret
} // end of method Module1::Main

You can see in the IL that both local variables a and b are declared before that start of the code. You will also notice that nothing after the Exit Sub gets compiled, the assignment of b does not show up in the IL. So the behavior we see in VS is consistent with what we see in the IL, b is defined but never used.