Day 4: Developing with InterSystems Objects and SQL
I'm participating in the Developing with InterSystems Objects and SQL with Joel Solon. The course is very nice and I will share with you some tips I got during the training. Tips presented in the day 4:
- All data are stored in Globals and Global names start with ^. Example of global: ^animal. Global can have multiples locations to the data ("sub data"). Example: ^animal(1).
- ^%* globals are accessible from any system-wide (from any namespace).
- Globals enables IRIS to support multimodel data (object, relational, document, multidimensional, etc).
- To see globals go the Management Portal > Explorer > Globals > Select Global > View or in Terminal type do ^%G or zwrite ^global.
- There are an automatic correspondence between persistent classes and SQL Tables:
- Package is SQL Schema;
- Class is a Table;
- Property is a Column;
- Method is a Store Procedure; (when sqlProc);
- Relationship between classes is a SQL Foreign Key constraint (must be bi-directional);
- Object is a Row.
- One table can correspond to multiple classes, but serial class is part of the table of the persistent class (don't have a specific table).
- One class can correspond to multiple tables.
- We have some classes types:
- Non-registered: not class object (container for methods only);
- Registered: transient objects;
- Persistent: SQL persistence in tables;
- Serial: SQL persistence in the main table (serial is embedded);
- Datatype: not class object is used to do new validations and conversions to base data types.
- Classes can be composed by:
- Properties;
- Methods;
- Class queries: SQL Select statements;
- Parameters: user constants or system constants to configure the class behavior;
- Foreign keys: to referencial integrity;
- Indexes: to improve performance and do unique values;
- Triggers: fire methods associated with persistence events;
- XData: XML or JSON definitions associated with the class;
- Storage: description of the data storage.
- Classes by convention has first letter of the word in Capital. Example: CountryOrigin. Parameters are all capital. Example: COLORNUMBER.
- Class attributes qualify/configure a class. Example [SqlTableName = Animal] set the table name to a class. [Final] not allows inheritence. [Private] not allows call methods or use properties for non-subclasses.
- Internally IRIS generate Get and Set to properties, and are not visible but can be declared to change the behavior.
- Is possible override a method of the superclass, to do this, repeat the class name, the arguments. Is possible increase the number of arguments, not decrease.
- Use ##super() to call base class method.
- To create abstract class use [Abstract] and prevent instantiation.
- Is possible extends multiple classes. Example Class Person extends (%Persistent, %Animal). (Persistent must be the fisrt in the extends, see joel tip in the comments)
- REST is REpresentational State Transfer. Is based in the HTTP protocol. Use HTTP verbs: GET (select), POST (insert), PUT (update) and DELETE (delete).
- For expose your class as REST resource extend from %CSP.REST.
- Use URLMap inside XData block to configure the routes of your REST service. Using the Portal, create a Web Application, enable REST and specify Dispatch class.
- %JSONAdaptor provides convertion between objects and JSON. Use obj.%JSONImport(jsonObj) to assign DynamicObject to a object. Use obj.%JSONExportToString(.jsonString) to write a JSON String to a object.
- %JSON.Formatter format a JSON String for human readability.