4. Defining your first resource
In the context of your service, a “resource” is a fundamental entity that your service manages. For our WidgetService, the most basic entity we need is a Widget.
To create a Widget, we need to define a model and annotate it with the @resource decorator.
Code implementation
Section titled “Code implementation”After the top-level namespace declaration, add the following lines:
/** A widget */@resource("widgets")model Widget { /** The widget name */ @key("widgetName") @visibility(Lifecycle.Read) name: string;
/** The ID of the widget's manufacturer */ manufacturerId: string;}Code explanation
Section titled “Code explanation”Here are some important points about the code:
- The
Widgetmodel has a@resourcedecorator with a parameter of"widgets". This string is the “collection name” of the resource and it determines where the resource appears in the service URI layout. - The
nameproperty has a@keydecorator with a parameter of"widgetName". This string customizes the name of the path parameter (and the parameter name itself) in operations that use this resource type. Note that there must be one property with a@keydecorator on all resource types! - The
@visibility(Lifecycle.Read)decorator on thenameproperty indicates that thenameproperty should only appear in operation responses, not in operations that allow you to change properties of theWidgetresource. - We use
@docdecorators on the model type and all properties for description. Documentation strings are enforced by linting rule when authoring specs withAzure.Core!
Now that we have a resource type, the next step is to define operations for this resource.