5. Defining standard resource operations
The Azure.Core
namespace provides a variety of standard lifecycle operations for resource types. These operations adhere to the requirements of the Azure REST API Guidelines.
Operation interface definition
To define standard operations for a resource type, create an instance of the ResourceOperations
interface that is tailored to your service. Here’s an example:
In this example:
ServiceTraits
is defined as the intersection of three trait model types available inAzure.Core
. Learn more about interface-level service traits here.Operations
is defined as the instantiation ofAzure.Core.ResourceOperations
with the service trait types you defined.
Note: The name
Operations
is used for convenience, but you might define multiple aliases ofResourceOperation
for a single service to apply different customizations for some operations. You might choose a more explicit name likeStandardOperations
.
Next, we’ll use this interface alias to define the standard resource operations we need.
Resource operations definition
We’ll define the standard set of CRUD (Create, Read, Update, Delete) operations typically needed for a resource type in an Azure service. We’ll do this by defining an interface called Widgets
:
Note: It’s not necessary to define your resource operations inside of an
interface
. You can also define them in a sub-namespace of your service or inside the top-level namespace of the service. However, it’s a best practice in TypeSpec to useinterface
to encapsulate the operations of a particular resource type.
The Widget
interface defines the following standard lifecycle operations:
ResourceRead<TResource>
: Defines a “read” operation for a single resource instance.ResourceCreateOrUpdate<TResource>
: Defines an “upsert” operation which either creates or updates an instance of the resource type depending on whether it already exists.ResourceDelete<TResource>
: Defines a “delete” operation to delete a specific instance of the resource.ResourceList<TResource>
: Defines an operation that lists all instances of the resource type.
Note: There are both instantaneous and long-running versions of “create”, “update”, and “delete” operations for resource types depending on what you need for a particular resource!
These operations will all exist under the route path /widgets/{widgetName}
, with the list operation generating the path /widgets
.
Error response customization
If your service needs to use a custom error response type for all resource operations (which is uncommon), you may pass in a custom error response type to the ResourceOperations
interface:
You can also reuse the standard Azure Core error types with minor customizations: