Architecture:MediaWiki/data object pattern

From mediawiki.org


The data object pattern is often used when modelling domain entities, across all layers. Data objects are generally simple wrappers around a data structure that represents the properties of the object in question.

Data objects are ideally lightweight, serializable, and newable. Data objects may however also be mutable or perform lazy initialization by relying on persistence layer service objects. Objects that write themselves into the database ("active records") still fit the definition, though this approach is not recommended.

Instantiation:
Some kinds of data objects, in particular immutable values and exceptions, are typically newable - that is, they can be constructed directly. Other kinds, particularly ones that support lazy initialization or otherwise interact with the storage layer, should use dependency injection, and should be obtained from some kind of factory.

Subtypes:

  • A record is a data object that is defined in the persistence layer and can be retrieved using a unique identifier.
  • An immutable value is a data object that cannot be modified. It takes all information as constructor arguments, and provides getters to access this information. It does not perform I/O operations, and has no access to service objects.
  • An exception is a data object that extends the pre-defined Extension class. Exceptions should be immutable value objects.
  • An active record is a data object that implements its own persistence logic, it can load itself from storage, and update itself in storage. This pattern should generally be avoided, since it typically relies on global state.
  • A content objects represents user editable content from the content representation domain.

Layer constraint:
Data objects can be useful in all layers.

Status:
As of June 2020, data objects are used throughout the MediaWiki codebase. However, they are often not entirely independent of global state, and some prominent examples like the Title and User objects are extremely heavy and belong to the active record subtype.

Examples:
An example of the data object pattern is the includes/Revision/RevisionRecord.php class.