Setting Up Zero Friction Projects - Data Access

time to read 2 min | 342 words

After talking about the advantages setting up environments where the laziest approach is also the faster approach, I want to talk a bit about the details of actually doing it.

Zero Friction approach is very important for the overall success of a project, because it means that the right thing is the easy thing, so that is what will get done.

Something to note is that this doesn't excuse ignorance, the developers using this approach should be able to at least understand the concepts and methods that they use. I am saying that there is no excuse for making the right thing the hard thing. That is setting yourself up for failure.

We will start from the database, a subject near and dear to my heart. When you are working with a database, you are going to have to deal with schema changes, and those can be a pain if you are making rapid modifications, very often. This is even more of a pain when you are building the domain model, and keep having to make changes all the time.

My solution for that is to setup a /home/createindex.castle action, which has the following code:

[AccessibleThrough(Verb.Post)]
public void CreateDatabase(bool allow)
{
	PropertyBag["allow"] = allow;
	if (allow == false)
	{
		PropertyBag["message"] = "You must check the box in order to create the database";
		return;
	}
	if(Request.IsLocal == false)
	{
		PropertyBag["message"] = "This action is only valid from the local machine";
		return;
	}
	try
	{
		ActiveRecordStarter.CreateSchema();
		PropertyBag["message"] = "Database successfully created";
	}
	catch (Exception ex)
	{
		PropertyBag["error"] = ex;
	}
}

This means that I can hit a web page, check a box, submit and I have a new database. The reason that I have the extra steps in the middle is that I this is a destructive operation.

Another option is to use the SchemaUpdate feature that was recently added to NHibernate, that is a non destructive operation, one that you can safely put inside the Application_Start method.