constructor Create (aCustKey: TCustomerKey); property CustKey: TCustomerKey read FCustKey write SetCustKey; pub1ished property CustNo: Doub1e read GetCustNo write SetCustNo; property Company: string
84 Dictionary in this case means a collection of elements each with a (unique) key value referring to it. (It is also known as an associative array.) In a classic dictionary you have words acting as keys for their definitions, but in programming terms the key doesn't have to be a string (even if this is a rather frequent case).
read GetCompany write SetCompany; property Addrl: string read GetAddrl write SetAddrl; property City: string read GetCity write SetCity; property State: string read GetState write SetState; property Zip: string read Getzip write Setzip; property Country: string read GetCountry write SetCountry; property Phone: string read GetPhone write SetPhone; property FAX: string read GetFAX write SetFAX; property Contact: string read GetContact write SetContact;
While the first class is very simple (each object is initialized when it is created), the TCustomer class uses a lazy initialization (or proxy) model and keeps around a reference to the source database shared (class var) by all objects. When an object is created it is assigned a reference to the corresponding TCustomerKey, while a class data field refers to the source dataset. In each getter method, the class checks if the object has indeed been initialized before returning the data, as in the following case:
function TCustomer.GetCompany: string; begi n
Result := FCompany; end;
The EnforceInit method checks a local flag, eventually calling Init to load data from the database to the in-memory object:
procedure TCustomer.EnforceInit; begi n if not fInitDone then Init;
procedure TCustomer.Init; begi n
RefDataSet.Locate( 'custno', CustKey.CustNo, );
// could also load each published field via RTTI
FCustNo := RefDataSet.FieldByName ('Cust/Vo').AsFloat; FCompany := RefDataSet.FieldByName ('Company').AsString; FCountry := RefDataSet.FieldByName ('Country').AsString;
Given these two classes, I've added a special purpose dictionary to the application. This custom dictionary class inherits from a generic class instantiated with the proper types and adds to it a specific method: type
TCustomerDictionary = class (
TObjectDictionary <TCustomerKey, TCustomer>) publ ic procedure LoadFromDataSet (dataset: TDataSet); end;
The loading method populates the dictionary, copying data in memory for only the key objects:
dataset: TDataSet); var custKey: TCustomerKey; begi n
TCustomer.RefDataSet := dataset;
while not dataset.EOF do
Was this article helpful?
What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.