Skip to content

Commit 0fcb932

Browse files
Merge pull request #313351 from hhunter-ms/hh-266427
[Durable][UUF] Add example for deleting entities
2 parents a0ca48a + e91ea0d commit 0fcb932

1 file changed

Lines changed: 58 additions & 6 deletions

File tree

articles/azure-functions/durable/durable-functions-dotnet-entities.md

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,65 @@ public class Counter : TaskEntity<int>
151151
152152
### Deleting entities in the isolated model
153153

154-
Deleting an entity in the isolated model is accomplished by setting the entity state to `null`, and this process depends on the entity implementation path used:
154+
Deleting an entity in the isolated model is accomplished by setting the entity state to `null`, and this process depends on the entity implementation path used.
155+
156+
#### Delete with ITaskEntity or function-based syntax
157+
158+
When deriving from `ITaskEntity` or using [function based syntax](#function-based-syntax), delete is accomplished by calling `TaskEntityOperation.State.SetState(null)`:
159+
160+
```csharp
161+
// Inside a function-based entity dispatch
162+
switch (operation.Name.ToLowerInvariant())
163+
{
164+
case "delete":
165+
operation.State.SetState(null);
166+
break;
167+
}
168+
```
169+
170+
#### Delete with TaskEntity\<TState\>
171+
172+
When deriving from `TaskEntity<TState>`, a delete operation is implicitly defined. However, it can be overridden by defining a method `Delete` on the entity. State can also be deleted from any operation via `this.State = null`.
173+
174+
- To delete by setting state to `null` requires `TState` to be nullable.
175+
- The implicitly defined delete operation deletes non-nullable `TState`.
176+
177+
The following example shows a `TaskEntity<int?>` with nullable state that overrides the default delete:
178+
179+
```csharp
180+
public class Counter : TaskEntity<int?>
181+
{
182+
public void Delete()
183+
{
184+
// Custom logic before deleting, such as logging
185+
this.State = null;
186+
}
187+
188+
[Function(nameof(Counter))]
189+
public static Task Run([EntityTrigger] TaskEntityDispatcher dispatcher)
190+
=> dispatcher.DispatchAsync<Counter>();
191+
}
192+
```
193+
194+
#### Delete with a POCO entity
195+
196+
When using a POCO as your state (not deriving from `TaskEntity<TState>`), a delete operation is implicitly defined. It's possible to override the delete operation by defining a method `Delete` on the POCO. However, there isn't a way to set state to `null` in the POCO route, so the implicitly defined delete operation is the only true delete.
197+
198+
```csharp
199+
public class Counter
200+
{
201+
public int Value { get; set; }
202+
203+
// The implicit delete operation handles state removal.
204+
// Defining a Delete method here overrides the implicit behavior,
205+
// but you cannot set state to null from within a POCO entity.
206+
207+
[Function(nameof(Counter))]
208+
public static Task Run([EntityTrigger] TaskEntityDispatcher dispatcher)
209+
=> dispatcher.DispatchAsync<Counter>();
210+
}
211+
```
155212

156-
- When deriving from `ITaskEntity` or using [function based syntax](#function-based-syntax), delete is accomplished by calling `TaskEntityOperation.State.SetState(null)`.
157-
- When deriving from `TaskEntity<TState>`, delete is implicitly defined. However, it can be overridden by defining a method `Delete` on the entity. State can also be deleted from any operation via `this.State = null`.
158-
- To delete by setting state to null requires `TState` to be nullable.
159-
- The implicitly defined delete operation deletes non-nullable `TState`.
160-
- When using a POCO as your state (not deriving from `TaskEntity<TState>`), delete is implicitly defined. It's possible to override the delete operation by defining a method `Delete` on the POCO. However, there isn't a way to set state to `null` in the POCO route, so the implicitly defined delete operation is the only true delete.
161213
::: zone-end
162214

163215
::: zone pivot="in-proc"

0 commit comments

Comments
 (0)