Decorator Metadata Update
Chris Hewell Garrett
Refresher: Why metadata?
- Dependency Injection
- ORMs (extremely useful to track database column information and size, foreign and primary key relationships, constraints, etc.)
- Runtime Type Information (often used by DI and ORM, but also for runtime type checking, validation)
Serialization/Marshalling (i.e., controlling
JSON.stringifyoutput, or WASM or native platform interop)
- Unit Testing (mark test methods on a class that serves as a test suite, specify related information such as execution contexts, issue numbers, etc.) MSTest and NUnit are good examples of this in C#/.NET
Routing (i.e., mark methods as
POST, or belonging to a specific url path, or what request body transforms to use)
- Debugging (i.e., mark classes/methods as something the debugger should step over when debugging, or what alternative representation should be used in a watch/locals window)
- Membranes (i.e., mark how certain methods, parameters, or return values should be interpreted when being passed in and out of a membrane-wrapped object)
- Reflect.metadata is the single most used decorator library (> 5 mil monthly downloads), suggests users find the pattern valuable
How it used to work
Legacy decorators used to receive the class definition as the first argument. Decorators could use the class as a key in a WeakMap to expose metadata.
- Every decorator had to define its own way of adding metadata.
- No support for adding metadata to private elements.
- No longer possible in current proposal because decorators do not receive the class.
Option 1: Shared Metadata Object (Current Proposal)
Champion's preferred approach. Decorator context receives an additional `metadata` property, which is a POJO.
- All decorators applied to the class receive the same object
- The object is assigned to the Symbol.metadata property after class definition
- Metadata object inherits from parent class's metadata
- Very simple
- Can do shared/public metadata and truly private metadata via WeakMap
- Inheritance by default, but can opt-out by manually crawling prototype chain
- Introduces a shared namespace
Option 2: Shared Key Object
Decorators receive an object on `context` that can be used as a WeakMap key for metadata
- `context.metadataKey` is a frozen object that is the same for an entire class
- Object is exposed on class via `Symbol.metadata`
- Object has a `parent` property which can be used to get the parent metadata
- Private by default
- No shared namespace
- No way to share public metadata, e.g. type information, to be used by multiple parties
- Inheritance is trickier to use
Option 3: Same as current proposal, but limit to Symbol keys
- Instead of direct access, add `getMetadata` and `setMetadata` functions to context object
- `setMetadata` sets value on the metadata object, but ensures that the key is a Symbol
- This helps to prevent collisions in the namespace by encouraging usage of Symbols by default
- Still a shared namespace, but less likelihood of accidental collisions since users must use Symbols, and can choose not to share a symbol
- Inheritance works by default
- Cannot opt-out of inheritance during decoration
Decorator Metadata Update