Scott White's Tech Blog

Technology with opinion

Thursday, November 19, 2009

Mapping Date & Time fields With NHibernate

Let's say you have an entity or POCO that you have mapped from NHibernate (or any ORM) and the database has separated out the Date & Time values into separate fields.  Your best best is to map these Date & Time fields into properties (DateTime for Date and TimeSpan for Time).  Then create a helper property in your POCO to add them together.  I'd like to add that I'm not a fan of separating these sorts of things in the database, but some people do this for some reason:

HBM:


POCO

In my case either of these could be null (yes another great idea) which means I had to handle the nullability however this illustrates at least in worse case scenerio what will happen.  Date & Time data types are available in SQL Server 2008 and work with NHibernate 2.1 with .Net 2.0 or above.

Monday, November 16, 2009

Associating Users & Logins in SQL Server after Database Restore

Often times when you restore a database in SQL Server from one server to the next the Logins will become disassociated with the User a specific database.  We ran into this today and another software engineer found a good script from a guy he knows:



Executing this script should find unassociated logins and reassociate them with the users in the current database

Friday, November 06, 2009

CI Done Simple with CruiseControl.Net + MSBuild

If you want to setup Continuous Integration the learning curve for automated builds can be moderate for some. CruiseControl.Net (CCNet) allows you to integrate with automated build technologies such as NAnt, MSBuild, etc.  The advantage of this is that you can specify the Visual Studio Solution so that MSBuild knows the references and source code to build.

This example assumes using TFS for source code control, which definitely isn't my favorite SCM tool but odds are if you want to keep it simple and you're not using NAnt then you're probably in a shop that uses TFS.

Sunday, October 25, 2009

Alt.Net Google Reader Bundle

Google Reader is a great way to subscribe to different RSS feeds because it enables certain social networking features on top of them.  If you are "Sharing" with other users then you will see what other users in your network liked and their comments.  It also makes it very easy to send links to people via email.

I created an ALT.Net Google Reader bundles a while back that I try to keep updated with different ALT.Net blogs that I subscribe to.  To subscribe to this bundle click the link below and then click the link "Subscribe".
http://www.google.com/reader/bundle/user/03066432769507324653/bundle/Alt.Net

You can expand the feeds to see what you are subscribing to and even remove certain feeds after you add the bundle.  If you have an ALT.Net blog and would like your blog added to this bundle just send me a message on Twitter or leave a comment here.

Tuesday, September 29, 2009

Practical use for NUNit 2.5's [TestCase]

Since NUnit 2.5, NUnit has supported a concept of Test Cases.  Briefly a test case to NUnit is a test which runs with a different set of parameters and can expect a different set of results.  This has the potential to minimize the amount of code required to write new test methods.

To this point I've been using standard [Test] attributes and since seeing this attribute in the documentation I've been thinking about uses for it.  Now that I've started using [TestCase] I've thought of some other uses for it cutting my test fixtures down to 1/2 or less the size previously.

Below is an simple example of the code without the [TestCase] using the traditional [Test] attribute:


The above code can be trimmed down to one function using the [TestCase] attribute:



The other beautiful thing about [TestCase]s is that it collapses each test case underneath a node which actually looks cleaner:



A couple of notes on the code above.  The compiler (C# here) will not allow you to use object initializers as a parameter to the [TestCase] so you cannot create a test function Validate(object myUser) and do something like below.



This is because the compiler will only allow constants in attributes, however if you really need something like this, NUnit allows a work around. This is documented here.

Sunday, September 27, 2009

Houston TechFest Samples

Houston TechFest was a lot of fun, I attended some very interesting sessions and had the opportunity to present on the topic of NHibernate.  I have uploaded the samples and they are downloadable by clicking here.  The presentation is available here.

Wednesday, September 23, 2009

Unit Testing - Mocking to test Edge Cases with NHibernate

Sometimes in code you want to test edge cases, some of these can be difficult to test even with integration tests because of database constraints, etc.  This may be a business key or unique identifier (that's not a primary key)- may be enforced or not.  The key is you make an assumption that in code you want to test for.  Mocking dependencies is the key here, to create behavior that would not otherwise be possible.

Unlike the pattern illustrated by Ayende or that I added onto here, I wanted NHibernate's SessionFactory and Session objects to return data that I could not return from the database because of unique index constraints.

In this scenerio I'm returning data from the database that matches certain values and it should always return a list that contains either 0 or 1 objects otherwise it throws an exception:


Problem is that integration tests are unlikely to uncover this error with normal data and end user testing won't uncover it either.  This is simple, but I want to make certain that the exception is correctly thrown.

The only tricky part really is that after mocking the ISessionFactory & ISession objects you need to also mock each ICriteria object returned from the Session object.  So with Rhino Mocks after telling it to expect a call to CreateCriteria that returns an ICriteria, we also need to create expectations for this result calling Add and its result calling list.  This is because fluent interfaces use method chaining which makes this part a little tedious:


This allows us to now test what would actually happen if more than one Animal objects were returned.  NHibernate makes this easy because there are interfaces for pretty much every object.

FYI: this code uses Rhino Mocks, NBuilder, NUnit & NHibernate