Caché includes a full-featured, next-generation object database
specifically designed to meet the needs of complex, transaction oriented applications.
The Caché Object Model includes the following features:
-
ClassesYou can define classes
that represent the state (data) and behavior (code) of your application components.
Classes are used to create instances of objects as both runtime components
and as items stored within the database.
-
PropertiesClasses can include
properties, which specify the data associated with each object instance. Properties
can be simple literals (such as strings or integers), user-defined types (defined
using data type classes), complex (or embedded) objects, collections, or references
to other objects.
-
RelationshipsClasses can define
how instances of objects are related to one another. The system automatically
provides navigational methods for relationships as well as referential integrity
within the database.
-
MethodsClasses can define behavior
by means of methods: executable code associated with an object. Object methods
run within a Caché server process (though they can be invoked from
a remote client). Object methods can be scripted using Caché ObjectScript,
SQL, or they can be generated using so-called method generators, which are
code that automatically creates customized methods according to user-defined
rules.
-
InheritanceBy deriving new classes
from existing ones, you can reuse previously written code as well as create
specialized versions of classes.
-
PolymorphismCaché supports
complete object polymorphism. This means that applications can use a well-defined
interface (a set of methods and properties provided by a superclass) and the
system will automatically invoke the correct interface implementation based
on the type of each object. This makes it much easier to develop flexible
database applications.
-
Swizzling (also known as lazy
loading)Caché automatically swizzles (brings into memory
from disk) any related persistent objects when they are referenced from other
objects. This greatly simplifies working with complex data models.
An object is an instance of a specific class. Within Caché you
can define classes in several ways. For information on how to define classes
refer to one of the following:
Creating Object Instances
You can create an object instance by either creating a new instance
using the
New command or you can open a persistent object
previously stored in the database using the
OpenId command.
The
New command creates and returns a new instance
of the specified class. For example:
person = New Sample.Person()
person.Name = "El Vez"
PrintLn person.Name
The name of the class (with an optional package name) follows the
New command.
There are parentheses following the class name. If you place an argument within
these parentheses, then it will be passed to the constructor (
%OnNew method)
for the object.
The
New command has exactly the same behavior as
invoking the
%New method in Caché ObjectScript.
The
OpenId command finds, opens, and returns an
instance of a persistent class that is stored within the database using its
unique object identifier value. For example:
person = OpenId Sample.Person(22)
PrintLn "Name: " & person.Name
PrintLn "Age: " & person.Age
The name of the class (with an optional package name) follows the
OpenId command.
There are parentheses following the class name within which contain arguments
for the
OpenId command. The
OpenId command
takes the following arguments:
If the
OpenId command cannot open the object, it
will return a non-object (null string) value.
The
OpenId command is polymorphic. If there are
multiple types of objects within an extent (subclasses of the same persistent
object stored together), then the
OpenId command will
return an instance of the object that corresponds to the given ID value.
The
OpenId command has exactly the same behavior
as invoking the
%OpenId method in Caché ObjectScript.
For compatibility, there is also an
Open with the same
behavior as the
%Open method in Caché ObjectScript.
An object instance is automatically destroyed when there are no longer
an variables or object properties referring to it.
For example, in the following code, setting
obj to
an empty string ("") closes the object as there are no other references to
it:
person = New Sample.Person()
person = ""
You can use the properties and methods of an object using dot syntax:
objref.Property
objref.Property = value
objref.Method()
If the value preceding the dot operator is not a valid object instance,
then a runtime error will occur.
To get or set the value of a property, use the dot operator with a variable
that refers to an object:
person = OpenId Sample.Person(22)
PrintLn person.Name
person.Name = "El Vez"
person = "" ' don't save changes...
You can follow object references using cascading dot syntax:
person = OpenId Sample.Person(22)
PrintLn person.Home.City
PrintLn person.Home.State
From within an instance method, you use the special variable,
Me,
to access other properties of the same object:
Method PrintOut() [language = basic]
{
PrintLn Me.Name
}
To invoke an instance method, simply use the dot operator with a variable
that refers to an object:
person = New Sample.Person()
PrintLn person.NinetyNine()
From within an instance method, you use the special variable,
Me,
to invoke other instance methods of the same object:
Method Test() [language = basic]
{
Me.Test2()
}
To call a class method (a method that can be invoked without having
an object instance) use the following syntax:
"MyApp.MyClass".MyMethod(22)
The name of the class (and optional package name) is placed within ""
and followed by the dot operator and the method.
The
With statement allows you to perform
a series of statements on a specified object instance without requalifying
the name of the object variable. For example:
person = OpenId Sample.Person(22)
With person
PrintLn .Name
PrintLn .SSN
PrintLn .Home.City
End With