我应该如何建模类之间的关联

发布于 2024-08-24 07:30:18 字数 804 浏览 7 评论 0原文

我正在我的应用程序中对域模型进行建模。显然,一些对象关系是关联,但这些关联也具有属性。例如:

Foo 可以有一对多的 Bar。但是,关联具有属性,例如关联有效时间的日期范围。因此,我这样做的方式如下:

public interface Association<S, T> {
     public S getSource();

     public T getTarget();
}

然后对于类似上面的内容:

public class FooToBarAssociation implements Association<Foo, Bar> {
    public Foo getSource();
    public Bar getTarget();
    public Date getFromDate();
    public Date getToDate();
}

然后 Foo 类具有:

private List<FooToBarAssociation> associations;

我的问题是:

  1. 这是对与属性的关联进行建模的适当方法吗?

  2. 添加/删除与 Foo 的关联的业务逻辑应该在哪里?创建 FooToBarAssociation 有时需要一些业务逻辑,我想知道是否应该在 FooService 中处理,然后调用 setAssociations 而不是在模型对象中。我一直听说要尽可能地将业务逻辑排除在模型对象之外。

I am modelling the domain model in my application. Obviously, some of the object relationships are assocations, but those associations also have attributes. For example:

Foo can have one-to-many Bars. However, the association has attributes, such as a date range for how long that association is valid. So, the way I have done this is as follows:

public interface Association<S, T> {
     public S getSource();

     public T getTarget();
}

Then for something like the above:

public class FooToBarAssociation implements Association<Foo, Bar> {
    public Foo getSource();
    public Bar getTarget();
    public Date getFromDate();
    public Date getToDate();
}

Then the Foo class has:

private List<FooToBarAssociation> associations;

My questions are:

  1. Is this an appropriate way to model the association with attributes?

  2. Where should the business logic be for adding/removing the associations to Foo? Creating a FooToBarAssociation will sometimes require a bit of business logic and I'm wondering if that should be handled in a FooService, which then calls setAssociations rather than being in the model object. I've always heard to keep biz logic out of the model objects as much as possible.

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

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

发布评论

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

评论(2

勿挽旧人 2024-08-31 07:30:18

恕我直言,在 UML 中实现关联类时适用相同的规则。
您创建的是一种方式,其他方式可能是嵌套 - Foo 包含 Assoc,而 Assoc 又包含 Bar。您还可以围绕包含 Assoc 的 Bar 和包含 Foo 的 Assoc 以相反的方式创建它。 Foo 可以包含 Assoc,Bar 可以包含 Assoc。有很多可能性,这仅取决于您的要求。
在您的示例中,您的解决方案看起来不错。
你听到的似乎不错,但有时可能会很复杂。

IMHO same rules apply as when you implement association classes in UML.
What you created is one way, other might be nesting - Foo contains Assoc which in turn contains Bar. You can create it also the other way around Bar containing Assoc and Assoc containing Foo. Foo can contain Assoc and Bar can contain Assoc. There are many possibilities, it depends only on you requirements.
In your example your solution looks good.
What you heard seems good, but sometimes it might be complicated.

酒与心事 2024-08-31 07:30:18

回答你的问题:

  1. 我认为你的模型是合适的。
  2. 在面向领域的模型中,我会将关联逻辑添加到 Foo.

我还有另一个建议,但它适用于特定情况。如果您需要模型一次仅处理一个关联,则可以使用以下简化模型。

public class Foo {

    private Date startDate;
    private Date endDate;
    private Bar bar;

    public Date getStartDate() {
        return startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public Bar getBar() {
        return bar;
    }


}

其中开始日期和结束日期对应于 bar 与 Foo 关联的时间段。这样,即使 Foo 到 Bar 是一对多,您一次也只能处理一个 Bar。 Foo Bar 关联将根据有效日期从数据库中获取,该日期位于开始日期和结束日期之间。

Answers to your questions:

  1. I think your model is appropriate.
  2. In a domain oriented model, I would add the association logic to Foo.

I have another suggestion, but it applies to a specific situation. If you need the model to work with only one association at a time the following simplified model would work.

public class Foo {

    private Date startDate;
    private Date endDate;
    private Bar bar;

    public Date getStartDate() {
        return startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public Bar getBar() {
        return bar;
    }


}

where the start and end dates correspond to the period for which bar is associated with Foo. This way, even though Foo to Bar is one to many, you would only work on one Bar at a time. The Foo Bar association would be fetched from the database, based on an effective date, which would lie between start and end dates.

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