I have two entities Foo and Bar with a Many to Many relationship between them.
我有两个实体Foo和Bar,它们之间有多对多的关系。
Let's say there is no semantic argument for why Foo might be "responsible" for the many to many relationship, but we arbitrarily decide that Foo is responsible for the relation (I.e., in NHibernate we would mark Bar as Inverse)
假设没有语义论证为什么Foo可能对多对多关系“负责”,但我们任意决定Foo负责关系(即,在NHibernate中我们将Bar标记为Inverse)
That's all well and good from a DB perspective, but my entity APIs reveal a problem.
从数据库的角度来看,这一切都很好,但我的实体API揭示了一个问题。
// Responsible for the relation
public class Foo
{
List<Bar> Bars = new List<Bar>();
public void AddBar(Bar bar)
{
Bars.Add(bar);
bar.AddFoo(this);
}
}
public class Bar
{
List<Foo> Foos = new List<Foo>();
// This shouldn't exist.
public void AddFoo(Foo foo)
{
Foos.Add(foo);
foo.AddBar(this); // Inf Recursion
}
}
If we've decided that Foo is responsible for this relationship, how do I update the associated collection in Bar without creating a public Bar.AddFoo() method which shouldn't even exist?
如果我们已经确定Foo负责这种关系,那么如何更新Bar中的关联集合而不创建一个甚至不存在的公共Bar.AddFoo()方法?
I feel like I should be able to maintain the integrity of my domain model without resorting to having to reload these entities from the DB after an operation such as this.
我觉得我应该能够保持域模型的完整性,而不必在这样的操作之后从数据库重新加载这些实体。
UPDATE: Code tweak inspired by commenter.
更新:由评论者启发的代码调整。
4 个解决方案
#1
1
You said that one side will "own" the relationship. Make this method public. The other associations (or add methods) can be made internal to avoid consumers from interacting with it directly.
你说过一方会“拥有”这段关系。公开此方法。其他关联(或添加方法)可以在内部进行,以避免消费者直接与之交互。
public class Foo
{
private IList<Bar> Bars {get;set;}
public void AddBar(Bar bar)
{
Bars.Add(bar);
bar.Foos.Add(this);
}
}
public class Bar
{
internal IList<Foo> Foos {get;set;}
}
#2
3
You might be missing a domain concept there. Have you tried creating a third entity: FooBarRelationship?
您可能在那里错过了一个域概念。您是否尝试过创建第三个实体:FooBarRelationship?
#3
2
See Working bi-directional links in the Hibernate documentation.
请参阅Hibernate文档中的双向链接。
Many developers program defensively and create link management methods to correctly set both sides, e.g. in Person:
许多开发人员采取防御性程序并创建链接管理方法以正确设置双方,例如亲自:
protected Set getEvents() {
return events;
}
protected void setEvents(Set events) {
this.events = events;
}
public void addToEvent(Event event) {
this.getEvents().add(event);
event.getParticipants().add(this);
}
public void removeFromEvent(Event event) {
this.getEvents().remove(event);
event.getParticipants().remove(this);
}
I personally think Entity object holding the list of related object is being too smart, and you should let the DAL hit the database.
我个人认为持有相关对象列表的Entity对象太聪明了,你应该让DAL命中数据库。
DALFactory.FooAdapter.getBars(foo);
#4
-1
you could make it static
你可以让它静止
public class Foo
{
List<Bar> Bars = new List<Bar>();
public void AddBar(Bar bar)
{
Bars.Add(bar);
Bar.AddFoo(bar,this);
}
}
public class Bar
{
List<Foo> Foos = new List<Foo>();
// This shouldn't exist.
public static void AddFoo(Bar bar, Foo foo)
{
bar.Foos.Add(foo);
//foo.AddBar(this); inf recurtion
}
}
Not really ideal but it does get the function off the object its self
不是很理想,但它确实让对象脱离了自己的功能
#1
1
You said that one side will "own" the relationship. Make this method public. The other associations (or add methods) can be made internal to avoid consumers from interacting with it directly.
你说过一方会“拥有”这段关系。公开此方法。其他关联(或添加方法)可以在内部进行,以避免消费者直接与之交互。
public class Foo
{
private IList<Bar> Bars {get;set;}
public void AddBar(Bar bar)
{
Bars.Add(bar);
bar.Foos.Add(this);
}
}
public class Bar
{
internal IList<Foo> Foos {get;set;}
}
#2
3
You might be missing a domain concept there. Have you tried creating a third entity: FooBarRelationship?
您可能在那里错过了一个域概念。您是否尝试过创建第三个实体:FooBarRelationship?
#3
2
See Working bi-directional links in the Hibernate documentation.
请参阅Hibernate文档中的双向链接。
Many developers program defensively and create link management methods to correctly set both sides, e.g. in Person:
许多开发人员采取防御性程序并创建链接管理方法以正确设置双方,例如亲自:
protected Set getEvents() {
return events;
}
protected void setEvents(Set events) {
this.events = events;
}
public void addToEvent(Event event) {
this.getEvents().add(event);
event.getParticipants().add(this);
}
public void removeFromEvent(Event event) {
this.getEvents().remove(event);
event.getParticipants().remove(this);
}
I personally think Entity object holding the list of related object is being too smart, and you should let the DAL hit the database.
我个人认为持有相关对象列表的Entity对象太聪明了,你应该让DAL命中数据库。
DALFactory.FooAdapter.getBars(foo);
#4
-1
you could make it static
你可以让它静止
public class Foo
{
List<Bar> Bars = new List<Bar>();
public void AddBar(Bar bar)
{
Bars.Add(bar);
Bar.AddFoo(bar,this);
}
}
public class Bar
{
List<Foo> Foos = new List<Foo>();
// This shouldn't exist.
public static void AddFoo(Bar bar, Foo foo)
{
bar.Foos.Add(foo);
//foo.AddBar(this); inf recurtion
}
}
Not really ideal but it does get the function off the object its self
不是很理想,但它确实让对象脱离了自己的功能