bet9链接:六角形建筑模式

由bet9链接《вт989点сс》_bet8链接编辑,六角形架构模式用于将业务逻辑(域逻辑)与相关的基础结构代码(例如用于访问数据库或外部API 的代码)隔离开来。

在以下情况下使用六角形架构模式:

  • 您想要将应用程式架构分离,以建立可以完整测试的元件。

  • 多种类型的用户端可以使用相同的网域逻辑。

  • 您的UI 和资料库元件需要不会影响应用程式逻辑的定期技术重新整理。

  • 您的应用程式需要多个输入提供者和输出取用者,而自订应用程式逻辑会导致程式码复杂性和扩充性不足。

范本程式码

本节中的范例程式码说明如何使用Lambda 实作网域模型、将其与基础设施程式码(例如存取DynamoDB 的程式码) 分开,以及如何实作函数的单元测试。

网域模型

网域模型类别不知道外部元件或相依性,它只会实作商务逻辑。在下列范例中,类别Recipient是检查保留日期是否有重叠的网域模型类别。

class Recipient:
def init(self, recipient_id:str, email:str, first_name:str, last_name:str, age:int):
self.__recipient_id = recipient_id
self.__email = email
self.__first_name = first_name
self.__last_name = last_name
self.__age = age
self.__slots = []

@property
def recipient_id(self):
return self.__recipient_id
..... 

def are_slots_same_date(self, slot:Slot) -> bool:
for selfslot in self.__slots:
if selfslot.reservation_date == slot.reservation_date:
return True 
return False

def is_slot_counts_equal_or_over_two(self) -> bool:
.....
输入端口

RecipientInputPort类别会连线至收件者类别,并执行网域逻辑。

class RecipientInputPort(IRecipientInputPort):
def init(self, recipient_output_port: IRecipientOutputPort, slot_output_port: ISlotOutputPort):
self.__recipient_output_port = recipient_output_port
self.__slot_output_port = slot_output_port

def make_reservation(self, recipient_id:str, slot_id:str) -> Status:
status = None 

recipient = self.__recipient_output_port.get_recipient_by_id(recipient_id)
slot = self.__slot_output_port.get_slot_by_id(slot_id)
.....

# ---------------------------------------------------
# execute domain logic
# ---------------------------------------------------
ret = recipient.add_reserve_slot(slot)
.....

if ret == True:
status = Status(200, "The recipient's reservation is added.")
else:
status = Status(200, "The recipient's reservation is NOT added!")
return status
介面卡类别

DDBRecipientAdapter类别会实作DynamoDB 资料表的存取权。

class DDBRecipientAdapter(IRecipientAdapter):
def init(self):
ddb = boto3.resource('dynamodb')
self.__table = ddb.Table(table_name)

def load(self, recipient_id:str) -> Recipient:
try:
response = self.__table.get_item(
Key={'pk': pk_prefix + recipient_id})
 ... 

def save(self, recipient:Recipient) -> bool:
try:
item = {
"pk": pk_prefix + recipient.recipient_id,
"email": recipient.email,
"first_name": recipient.first_name,
"last_name": recipient.last_name,
"age": recipient.age,
"slots": []
}
...
单元测试

您可以通过注入模拟类来测试域模型类的业务逻辑。下列范例会提供网域模型Recipent类别的单元测试。

def test_add_slot_one(fixture_recipient, fixture_slot):
slot = fixture_slot
target = fixture_recipient
target.add_reserve_slot(slot)
assert slot != None
assert target != None
assert 1 == len(target.slots)
assert slot.slot_id == target.slots[0].slot_id
assert slot.reservation_date == target.slots[0].reservation_date
assert slot.location == target.slots[0].location
assert False == target.slots[0].is_vacant

def test_add_slot_two(fixture_recipient, fixture_slot, fixture_slot_2):
.....

def test_cannot_append_slot_more_than_two(fixture_recipient, fixture_slot, fixture_slot_2, fixture_slot_3):
.....

def test_cannot_append_same_date_slot(fixture_recipient, fixture_slot):
.....

  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值