Azure / Azure/azure-functions-durable-python

Proposal: Support Class-Based Syntax for Python Entities

未关闭
#278 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
Enhancement fixed-in-v2
主要语言
Python
星标
157
派生
70
平均合并
2 天 10 小时
30 天内合并 PR
2

描述

Currently, Entities in Python (and JavaScript) only support function-based syntax, where operations are delineated by a cascade of if-statements. In C#, a class-based syntax is supported, which allows individual operations to be split into methods belonging to a larger class that represents the Entity itself.

Implementing this alternative syntax should not be too difficult, it's mostly just a layer of indirection. Making matters simpler, we already have a community-driven example of how to do this for TypeScript here: https://scale-tone.github.io/2021/03/15/durable-mvc .

As an example for the syntax, consider the following entity function using the current implementation syntax

```Python
def entity_function(context: df.DurableEntityContext):
current_value = context.get_state(lambda: 0)
operation = context.operation_name
if operation == "add":
amount = context.get_input()
current_value += amount
elif operation == "reset":
current_value = 0
elif operation == "get":
pass

context.set_state(current_value)
context.set_result(current_value)
```

It's class-based syntax alternative would look as follows:

```Python
class Counter:
def __init__(self): # this corresponds to state initializer, or the `lambda: 0` in the function-based syntax
self.entity_state= 0

def add(self, amount):
self.entity_state+= amount
return self.entity_state
def get(self):
return self.entity_state
def reset(self):
return self.entity_state= 0
```

Finally, it makes sense to me that the `return` statement would set the value for `context.set_result`. A more difficult question is to know what value to save for `set_state`. One easy solution is to require the state to be under the name `self.entity_state` but perhaps that's too restrictive. What do others think?

贡献指南

打开贡献指南

调研方向

该 issue 没有指定任何文件或测试。首先检查 DurableEntityContext 周围现有的基于函数的实体处理方式以及链接的 TypeScript 示例,然后确定类方法、初始化、返回的结果和持久化状态应如何映射到当前实体模型。完成的前提是在实现之前就语法和状态处理设计达成一致。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
backend
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
需要澄清
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。