The home collection class is a very important class that collaborates with the business object to provide a number of support services. It is a singleton class which means there is only ever one instance in the image at any given time.
The primary tasks of the home collection are to provide support protocol for creating, finding and accessing instances of the model class.
TstDepartmentHome singleton allInstances
This will return a collection of all TstDepartment objects. Note that this is different than the class method, allInstances which will actually query the image for all known instances of an object. The home collection object answers a collection of all instances that were created through the public creation protocol on the home collection itself. To illustrate this, evaluate the following code:
TstDepartment new. TstDepartmentHome singleton allInstances.
The department that was created in the first statement does not show up in the list of allInstances that the home collection returns. To create bona fide persistent instance of the TstDepartment class the create method should be used on the home collection.
Before working with our department objects we need to understand the transaction model. At any one point in time there is always a current transaction inside of which all of the work on business objects is done. A special transaction exists which is the shared transaction. The shared transaction represents the persisted view of the business model and is read only. To begin changing persisted objects a new transaction must be started. Each transaction has a parent which is the transaction into which it will commit its changes. A top-level transaction is one which has a parent of the shared transaction, that is, when it is committed its changes will be made persistent. Top-level transactions can be created with the following method:
Transaction begin
To create objects a transaction must be started. The transaction allows work to be undone and committed as a unit of work to the persistent store. To create data for our sample we need to evaluate the following code in the System Transcript window:
Transaction begin. (TstDepartmentHome singleton create) department: 'Math'; room: 'A2'. Transaction current commit.
Now inspect the same code two more times to create English and History departments. Provide a room number for each department as well.
To see the committed department the home collection can be queried for allInstances.
TstDepartmentHome singleton allInstances.
Home collection also have "lookup" protocol to let us search for objects. Method to help locate an object by the key are generated automatically, that is, TstDepartmentHome has a method, findByDepartment: aDepartmentString.
| department | Transaction begin. department := TstDepartmentHome singleton findByDepartment: 'Math'. department room: 'A3'. Transaction current commit.
To delete an object it can be sent the unary method, markRemoved. This does not actually delete the object but rather marks it to be deleted in the current transaction and it is then deleted when the transaction is committed.
| department | Transaction begin. department := TstDepartmentHome singleton findByDepartment: 'Math'. department markRemoved. Transaction current commit.