Larman 的系统操作合约 - CRUD 示例

发布于 2024-10-17 14:58:28 字数 607 浏览 8 评论 0原文

我对在类似 CRUD 的操作上应用 Larman 的系统操作契约(《应用 UML 和模式》一书中的 OO 分析)感到有些困惑。更准确地说,我对后置条件部分感到困惑。

例如,如果我的 CRUD 系统操作如下所示:

createEmployee(employee:Employee), 
readEmployee(employeeId:int), 
updateEmployee(employee:Employee), 
deleteEmployee(employeeId:int)

后置条件是什么,例如,readEmployee 系统操作,或诸如 searchEmployees 等其他操作?

例如:对于读操作,系统需要从数据库读取记录,实例化域对象,在域对象上设置属性值(也设置关系),仅此而已。这是否意味着上面提到了后置条件 - 实例创建、属性更改等。或者,读操作没有任何后置条件。这些对我来说听起来都不合逻辑。

我的困惑在于领域模型(状态)和数据库(状态)之间的关系。我只是不明白上述操作会对域模型产生影响。我一直认为数据库是保存系统状态的地方。创建员工后,其对象的状态将保留在数据库中......但是域模型状态会发生什么情况?

I have some confusion with applying Larman's system operation contracts (OO Analysis from book Applying UML and Patterns) on CRUD-like operations. More precisely, I'm confused with postcondition part.

For example, if I have CRUD system operations looking as follows:

createEmployee(employee:Employee), 
readEmployee(employeeId:int), 
updateEmployee(employee:Employee), 
deleteEmployee(employeeId:int)

what would be postcondition on, for example, readEmployee system operation, or some other operation like searchEmployees etc?

For example: for read operation, system needs to read record from database, instantiate domain object, set attribute values on domain object (set relations also) and that's it. Does it means that postconditions are above mentioned - instance creation, changes on attributes, etc. Or, read operation does not have any postcondition. None of this does sound logical to me.

My confusion is about relation between domain model (state) and database (state). I just don't get implications which above operations will have on domain model. I always think in way that the database is a place that preserves the state of the system. After I create employee, its object's state will be persisted in database... But what happens with domain model state?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

把昨日还给我 2024-10-24 14:58:28

后置条件定义了操作后应用程序(或对象,取决于抽象级别)应处于什么状态才能被视为成功。例如,对于 readEmployee 操作,后置条件是:

  • 创建一个新的 Employee 实例。
  • Employee 实例包含与数据库值匹配的属性。
  • 数据库连接已关闭。

我喜欢将“前置条件”和“后置条件”分别视为应用程序在执行操作之前和之后的“心态”。正如您可以想象的那样,当您进行 DbC 时,这更像是一个思考过程,而不是编码练习。

(如果您进行单元测试,状态会明确您的测试需要涵盖哪些内容。基本上,您最终会测试应用程序的“心态”。)

有趣的是,如果您考虑与 DbC 相反,您意识到要确定应用程序(或对象)应该公开哪些操作,只需列出它可以具有哪些状态以及如何在这些状态之间转换。进行这些转换所需采取的操作将成为您的操作,并且您不必费心实施不会导致任何所需状态的操作。因此,例如,您可能希望应用程序具有以下状态。

  • 已添加员工详细信息 (S1)
  • 已加载员工详细信息 (S2)
  • 已更新员工详细信息 (S3)
  • 已删除员工详细信息 (S4)

可以进行以下状态转换。

  • S1-> S3(添加新员工,更新详细信息)
  • S1 -> S4(添加新员工,删除员工)
  • S2-> S3(加载员工详细信息,更新员工详细信息)
  • S2-> S4(加载员工详细信息,删除员工)
  • S4 -> S1(删除员工,添加新员工)
  • S2-> S1(加载员工详细信息,添加新员工)
  • S3 -> S1(更新员工详细信息,添加新员工)
  • S3 -> S2(更新员工详细信息,加载员工详细信息)

根据上述内容,您可以以只允许有效转换的方式编写操作,其他任何操作都会导致错误。

不可能的状态转换:

  • S4→→ S2(无法删除员工,然后加载其详细信息)
  • S4 -> S3(无法删除员工,然后更新其详细信息)

状态建模可能是设计对象中最重要的部分,因此您提出了正确的问题。如果您想要有关状态建模的优质资源,请从 Sally Shlaer / Stephen Mellor 获取在状态中对世界进行建模的对象生命周期。这是一本很老的书,在 Amazon 上几乎不需要花钱,但它介绍的原理构成了现代 UML 的基础——顺便说一句,书中使用的符号看起来一点也不像 UML。

我意识到我没有触及数据库状态,但在概念层面上,数据库层只是另一个状态系统,并且适用相同的原则。

我希望这有用。

The post-condition defines what the state of your application (or object, depending on the level of abstraction) should be after the operation for it to be considered as successful. For the readEmployee operation, for example, the post-condition would be that:

  • a new Employee instance is created.
  • the Employee instance contains attributes matching the database values.
  • the database connection is closed.

I like to think of "pre-condition" and "post-condition" as the "state of mind" of your application before and after an operation has executed, respectively. As you can imagine, it's more a thought process than a coding exercise when you do DbC.

(If you do unit-testing, states make it clear what needs to be covered by your tests. Basically, you end up testing the "state of mind" of your application.)

Interestingly, if you consider the reverse of DbC, you realise that to identify what operations your application (or object) should expose, it is simply a matter of listing what states it can have and how it transitions between these states. The actions that you need to take to make these transitions then become your operations, and you do not have to bother with implementing operations that do not lead to any desired states. So, for example, you probably want the following states for your application.

  • Employee details added (S1)
  • Employee details loaded (S2)
  • Employee details updated (S3)
  • Employee details deleted (S4)

The following state transitions are possible.

  • S1 -> S3 (add new employee, update the details)
  • S1 -> S4 (add new employee, delete the employee)
  • S2 -> S3 (load employee details, update employee details)
  • S2 -> S4 (load employee details, delete employee)
  • S4 -> S1 (delete employee, add new employee)
  • S2 -> S1 (load employee details, add new employee)
  • S3 -> S1 (update employee details, add new employee)
  • S3 -> S2 (update employee details, load employee details)

Based on the above, you can write your operations in such a way that only valid transitions are allowed, with anything else giving rise to errors.

Impossible state transitions:

  • S4 -> S2 (cannot delete an employee, then load their details)
  • S4 -> S3 (cannot delete an employee, then update their details)

State modeling is probably the most important part of designing objects, so you're asking the right questions. If you want a good resource on state modeling, get Object Lifecycles Modeling the World in States from Sally Shlaer / Stephen Mellor. It is quite an old book and costs almost nothing on Amazon, but the principles it introduces form the basis of modern UML -- incidentally, the notation used in the book looks nothing like UML.

I realise I did not touch on database state, but at the conceptual level, the database layer is just another system of states and the same principles apply.

I hope this was useful.

哆啦不做梦 2024-10-24 14:58:28

我对拉曼合约的解释始终是相对于领域模型的。 Larman 明确指出后置条件只有 5 种类型:

  1. 实例创建、
  2. 实例删除
  3. 、属性值更改。
  4. 协会成立。
  5. 协会被打破。

因此,读取(或搜索)操作将没有后置条件,至少在正在读取或搜索的元素上没有。例如,如果一天内有 10,000 个用户执行读取/搜索,但从未执行任何其他操作(C、U、D),则域中的对象不会发生变化。

然而,在记住搜索/读取的领域中存在一个例外。例如,谷歌肯定会跟踪搜索。在这种情况下,执行搜索的后置条件是在其域模型中创建新对象,例如创建了一个搜索实例(实例创建)。

My interpretation of Larman's contracts is always with respect to the domain model. Larman clearly states there are only 5 types of post conditions:

  1. Instance creation
  2. Instance deletion
  3. Attribute change of value.
  4. Associations formed.
  5. Associations broken.

Therefore, a Read (or search) operation would have no post conditions, at least not on the elements that are being read or searched. For example, if 10,000 users performed reads/searches in one day, but never did any of the other operations (C, U, D), there would be no change to the objects in the domain.

There is an exception to this, however, in domains where searches/reads are remembered. For example, Google surely keeps track of searches. In this case, doing a search has the postcondition of creating a new object in their domain model, e.g., A Search instance s was created (instance creation).

落叶缤纷 2024-10-24 14:58:28

令人困惑的是在 Larman 提供的合约模板中提到数据模型关系,如下所示:

Contract CO2: enterItem
Operation: enterItem(itemID : ItemID, quantity : integer)
...
sli was associated with a ProductSpecification, based on itemID match (association formed).

数据库的详细引用属性不应在操作合约中提及。最好将其保留为:“sli 与 ProductSpecification 相关联”。

事实上,这是拉曼的运营合同中没有详细谈论的事情之一。考虑一个计算项目总数并返回总数的操作合约!好像不能写成经营合同。

The confusing comes form mentioning data model relation within the contract template that Larman provided as in :

Contract CO2: enterItem
Operation: enterItem(itemID : ItemID, quantity : integer)
...
sli was associated with a ProductSpecification, based on itemID match (association formed).

The detail referential properties of the database should not be mentioned in the operation contract. It is better to leave it as: "sli was associated with a ProductSpecification".

In fact, it is one of the things that Larman's operation contracts does not talk about in much detail. Think about a contract for an operation that calculates a total number of items and return the total ! seems that it cannot be written as an operation contract.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文