Skip to Content
使用 Python 的架构模式
book

使用 Python 的架构模式

by Harry Percival, Bob Gregory
May 2025
Intermediate to advanced
304 pages
3h 58m
Chinese
O'Reilly Media, Inc.
Content preview from 使用 Python 的架构模式

附录 E。 验证

每当我们在上讲授和讨论这些技术时,都会反复提到一个问题:"我应该在哪里进行验证?这属于领域模型中的业务逻辑,还是属于基础架构问题?

与任何建筑问题一样,答案是:视情况而定!

最重要的考虑因素是,我们要将代码很好地分离,使系统的每个部分都简单明了。我们不想在代码中加入无关紧要的细节。

验证到底是什么?

当人们使用 "验证"一词时,通常是指对操作的输入进行测试以确保其符合某些标准的过程。 符合标准的输入被认为是有效的,而不符合标准的输入则是无效的

如果输入无效,操作就不能继续,而应该带着某种错误退出。换句话说,验证就是创建先决条件。我们发现,将前提条件分为语法、语义和语用三个子类型是非常有用的。

验证语法

在语言学中,一种语言的句法是一套规范语法句子结构的规则。例如,在英语中,句子 "Allocate three units ofTASTELESS-LAMP to order twenty-seven "在语法上是正确的,而短语 "hat hat hat hat hat hat wibble "则不正确。我们可以把语法正确的句子描述为结构良好的句子。

这如何映射到我们的应用程序中?下面是一些语法规则的示例:

  • Allocate 命令必须有订单 ID、SKU 和数量。

  • 量是一个正整数。

  • SKU 是一个字符串。

这些是关于传入数据的形状和结构的规则。没有 SKU 或订单 ID 的Allocate命令不是有效信息。它相当于 "分配三个给"。

我们倾向于在系统边缘验证这些规则。我们的经验法则是,消息处理程序应始终只接收格式良好且包含所有必要信息的消息。

一种方法是将验证逻辑放在信息类型本身:

验证消息类(src/allocation/commands.py)

from schema import And, Schema, Use


@dataclass
class Allocate(Command):

    _schema = Schema({  1
        'orderid': int,
         sku: str,
         qty: And(Use(int), lambda n: n > 0)
     }, ignore_extra_keys=True)

    orderid: str
    sku: str
    qty: int

    @classmethod
    def from_json(cls, data):  2
       data = json.loads(data)
       return cls(**_schema.validate(data))
1

schema可以让我们以一种很好的声明方式来描述信息的结构和验证。

from_json 方法会读取一个 JSON 字符串,并将其转化为我们的消息类型。

不过,由于我们需要两次指定字段,这可能会造成重复,因此我们可能需要引入一个辅助库来统一验证和声明我们的消息类型: ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

实用 Python 数据整理与数据质量

实用 Python 数据整理与数据质量

Susan E. McGregor
ppk on JavaScript

ppk on JavaScript

Peter-Paul Koch

Publisher Resources

ISBN: 9798341657212