About the Caché Class Definition Classes

The Caché Library set of class definitions classes has been superceded by the %Dictionary package. The %Library classes described here are maintained for compatibility with existing applications.

New code should make use of the classes within the %Dictionary package. Documentation on can be found in the online documentation in the Caché Development Guides section in the book titled, Using Caché Objects.

Make sure that you specify the correct package name when using these classes or you may inadvertently use the wrong class.

The Caché Library includes a number of class definition classes that provide object access to the Caché Unified Dictionary. Using these classes you can programmatically examine class definitions, modify class definitions, create new classes, or even write programs that automatically generate documentation.
There are two parallel sets of class definition classes: those that represent defined classes and those that respresent compiled classes. A defined class definition represents the definition of a specific class. It includes only information defined by that class; it does not include information inherited from super classes. In addition to providing information about classes in the dictionary, these classes can be used to programmatically alter or create new class definitions.
A compiled class definition includes all of the class members that are inherited from super classes. A compiled class definition object can only be instantiated from a class that has been compiled. You cannot save a compiled class definition. In this document we will only discuss defined classes but the operation of the compiled class definitions is similar.
The family of class definition classes that represent defined classes includes:
%AbstractDefinition Abstract base class for all class definition classes.
%ClassDefinition Represents a class definition. Contains class keywords as well as collections containing class member definitions.
%ForeignKeyDefinition Represents a foreign key definition within a class.
%IndexDefinition Represents an index definition within a class.
%MethodDefinition Represents a method definition within a class.
%ParameterDefinition Represents a parameter definition within a class.
%PropertyDefinition Represents a property (attribute) definition within a class.
%QueryDefinition Represents a query definition within a class.
%TriggerDefinition Represents an SQL trigger definition within a class.
Browsing Class Definitions
You can use the class definition classes to browse through the class definitions within the Caché dictionary using the same techniques you would use in a database application: you can use the %ResultSet object to iterate over sets of classes and you can instantiate persistent objects that represent specific class definitions.
For example, from within a Caché process you can get a list of all classes defined within the dictionary for the current namespace by using the %ClassDefinition.ClassInfo query:
New result
Set result=##class(%ResultSet).%New("%ClassDefinition.ClassInfo")
Do result.Execute()
For  Quit:result.Next()=0 Write result.GetDataByName("Name")
Do result.%Close()
You can just as easily invoke this query from an ActiveX or Java client using the client ResultSet object.
This %ClassDefinition.ClassInfo query will return all of the classes visible from the current namespace (including classes in the system library). You can filter out unwanted classes using the various columns returned by the %ClassDefinition.ClassInfo query.
You can get detailed information about a specific class definition by opening a %ClassDefinition object for the class and observing its properties. The ID used to store %ClassDefinition objects is the class name:
Set cdef=##class(%ClassDefinition).%OpenId("Employee")
Write cdef.Name
; get list of properties
Set count=cdef.Properties.Count()
For i=1:1:count Write cdef.Properties.GetAt(i).Name
Do cdef.%Close()
You can also do this easily from an ActiveX or Java client.
Altering Class Definitions
You can modify an existing class definition by opening a %ClassDefinition object, making the desired changes, and saving it using the %Save method.
You can create a new class by creating a new %ClassDefinition object, filling in its properties and saving it. When you create %ClassDefinition object you must pass the name of the class via the %New command. When you want to add a member to the class (such as a property or method) you must create the corresponding definition class (passing its %New command a string containing "classname.membername") and add the object to the appropriate collection within the %ClassDefinition object.
For example:
Set cdef=##class(%ClassDefinition).%New("MyClass")
Set cdef.Persistent=1
Set cdef.Super="%Persistent,%Populate"

; add a Name property
Set pdef=##class(%PropertyDefinition).%New("MyClass.Name")
Do cdef.Properties.Insert(pdef)
Do pdef.%Close()

Set pdef.Type="%String"
; save and close the class definition object
Do cdef.%Save()
Do cdef.%Close()