领域驱动设计中关于命名和构造值对象的混淆

Confusion regarding naming and structuring Value Objects in Domain Driven Design

在阅读了 Eric Evan 关于域驱动设计的书之后,我正在尝试实现我的第一个域驱动应用程序。我对如何创建值对象有点困惑。

因此,在我的应用程序中,用户可以购买一项服务,让他们在 Youtube 上 post 的视频获得一定数量的观看次数,这是由观看这些视频的我的应用程序的其他用户实现的(基本上是许多已经可用的 youtube 推广应用程序的复制品,供学习。

假设该服务在应用程序中表示为一个名为 WatchTime 的实体。 WatchTime 实体包含一些信息,例如 MaxViews(购买的最大观看次数)和 CurrentViews(已完成的观看次数)。 MaxViews 和 CurrentViews 是值对象,两者相似。

所以问题是,我是否应该有一个共同的 'Views' 值对象,它是 MaxView 和 CurrentView 领域概念的类型,像这样:

class Views extends ValueObject<Views> {
    @override
    bool fsameValueAs(Views other) {
       // Implementation
    }


    int mvalue;
}


class WatchTime extends Entity<WatchTime> {
    @override
    bool fsameIdentityAs() {
        // Implementation
    }



    Views mmaxViews;
    Views mcurrentViews;
}

我应该为 MaxViews 和 Current Views 做一个单独的 class 因为它们是领域概念并且应该是明确的,有点像这样:

class Views {}

class MaxViews extends ValueObject<MaxViews> {
    // Other stuff

    Views mviews;
}

class CurrentViews extends ValueObject<CurrentViews> {
    //Other stuff

    Views mviews;
}

class WatchTime extends Entity<WatchTime> {
    @override
    bool fsameIdentityAs() {
        // Implementation
    }



    MaxViews mmaxViews;
    CurrentViews mcurrentViews;
}

像这样:

class MaxViews extends ValueObject<MaxViews> {
    // Other stuff

    int mviews;
}

class CurrentViews extends ValueObject<CurrentViews> {
    //Other stuff

    int mviews;
}

class WatchTime extends Entity<WatchTime> {
    @override
    bool fsameIdentityAs() {
        // Implementation
    }



    MaxViews mmaxViews;
    CurrentViews mcurrentViews;
}

我不太确定我应该怎么做。

我也不确定书中的另一件事。所以我明白,明确域的概念将使应用程序更清晰、更易于导航和阅读。所以要明确一些东西,有必要使它成为与域同名的实体或值对象class,或者变量名与域概念相同就足够了。

例如, .

class Money {}

Money mSalary;

class Salary {}

Salary mSalary;

我很确定我的建议可能看起来很愚蠢,如果你能给我更好的建议,我会很高兴。谢谢。

I'm not really sure how I should go about this.

任何一个答案都可以是最好的,这取决于您的领域、这些东西彼此之间的相似程度、它们变化的频率、它们是否出于相同的原因而变化,等等。

一种常见的情绪是duplication is cheaper than the wrong abstraction。换句话说,如果您还不确定这些实现是否应该全部派生自一个共同的基础,那么请将它们分开,并观察它。

您还应该查看 Effective Java 的第 19 条,作者是 Joshua Block。 “为继承而设计和记录,否则禁止”。

Designing a class for inheritance requires great effort and places substantial limitations on the class.... it is not uncommon to receive subclassing-related bug reports after modifying the internals of a nonfinal concrete class that was not designed and documented for inheritance.

请注意,您还可以选择拥有两个独立的 类 来实现一个或多个通用 接口 ,这有助于减少您的耦合量担心。


is is necessary to make it an entity or a value object with the same name as the domain class

没有。您可以拥有多个相同类型的数据成员是很常见的;成员的名称不必与成员类型的名称相匹配。