在我的域中对真/假属性进行建模的正确方法是什么?

发布于 2024-12-01 00:11:05 字数 383 浏览 0 评论 0原文

想象一下,我有一个带有实体的域,该实体的属性之一是真/假属性,例如活动/非活动

对此进行建模的正确方法是什么?

  1. 使用布尔值? False = 不活动True = 活动
  2. 使用枚举:

    公共枚举状态 { 活跃 = 1, 不活动 = 2 }

使用 Enum 是正确的方法,但使用 Boolean 属性我也认为没有错误。

DDD的各位对此有何看法?

Imagine I have a domain with a Entity and one of properties of this Entity is a true/false property like a Active/Inactive.

What's the correct way to model this?

  1. Using a boolean? False = Inactive and True = Active ?
  2. Using a Enum:

    public enum Status
    {
    Active = 1,
    Inactive = 2
    }

I think using a Enum is the correct way, but using a Boolean property I also see is not wrong.

What you guys of DDD think about this?

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

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

发布评论

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

评论(3

鲜肉鲜肉永远不皱 2024-12-08 00:11:05

如果它是真/假对,那么布尔值就可以了,恕我直言。没必要把事情搞得太复杂。

如果该值用作方法参数,则枚举甚至类可能会更好地提高可读性。但话又说回来,您可能会考虑调用两种不同的方法。

枚举的可能问题是可能会使用无效值。这可以通过使用这样的类来缓解:

public class ActivityState {
    public static ActivityState Active = new ActivityState();
    public static ActivityState Inactive = new ActivityState();
}

...

// in your entity:

public ActivityState IsActive { get; set; }

this.IsActive = ActivityState.Active; // or
this.IsActive = ActivityState.Inactive;

它可以像枚举一样使用,但确保不允许使用无效值。

If it's a true/false pair, then boolean is just fine IMHO. No need to overcomplicate things.

If the value is being used as a method parameter, an enum or even class might be better for readability. But then again, you might consider calling two different methods instead.

Possible issue with enum is that one might be able to use invalid values. This can be mitigated by using a class like this:

public class ActivityState {
    public static ActivityState Active = new ActivityState();
    public static ActivityState Inactive = new ActivityState();
}

...

// in your entity:

public ActivityState IsActive { get; set; }

this.IsActive = ActivityState.Active; // or
this.IsActive = ActivityState.Inactive;

It can be used like an enumeration, but makes sure that no invalid values are allowed.

朕就是辣么酷 2024-12-08 00:11:05

我想说枚举只是针对这些情况,以避免将概念与原始类型或类映射。

例如,避免使用布尔值映射 2 个值逻辑,或者使用整数 1、2、3 映射更多值(如信号量的颜色:红、绿、黄)。

枚举的存在只是为了避免这种情况,并使其清晰它们各自的含义,提高易读性和可维护性,以及类型安全(您可以限制一种方法只接收您的枚举类型,而不是更广泛的布尔类型,这使您的代码更安全)。

I would say that Enums are there just for these cases, to avoid mapping concepts with primitive types or classes.

For example, avoid mapping 2 value logic with boolean, or for more values (like the colours of a semaphore: red, green, yellow) with integers 1, 2, 3.

Enums are there just to avoid that, and to make it clear what each one of them means, improving legibility and maintainability, as well as type safety (you can restrict a method to just receive your Enum type, and not the wider Boolean type, this makes your code safer).

戏蝶舞 2024-12-08 00:11:05

当需要状态机时使用枚举。
Enum 表示标志是相互排斥且密切相关的。

例如 - 如果我们在 RPG 游戏中有一个战士类别,它可以具有“活着”或“死亡”状态。这些不可能同时都是真实的。因此 - 在这里使用枚举是有意义的。

相比之下:战士可以战斗/奔跑/喝酒/说话/吃饭/睡觉。这里我们不能使用枚举,因为它们并不相互排斥(战士应该能够在挥舞剑和吃三明治的同时绕圈跑)。

Use enums when You need state machine.
Enum says that flags are mutually exclusive and closely related.

E.g. - if we have a Warrior class in RPG game, it can have states "Alive" or "Dead". Those can't be truthful both simultaneously. Therefore - it makes sense to use enums here.

In contrast: Warrior can be fighting/running/drinking/talking/eating/sleeping. Here we can't use enum because those aren't mutually exclusive (Warrior should be able to run in circles while swinging his sword and eating sandwich).

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