Tuesday, August 08, 2006

Tutorial - NHibernate and VS2005 - Part 1

Back on track. Or at least I hope so.

In part 1 my goals are:

  1. create a class Customer holding some archetypical customer fields
  2. create a database with a single table Customer
  3. write enough NHibernate plumbing code to connect to this database and
  4. persist an instance of the Customer class into the database
Not terribly lofty, but achievable. I hope.

Let's get started with
  1. create a class Customer holding some archetypical customer fields
I have installed SQL Server 2005 Express on a machine called, say, BUBBAPC. On this machine I have a single server called, say, BUBBADB (try saying that, it's fun buh-bah-dee-bee hee-hee-hee.) Also, say, I am running this as user BUBBA.

Also assume that my sa account has password sabubba.

Connect to the database either through Microsoft SQL Server Management Studio or with
sqlcmd -S BUBBAPC\BUBBA01 -U sa -P sabubba
Now, dump in the following script:

 use master

 go

 create database nhibernate

 go

 create login [BUBBAPC\BUBBA] from windows

 go

 use nhibernate

 go

 create user [BUBBA] for login [BUBBAPC\BUBBA]

 go

 exec sp_addrolemember 'db_owner', 'BUBBA'

 go


Let's try this out:
sqlcmd -S BUBBAPC\BUBBA01
1> select count(*) from sysobjects
2> go
-----------
1762
(1 rows affected)
1> quit
Wow, fancy.

Now, back to Visual Studio. I'm assuming you've loaded the solution file from Part 0.1.

Add a new class file to NHibernateTest called Customer.cs

Remove all of the autogenerated cruft and replace it with this:

    1 using System;

    2 

    3 namespace NHibernateTest

    4 {

    5     public class Customer

    6     {

    7         #region ID

    8         private int m_ID;

    9         public int ID

   10         {

   11             get { return m_ID; }

   12             set { m_ID = value; }

   13         }

   14         #endregion

   15 

   16         #region Name

   17         private string m_Name;

   18         public string Name

   19         {

   20             get { return m_Name; }

   21             set { m_Name = value; }

   22         }

   23         #endregion

   24     }

   25 }


That's all we need to do for the POD class Customer
Now we need to tell NHibernate what to do with this class. This is all driven by an xml file.

Add an XML File called Customer.hbm.xml.

Drop in this bit of xml:

    1 <?xml version="1.0" encoding="utf-8" ?>

    2 <hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"

    3     default-lazy="false">

    4   <class

    5       name="NHibernateTest.Customer, NHibernateTest"

    6       table="Customer">

    7     <id name="ID" column="ID" type="Int32">

    8       <generator class="identity" />

    9     </id>

   10     <property name="Name" column="Name" type="String"

   11       length="50"/>

   12   </class>

   13 </hibernate-mapping>


Set the Build Action for Customer.hbm.xml to Embedded Resource.

And now we need to create the underlying table. First, make sure the Server Explorer is visible in Visual Studio. You can get there with - Ctrl-Alt-S or View|Server Explorer.

Right click on Data Connectons|Add Connection...
Data Source: Microsoft SQL Server
Continue

  Server Name: BUBBAPC\BUBBA01
  Log in using Windows Authentication
  Database Name: NHibernate

Right click on Tables|Add New Table...
Add the following columns
  Column NameData TypeAllow Nulls
  IDintNo
  Namevarchar(50)No

Right click on ID and Set Primary Key. in the Column Properties tab find 'Identity Specification' and set Is Identity to Yes. It should look something like this:


Save this. When it asks for the table name enter Customer.

We're circling in now..

Add a reference to the NHibernate Assembly nhibernate.dll

Add an Application Configuration File call App.config to NHibernateTest with this XML:

    1 <?xml version="1.0" encoding="utf-8" ?>

    2 <configuration>

    3   <configSections>

    4     <section name="nhibernate"

    5             type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>

    6   </configSections>

    7 

    8   <nhibernate>

    9     <add key="hibernate.connection.provider"

   10       value="NHibernate.Connection.DriverConnectionProvider"/>

   11     <add key="hibernate.dialect"

   12       value="NHibernate.Dialect.MsSql2000Dialect"/>

   13     <add key="hibernate.connection.driver_class"

   14       value="NHibernate.Driver.SqlClientDriver"/>

   15     <add key="hibernate.connection.connection_string"

   16       value="Server=BUBBAPC\BUBBA01;initial catalog=nhibernate;Integrated Security=SSPI"/>

   17   </nhibernate>

   18 </configuration>


Replace the contents of Program.cs with this:

  1 using System;
  2 using System.Diagnostics;
  3 using System.Reflection;
  4 
  5 using NHibernate;
  6 using NHibernate.Cfg;
  7 
  8 namespace NHibernateTest
  9 {
  10    class Program
  11&;;nbsp;   {
  12      static void Main(string[] args)
  13      {
  14        try
  15        {
  16          Configuration cfg = new Configuration ();
  17          cfg.AddAssembly (Assembly.GetExecutingAssembly ().GetName ().Name);
  18 
  19          ISessionFactory factory = cfg.BuildSessionFactory ();
  20 
  21          using (ISession session = factory.OpenSession ())
  22          using (ITransaction transaction = session.BeginTransaction ())
  23          {
  24            Customer customer = new Customer ();
  25            customer.Name = "Joseph Cool";
  26 
  27            session.Save (customer);
  28 
  29            transaction.Commit ();
  30 
  31            Debug.WriteLine ("Success! ID=" + customer.ID);
  32          }
  33        }
  34        catch (Exception e)
  35        {
  36          Debug.WriteLine (e);
  37        }
  38      }
  39    }
  40 }


If you see this error: NHibernate.MappingException: Unknown entity class: NHibernateTest.Customer that means you forgot to update the Build Action for Customer.hbm.xml to Embedded Resource

That's it. C'est fini.

Everytime to run this app you will get another row in Customer.

Monday, August 07, 2006

Tutorial - NHibernate and VS2005 - Part 0.1

Okay, slight wrinkle. I spent the better part of the last 2 days just getting back into working order. My version of SQL Server 2005 wasn't getting along with my version of Visual Studio 2005. Specifically, SQL Server wouldn't connect. I figure I'd re-install, and from there things went downhill.

After several uninstall/reinstall/repair cycles it finally dawned on my that my CTP of SQL Server 2005 couldn't possibly play nice with Visual Studio as it was compiled against a beta of the .NET framework. D'Oh. That explains, of course, why SQL Server 2005 kept trying to install a beta of Visual Studio.

So, SQL Server 2005 Express it is then.

Oh well, now that the bitterness has passed I'm ready to move on again.

I'm happy to say that my app works flawlessly. And here it is:

    1 using System;

    2 

    3 namespace NHibernateTest

    4 {

    5     class Program

    6     {

    7         static void Main(string[] args)

    8         {

    9         }

   10     }

   11 }


Stunning isn't it?

It's exactly what it looks like: the standard wizard-generated mainline for a console app.

Since this is a tutorial, I might as well go through the steps:


The only non-standard entry is unchecking the 'Create directory for solution' check box.

That's it, just one step. After the day and a half of re-installing it's all I can deal with right now.

[Ed: Copied with CopySourceAsHtml]

[Listening to (via Pandora): Glenn Miller - Pennsylvania Six-Five Thousand]

Thursday, July 06, 2006

Tutorial - NHibernate and VS2005 - Part 0

Ok, maybe I'm just dumber than the average bear, but I just couldn't get NHibernate working.

I tried several beginner level tutorials, but still nothing.

Mind you I was trying the lastest alpha release (1.2.0 alpha 1).

And I was trying to hook up to Access 2003 as a back end which adds it's own complexity.

So, I figured I'd go back to First Principles. All of the tutorials use SQL Server 2005 which I have as part of Visual Studio 2005 Standard.

I still wanted to stick with the alpha, not sure why, but hey, I am a geek afterall. Fortunately I found a link with some of the magic.

Next, I figured I'd dumb it right down. 1 table, 2 columns. If I can't get that working then I probably should've gone into a different line of work.

That's enough for the preamble. Next, I'll actually start cutting some code.

[Listening to (via Pandora): Garbage - When I Grow Up]

Wednesday, April 26, 2006

Checking file contents using Subversion Hooks

I found lots of examples of verifying checkin comments using Subversion commit hooks but nothing which showed me how to validate the contents of the file.

In my case I want to make sure that I'm not checking in any unit test files that have classes decorated with [CurrentFixture]. FYI I use MbUnit for my unit tests.

Using perl or shell scripts would be relatively easy but I wanted to do things the batch file way. Not sure why. Something repressed from my childhood no doubt.

I'm "sure" there's an easier way even using batch files but here's what I came up with. In the hooks directory in your Subversion repository edit the pre-commit.bat file like so:

set REPOS=%1
set TXN=%2
set FIXQUOTES=c:\util\FixQuotes
set PROJECTROOT=G:\Projects\VolMan

for /F "usebackq tokens=2" %%f in (`svnlook changed -t "%TXN%" "%REPOS%"`) do (
for /F "usebackq" %%x in (`%FIXQUOTES% %%f`) do (
for /F "usebackq" %%g in (`findstr /LM "[CurrentFixture]" %PROJECTROOT%\%%x`) do (
if not x%%g==x (
echo Cannot check in a file containing a class decorated with [CurrentFixture] 1>&2
exit 1
)
)
)
)


* - the output of svnlook has forward slashes '/' instead of the dos-friendly backslashes '\' which caused findstr to puke. I needed to create a cheeseball little app FixQuotes to flip all forward slashes to backslashes. Here's where perl/shell script would've made things trivial.

It's sooo yucky to hardcode the path to FixQuotes and the root of the project too. But c'est la vie.

If anyone has an easier way pleeease tell me.

[Listening to: Staple - The Day the Blind Revolted]

del.icio.us killed my blog!

Ever since I "found" del.icio.us I have had nothing to blog about. Well, that and my never before heights of laziness.

All of my old "ditto" blog entries ended up just getting tagged instead of blogged about. Not that there's really anyone who reads this (at least yet [fingers crossed]).

I have however recently dusted off my compiler and fired up my last project. Naturally it has nothing to do with Hookup as I'm pretty sure that Windows Workflow Foundation has put an end to my aspirations.

No, I'm heading along a much more reasonable path to world domination (I'd settle for domination of my basement mind you.)

I'm writing a simple app to maintain volunteer information for a race since I know a bit about that.

Hopefully this lasts.

Damn you del.icio.us

[Listening to (via Pandora): Pantera - It Makes Them Disappear]