views:

271

answers:

4

I have two entities Foo and Bar with a Many to Many relationship between them.

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)

That's all well and good from a DB perspective, but my entity APIs reveal a problem.

    // 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?

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.

A: 

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

BCS
+2  A: 

See Working bi-directional links in the Hibernate documentation.

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.

DALFactory.FooAdapter.getBars(foo);
eed3si9n
+3  A: 

You might be missing a domain concept there. Have you tried creating a third entity: FooBarRelationship?

mmiika
sometimes this is appropriate, other times overkill. NH will let you store some basic data to capture the relationship, even without creating another entity.+1 for the suggestion though.
Ben Scheirman
If I had some other information to drag along with the relationship, I might do just that, but at the moment I'm hesitant to add that because I don't have such extra info.
James Thigpen
+1  A: 

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;}
}
Ben Scheirman
actually, if Foo's must be public so that consumers can use it, then expose it as some immutable collection and use internal the AddFoo() method.
Ben Scheirman
My collections are immutable, I just didn't want to bother with all that mess in the example.I've been going back and using internal to clean up some of the domain, I think this has really helped... We'll see how it works out.
James Thigpen