Last few days I’ve been playing with Aida/Web trying to model a web store. I’ve done this before with RubyOnRails and MySQL so relations and what data should be where is known ground. What was unknown to me is how to represent this in Smalltalk object system. No SQL, only objects.
I’ve started with basic class named Store with sections and items as OrderedCollection’s:
Object subclass: #Store
instanceVariableNames: 'name caption sections items'
classVariableNames: ''
poolDictionaries: ''
category: 'WebStore'
Then I’ve added Section, Item and Image classes. The idea is one store can have multiple sections and items. Items can belong to none, one or many sections. All items belongs to the store.
Object subclass: #Section
instanceVariableNames: 'parent name caption items'
classVariableNames: ''
poolDictionaries: ''
category: 'WebStore'
Object subclass: #Item
instanceVariableNames: 'parent name caption'
classVariableNames: ''
poolDictionaries: ''
category: 'WebStore'
Object subclass: #Image
instanceVariableNames: 'parent name height width content-type caption url'
classVariableNames: ''
poolDictionaries: ''
category: 'WebStore'
The parent instvar is there for back linking to parent object. This way web store can have nested sections (ie. sections within sections) and items as supplemental to items (ie. item variations on price, design, …).
With this model setup done I’ve added methods for adding items to web store. First one is addItem: and second one is class method newNamed:
addItem: anItem
self items add: anItem.
anItem parent: self
newNamed: aName caption: aCaption
^self new
name: aName; caption: aCaption
With this in place, basic web store model is done. At first glance this might not seem much better then building SQL relations. It is practically the same except some minor details (primary keys, indices, etc).
What I’m expecting later on is easier updating and managing this model. With growth, model will become more complex. Managing complexity like this should be one of Smalltalk advantages.