code_junkie

How to deal with a Many-To-Many Relation in my API

Nov 12th, 2011
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. // Responsible for the relation
  2. public class Foo
  3. {
  4. List<Bar> Bars = new List<Bar>();
  5.  
  6. public void AddBar(Bar bar)
  7. {
  8. Bars.Add(bar);
  9. bar.AddFoo(this);
  10. }
  11. }
  12.  
  13. public class Bar
  14. {
  15. List<Foo> Foos = new List<Foo>();
  16.  
  17. // This shouldn't exist.
  18. public void AddFoo(Foo foo)
  19. {
  20. Foos.Add(foo);
  21. foo.AddBar(this); // Inf Recursion
  22. }
  23. }
  24.  
  25. public class Foo
  26. {
  27. private IList<Bar> Bars {get;set;}
  28.  
  29. public void AddBar(Bar bar)
  30. {
  31. Bars.Add(bar);
  32. bar.Foos.Add(this);
  33. }
  34. }
  35.  
  36. public class Bar
  37. {
  38. internal IList<Foo> Foos {get;set;}
  39. }
  40.  
  41. protected Set getEvents() {
  42. return events;
  43. }
  44.  
  45. protected void setEvents(Set events) {
  46. this.events = events;
  47. }
  48.  
  49. public void addToEvent(Event event) {
  50. this.getEvents().add(event);
  51. event.getParticipants().add(this);
  52. }
  53.  
  54. public void removeFromEvent(Event event) {
  55. this.getEvents().remove(event);
  56. event.getParticipants().remove(this);
  57. }
  58.  
  59. DALFactory.FooAdapter.getBars(foo);
  60.  
  61. public class Foo
  62. {
  63. List<Bar> Bars = new List<Bar>();
  64.  
  65. public void AddBar(Bar bar)
  66. {
  67. Bars.Add(bar);
  68. Bar.AddFoo(bar,this);
  69. }
  70. }
  71.  
  72. public class Bar
  73. {
  74. List<Foo> Foos = new List<Foo>();
  75.  
  76. // This shouldn't exist.
  77. public static void AddFoo(Bar bar, Foo foo)
  78. {
  79. bar.Foos.Add(foo);
  80. //foo.AddBar(this); inf recurtion
  81. }
  82. }
Add Comment
Please, Sign In to add comment