Thursday, December 6, 2007

Cool Things in .NET 3.5

This does require Visual Studio to upgrade the database. I am using the Visual Studio Professional Trial for now.

So download the sqlce database from http://www.andargor.com/. Unpack the zip file to the C:\SQLMETAL directory. Now upgrade it according to Microsoft's Instructions at Upgrading CE from Earlier versions.

Next open a Visual Studio Command prompt and change to the directory where you have the compact database from above.

type in the following command

sqlmetal /code:srdce.cs /namespace:d20 /pluralize /serialization:unidirectional /language:csharp srdce.sdf

Open the file srdce.cs in Visual Studio. You now have a class library representation of the database and tables in the srdce.sdf database (in my case it is 6600+ lines of code) with full attributes for the database columns and tables with 10 classes (one per table) that describe the columns of each table and on class name srdce that has the collections classes for each of the table.

Now open a new C# class library (dll) project in Visual Studio named srdce and add C:\SQLMETAL\srdce.cs to it as a C# class.

Next add references to System.Data.Linq and System.Runtime.Serialization. Now compile once for a test to make sure you got everything right.

Next add a new test project to the solution named srdcetest. Add a project reference to the srdce project to the srdcetest project. Right click on the node for srdcetest and select add/Unit test. This will generate unit tests for all the classes generated from the database.

Next compile the solution to make sure everything works. Now go to the Test Menu and select "run all tests in solution". All the test should fail with a Assert.Inconclusive.

So now we do a little of the red/green/refactor thing. All of our tests fail, so we got red down. Now I want to have a test pass. So goto the SkillsTest method in the SrdceTest class. Change the code to

//IDbConnection connection = "d:\\sqlmetal\\srdce.sdf"; // TODO: Initialize to an appropriate value
Srdce target = new Srdce("c:\\sqlmetal\\srdce.sdf"); // TODO: Initialize to an appropriate value
Table<Skill> actual;
actual = target.Skills;
List<Skill> SkillList = new List<Skill>();
foreach (var Skill in actual)
SkillList.Add(Skill);
Assert.AreEqual(SkillList.Count, 40);

Now go to the using block at the top of the test class and add

using System.Collections.Generic;

Now go back to the SkillsTest method and right click and choose run tests. This should complete correctly.

So we have taken a database, created a class hierarchy for it, created some unit tests, refractored a unit test to be green and basically the result is as follows:

  1. We now have a done a ORM mapping between a database and a C# class hierarchy.
  2. We generated 304 Unit Tests.
  3. We proved that we can select all the data from the skills table and put it into a list for later use in the application. The list is a strongly typed list of the base data type.

Pretty cool, eh?

In later entries I will show how to create a database from the class hierarchy and how to persist data to the database. Finally we will look at how to make the class hierarchy a set of dll's that can be used as plugins in a application.

No comments: