问题标签 [invariants]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
1320 浏览

domain-driven-design - 聚合不变量是否可以包含基于来自其他地方的信息的规则?

在 DDD 中,聚合不变量是否可以包含基于另一个聚合中信息的规则?现在我不这么认为,但是这给我带来了一个问题,我不知道如何解决它。

我有一个名为 Asset(设备)的实体,我将其建模为聚合的根。它有一个标签(属性)列表,用于描述制造商、模型等内容。它存储称为 AssetType 的第二个聚合的标识,该聚合具有一个 TagType 列表,其中一些可以标记为强制性的。

现在在我看来,Asset 的不变条件之一应该引用关联的 AssetType 以在强制标签列表中强制执行非空值。但是我的胆量正在爬上我将如何强制执行一致性的想法。

这是否意味着聚合真的应该包含所有四个实体?如果根是 AssetType 并且它下面有一个 Assets 列表,它可以解决我的问题,但是这不太适合具有其他聚合维护不同类型 Asset 列表的核心用例。资产确实必须是根,否则我会遇到问题。

而且 AssetType 也不能很好地进入 Asset 聚合。这似乎同样荒谬。

我的胆量仍然说 Asset 和 AssetType 是两个独立的聚合,但我该如何解决一致性问题?还是我弄错了不变量?

0 投票
5 回答
1324 浏览

java - Imposing constraints or restrictions on method body, in Java

Context (Edit)

Some clarification was on demand, so I'll try to sum up what influences the question.

  • The goal of the project is to provide a certain functionality to programmers, most probably in the form of a library (a JAR with class files, I guess).

  • To use said functionality, programmers would have to conform to the constraints that must (should) be satisfied. Otherwise it won't function as expected (just like the locks from java.util.concurrent, that must be acquired / freed at the appropriate time and place).

  • This code won't be the entry point to the applications using it (ie, has no main).

  • There's a limited (and small) amount of operations exposed in the API.

Examples:

  1. Think of a small game, where almost everything is implemented and managed by the already implemented classes. The only thing left for the programmer to do, is to write a method, or a couple of them, that describe what the character will do (walk, change direction, stop, inspect object). I would want to make sure that their methods (possibly marked with an annotation?) just walk, or changeDirection, or calculate diff = desiredValue - x, and not, say, write to some file, or open a socket connection.

  2. Think of a transaction manager. The manager would be provided by this library, as well as some constant attributes of transactions (their isolation level, time-outs, ...). Now, the programmers would like to have transactions and use this manager. I would want to make sure that they only read, write, commit, or rollback on some resources, known to the manager. I wouldn't want them to launchRocket in the middle of the transaction, if the manager does not control any rockets to launch.

The Problem

I want to impose some invariants / restrictions / constraints on the body of a method (or group of methods), to be later implemented by some other programmer, in some other package/location. Say, I give them something along the lines of:

It is important (probably imperative), for the purposes of this project, that the method body satisfies some invariants. Or rather, it is imperative that the set of commands this method's implementation uses is limited. Examples of these constraints:

  • This method must not perform any I/O.
  • This method must not instantiate any unknown (potentially dangerous) objects.
  • ...

Put another way:

  • This method can call the methods of a known (specific) class.
  • This method can execute some basic instructions (maths, assign local variables, ifs, loops...).

I've been looking through Annotations, and there seems to be nothing close to this.
My options so far:

  1. Define some annotation, @SafeAnnotation, and apply it to the method, defining a contract with the implementer, that he will follow the rules imposed, or else the system will malfunction.

  2. Define an Enum with the allowed operations. Instead of exposing the allowed methods, only a method is exposed, that accepts a list of these enum objects (or something similar to a Control Flow Graph?) and executes it, giving me the control of what can be done.

Example:

My Question

Is there any feature in the language, such as annotations, reflection, or otherwise, allowing me to inspect (either at compile time or runtime) if a method is valid (ie, satisfies my constraints)?
Or rather, is there any way to enforce it to call only a limited set of other methods?

If not (and I think not), would this second approach be a suitable alternative?
Suitable, as in intuitive, well designed and/or good practice.

Update (Progress)

Having had a look at some related questions, I'm also considering (as a third option, maybe) following the steps given in the accepted answer of this question. Although, this may require some rethinking on the architecture.

The whole idea of using annotations to impose restrictions seems to require implementing my own annotation processor. If this is true, I might as well consider a small domain-specific language, so that the programmer would use these limited operations, later translating the code to Java. This way, I would also have control over what is specified.

0 投票
1 回答
58 浏览

c# - 有人可以解释为什么这个操作无效吗?

我今天正在阅读协变和逆变,我遇到了一篇关于堆栈交换的帖子,其中 Jon Skeet 在课堂上解释了不变性。他举了一个水果的例子,以及为什么在那个级别允许协方差是一件坏事:

那么,这如何解释有一个 Fruit 列表,您将添加从 Fruit 继承的类的实例?例如:

我过去做过这个,它总是按预期运行。为什么 CLR 不查看fruitBowl 的类型?是不是因为您首先将fruitBowl 的值设置为香蕉列表,该列表与水果列表协变,然后尝试将 Apple 添加到类型为真的集合中List<Banana>吗?

感谢下面的马特。记住您正在处理引用类型会有所帮助。永远对此投反对票。

0 投票
1 回答
621 浏览

domain-driven-design - 类不变量如何加强前置条件和后置条件?

关联

您可以将类不变量视为健康标准,操作之间的所有对象都必须满足该标准。作为类的每个公共操作的先决条件,因此可以假设类不变量成立。此外,它可以假定为类不变量所具有的每个公共操作的后置条件。从这个意义上说,类不变量作为类中公共操作的前置条件和后置条件的一般强化。有效的前提条件是与类不变量一起制定的前提条件。类似地,有效的后置条件是与类不变量一起制定的后置条件。

1.如果类上定义了类不变量Server

a)前提条件通常是根据被调用操作的形式参数制定的,那么类不变量如何加强方法的 ( Foo')前提条件

b)后置条件是根据被调用方法的返回值制定的,那么类不变量如何加强方法的Foo置条件呢?

2.在类上定义的类不变量能否Caller以任何方式加强Foo前置条件后置条件

3.如果在's参数上定义了类不变量:FoocmdIn

a) 如果在 range 内的状态的前提条件,但是在应该在 range 内的状态上定义的类不变量之一,那么前提条件确实得到了加强?FoocmdIn.Length1-20InputInput.Length2-19Foo

b) a ) 中的逻辑不是有点缺陷吗,因为如果类不变量已经声明Input.Length应该在范围内2-19,那么Foo定义一个并不总是存在的前提条件true不是错误(cmdIn.Length不能保存值120

c) 但是如果在应该在 range 内的状态上定义类不变量,那么' 的前提条件不是加强吗?InputInput.Length0-100Foo

d)定义的类不变量cmdIn也能以某种方式加强后置条件吗?Foo

4.如果在返回值上定义了类不变量Foo

a)如果在 range 内的状态的后置条件,但是在应该在 range 内的状态上定义的类不变量之一,那么的后置条件确实是加强的?FoocmdOut.Length1-20OutputOutput.Length2-19Foo

b)但是如果在应该在 range 内的Output状态上定义不变量,那么' 的后置条件不是加强的吗?Output.Length0-100Foo

c)定义的类不变量Output也能以某种方式加强Foo前提条件吗?

