Sql – Strategies for using NHibernate to generate a schema

nhibernatesql

I'm building a new app that is using NHibernate to generate the database schema but i can see a possible problem in the future.

Obviously you use all the data from your database is cleared when you update the schema but what stratagies do people use to restore any data to the new database. I am aware that massive changes to the schema will make this hard but was wondering how other people have dealt with this problem.

Cheers
Colin G

PS I will not be doing this against the live database only using it to restore test data for integration test and continuous integration

Best Answer

When testing, we use NHibernate to create the database, then a series of builders to create the data for each test fixture. We also use Sqlite for these tests, so they are lightening fast.

Our builders look a something like this:

public class CustomerBuilder : Builder<Customer>
{
   string firstName;
   string lastName;
   Guid id = Guid.Empty;

   public override Customer Build()
   {
      return new Customer() { Id = id, FirstName = firstName, LastName = }
   }

   public CustomerBuilder WithId(Guid newId)
   {
      id= newId;
      return this;
   }

   public CustomerBuilder WithFirstName(string newFirstName)
   {
      firstName = newFirstName;
      return this;
   }

   public CustomerBuilder WithLastName(string newLastName)
   {
      lastName = newLastName;
      return this;
   }
}

and usage:

var customer = new CustomerBuilder().WithFirstName("John").WithLastName("Doe").Build();

Because every line of code is written with TDD, we build up a comprehensive suite of data from scatch and will generally refactor some of it to factories that will wrap the above and make it a breeze to get dummy data in.