25.01.2015 Views

Using Caché Objects - InterSystems Documentation

Using Caché Objects - InterSystems Documentation

Using Caché Objects - InterSystems Documentation

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Referring to an Object — OREF, OID, and ID<br />

3.1.1 OID and ID Values<br />

<strong>Caché</strong> makes a further distinction between a “universal” object identifier (OID) and a “local”<br />

object identifier (ID). An OID value uniquely identifies any object stored in a database.<br />

Specifically, an OID value contains a class name along with an ID value. An ID value uniquely<br />

identifies an object belonging to a specific “extent” . An extent is a set of related objects,<br />

such as all instances of a specific class or all instances of a class and its subclasses.<br />

<strong>Caché</strong> provides two versions of methods that refer to persistent objects: one using OID and<br />

the other using ID values. For example, using <strong>Caché</strong> ObjectScript you can open an object<br />

instance using an OID value:<br />

Set object = ##class(MyApp.Person).%Open(oid)<br />

Or you can open it using an ID value:<br />

Set object = ##class(MyApp.Person).%OpenId(id)<br />

The ID versions have “Id” added to their name. In practice, applications most often use ID<br />

values; there are few circumstances in which you need to locate an object in which you do<br />

not know what extent (set of objects) an object belongs to.<br />

Note that these two forms are also available in <strong>Caché</strong> Basic:<br />

obj = Open MyApp.Person(oid)<br />

obj = OpenId MyApp.Person(id)<br />

3.1.2 OREFs and Reference Counting<br />

References to in-memory objects (OREFs) automatically manage a reference count—the<br />

number of items (variables or objects) currently referring to an object. Whenever you set a<br />

variable or object property to refer to a object, the object's reference count is automatically<br />

incremented. When a variable stops referring to an object (if it goes out of scope, is killed,<br />

or is set to a new value) the reference count is decremented. When this count goes to 0, the<br />

object is automatically destroyed (removed from memory) and its %OnClose method (if<br />

present) is called.<br />

For example, consider the following method:<br />

Method Test()<br />

{<br />

Set person = ##class(Sample.Person).%OpenId(1)<br />

}<br />

Set person = ##class(Sample.Person).%OpenId(2)<br />

<strong>Using</strong> <strong>Caché</strong> <strong>Objects</strong> 15

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!