( _ _ _ _ _ Foo_还是强化Foo前置条件后置条件?如果这就是文章的实际含义,那怎么可能?

谢谢

0 投票
1 回答
456 浏览

java - 代码中的类不变量

我想知道是否有确定特定类的不变量的确定程序,还是会根据具体情况进行更改?

显然它取决于某些变量并需要语句,但我的问题是,

我可以遵循某种指南来确定特定类的最佳不变量吗?

谢谢

0 投票
3 回答
4850 浏览

java - 如何找到循环不变的java

我试图找到循环的不变量(例如在下面的代码中)我真的不知道如何找到一般的不变量。任何人都可以帮助我如何找到一个不变量,并帮助我找到它的以下代码?谢谢

0 投票
2 回答
2461 浏览

java - 为两个嵌套的while循环查找循环不变量java

我对不变量有点熟悉,我或多或少可以在一个小循环中找到它。在为以下 java 伪代码求解不变量时,我感到很困惑。谁能帮忙:

0 投票
1 回答
390 浏览

java - 如何在 Java 中强制执行自定义类实例不变量

Java 中强制执行类实例不变量的最佳方法是什么(即确保某些语句在调用任何公共方法之前和之后都是正确的)?

我将在下面包含一个示例(让我对这个问题感到疑惑的示例),但实际上我对强加类实例不变量的通用解决方案更感兴趣。

假设我有以下课程:

并且我想对 Edge 类强制执行以下语句:

  • 0 <= minCardinality <= maxCardinality
  • if (constraint != null) then (minCardinality <= constraint.getCardinality() <= maxCardinality)

此处 Edge 实例由具有(强制性)属性 minCardinality 和 maxCardinality 集的解析器创建。(可选)约束属性留空。然后将实例移交给另一个模块(测试帧生成器),该模块可能会或可能不会通过 setter 方法设置约束。

0 投票
2 回答
1153 浏览

java - Java 不变异常

我有一个项目,其中某些数据具有不能使用语言结构强制执行的不变量,但我打算编写我的代码以便维护它们。如果它们由于某种原因被破坏,则意味着我的代码有问题。我有一些健全性检查代码可以发现这些不变量是否已被破坏,但我不确定响应这种情况的规范 Java 方法是什么——我应该抛出一些标准异常吗?使用断言?

请注意,这是某个值设置不正确的问题。错误本身不是在健全性检查时发生的,而是在过去发生的,健全性检查现在才发现它(即,健全性检查本身正在运行并不坏,只是糟糕的是检查失败)。

谢谢!

0 投票
2 回答
4551 浏览

domain-driven-design - DDD - 使用小聚合根执行不变量

我第一次尝试 DDD,但我遇到了聚合设计的问题。

我的应用程序包含 3 个实体;图、节点、链接。这些实体中的每一个都有一个可以由用户修改的名称属性(我认为这使得“名称”不适合作为实体 ID)。图表包含节点的集合,节点具有传出链接的集合(出于此问题的目的,忽略传入链接是安全的)。每个节点一次只能与一个图关联(但可以在图之间移动),同样,每个链接在任何给定时间只能与一个节点关联(但可以移动)。

我试图强制执行的不变量是所有实体名称在其父集合中都是唯一的。使用上述架构,不变量在实际集合上,所以我决定集合所有者(图和节点)都应该是聚合根。

我遇到的问题是我现在如何在 Node 上强制执行名称不变?在 Link 上很容易,因为它隐藏在 Node AR 中,因此 Node 可以确认所有 Link 重命名/移动都不会破坏这个不变量。但据我所知,没有什么可以阻止直接重命名 Node 可能会破坏不变量。最终一致性在这里不是可接受的选择,这必须是真正的系统不变量。

我正在考虑的方法是让 Node.Rename() 实际上强制执行不变量,但我担心这涉及查看其父 Graph 以检查重命名是否有效。这“感觉”不太对 - 感觉 Graph 应该是强制执行此命名空间约束的那个,而 Node 应该对此一无所知。

我希望这是有道理的,我期待听到人们的想法。

编辑:上面介绍的域模型是整个域的简化子集。太复杂了,所有实体都无法在单个 AR 中保存......