我应该如何建模类之间的关联
我正在我的应用程序中对域模型进行建模。显然,一些对象关系是关联,但这些关联也具有属性。例如:
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;
我的问题是:
这是对与属性的关联进行建模的适当方法吗?
添加/删除与 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:
Is this an appropriate way to model the association with attributes?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
恕我直言,在 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.
回答你的问题:
我还有另一个建议,但它适用于特定情况。如果您需要模型一次仅处理一个关联,则可以使用以下简化模型。
其中开始日期和结束日期对应于 bar 与 Foo 关联的时间段。这样,即使 Foo 到 Bar 是一对多,您一次也只能处理一个 Bar。 Foo Bar 关联将根据有效日期从数据库中获取,该日期位于开始日期和结束日期之间。
Answers to your questions:
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.
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.