Know those associations

time to read 3 min | 550 words

I just had a run into a mess of my own cooking. I had a set of classes, where one class held references to some other classes, and I didn’t map the associations properly. Not in the software and defiantly not in my mind.

Enter a lot of frustration about WTF is going on here.

I didn’t map all the associations and their cascades, which meant that I run into a lot of trouble when I started to really work with the model. Luckily I’ve a suite of tests that I can run, so I can play around with the cascades and see what is going on.

To add to the problem, I tried to do two things with it, the first was to follow the business logic and create associations that would make it easier to work with the objects. The second was to allow a to clear the database for the tests.

This got me into a lot of trouble…

I couldn’t use NHibernate to do it, because it failed to do a mass delete since the associations and cascades where in the way. At first I tried to work around that, but pretty soon it became clear that it’s not going to be possible.

I finally decided to separate the two concerns. I have not problem with having testing hooks, but changing the business logic because you want to have easier testing is a symptom that you went a long way too far.

I restored the association to the way they were supposed to be according to the model, and I’m going to keep an eye on that, I didn’t really paid attention to that before, and it’s going to be critical in a few days, when I start to put it all together.

For the tests, I’m using a simple function that deletes all the rows from the database after each test. On the first time that the tests are run, the whole database is recreated from scratch.

I’m not using Active Record to do it, since other tools need the schema as well, and I want to make sure that I don’t make accidental change to the schema by messing around with the business objects.

Still working on the rules, now it’s a rule that is a collection of rules. This one had me using just about any feature of C# 2 to get it done cleanly. I’ll post about it later.