In Part 6 I began working on the data side of the checkbox code and created a class to manage the links between "left" and "right" records. I also loaded up some data into an instance of the class.
...
There's an easy solution to this, which is to split the queue that contains the data out into its own class. This may seem like unnecessary proliferation of classes, but it will help to keep the code clean, and instantiating classes is a very inexpensive operation , especially as compared to data database access.
Here's my new class:
...
Have I mentioned how much I like having unit tests to fall back on? Refactoring is so much less stressful this way.
Now I can pass just the link data to the persister class methods:
...
Ideally I should be able to drop in any kind of persister I want without making code changes. And in fact I have written one such persister for TPS files. I'll explain how it works next time; for now, here's the unit test code to make sure that saving and loading works as expected.
Code Block |
---|
DeleteSaveAndLoadDataSaveAndLoadData PROCEDURE (*long addr) ! Declare Procedure ManytoMany CML_Data_ManyToManyLinks itemize(),pre() RightRecordX equate RightRecordY equate RightRecordZ equate end Persister CML_Data_ManyToManyLinksPersisterForTPS Filename cstring(500) CODE addr = address(UnitTestResult) BeginUnitTest('DeleteSaveAndLoadData') Filename = longpath() & '\CML_Data_ManyToManyLinksTests.tps' if exists(filename) remove(filename) end AssertThat(exists(filename),IsEqualTo(false),'Could not delete ' & filename) SaveAndLoadData') Persister.SetFilename(Filename) ManytoMany.Persister &= Persister ManyToMany.LeftRecordID = 1 ManyToMany.SetLinkTo(RightRecordX) ManyToMany.SetLinkTo(RightRecordZ) AssertThat(ManyToMany.IsLinkedTo(RightRecordX),IsEqualTo(true), 'before save, record X should be linked') AssertThat(ManyToMany.IsLinkedTo(RightRecordY),IsEqualTo(false),'before save, record Y should not be linked') AssertThat(ManyToMany.IsLinkedTo(RightRecordZ),IsEqualTo(true), 'before save, record Z should be linked') ManyToMany.Save() ManyToMany.Reset() AssertThat(ManyToMany.IsLinkedTo(RightRecordX),IsEqualTo(false),'after reset, record X should not be linked') AssertThat(ManyToMany.IsLinkedTo(RightRecordY),IsEqualTo(false),'after reset, record Y should not be linked') AssertThat(ManyToMany.IsLinkedTo(RightRecordZ),IsEqualTo(false),'after reset, record Z should not be linked') ManytoMany.Load() AssertThat(ManyToMany.IsLinkedTo(RightRecordX),IsEqualTo(true), 'after load, record X should be linked') AssertThat(ManyToMany.IsLinkedTo(RightRecordY),IsEqualTo(false),'after load, record Y should not be linked') AssertThat(ManyToMany.IsLinkedTo(RightRecordZ),IsEqualTo(true), 'after load, record Z should be linked') |
...
After that I load the data back up from the TPS file, and re-run the original set of tests to verify that the data has been restored. Does it work? Yes!
Next time: how the code works, and the changes required to the existing classes.