Book a Demo

Author Topic: when to use bi-directional associations?  (Read 5789 times)

fluxtah

  • EA User
  • **
  • Posts: 144
  • Karma: +0/-0
    • View Profile
when to use bi-directional associations?
« on: May 22, 2004, 05:09:24 am »
Hi,

I think I am probably over doing it with bi-directional associations, the reason I say this is you could essentially see a bi-directional association with any associated objects.

For instance;

A Customer has an Account
An Account is owned by a Customer

Customer <-------------> Account

I find in most cases when I look at my domain level classes I can find bi-directional associations

nother one;

A Consultant can deal with zero to many Jobs
A Job has one associated Consultant

I think the problem I have is that when I turn my domain level classes into business objects, I will be left with a lot of 2 way associations.

regards

Fluxtah

sargasso

  • EA Practitioner
  • ***
  • Posts: 1406
  • Karma: +1/-2
  • 10 COMFROM 30; 20 HALT; 30 ONSUB(50,90,10)
    • View Profile
Re: when to use bi-directional associations?
« Reply #1 on: May 23, 2004, 04:51:36 pm »
G'day Fluxtah,
LTNS :)

Not a very good answer today I'm afraid.  I don't think you should "worry" about it too much.  In fact, at the domain level I dont worry too much whether associations have any directionality at all.  The directionality comes into play at the collaboration level, when you are considering the need to navigate between objects. IOW do I need to get to the customer object from the account object when implementing this use case....

hth
Bruce
"It is not so expressed, but what of that?
'Twere good you do so much for charity."

Oh I forgot, we aren't doing him are we.

fluxtah

  • EA User
  • **
  • Posts: 144
  • Karma: +0/-0
    • View Profile
Re: when to use bi-directional associations?
« Reply #2 on: May 24, 2004, 03:08:31 am »
Hi Bruce, been busy working! but I am still alive and UML'ing :)

Thanks m8 that makes a lot of sense, I guess I should control how my business objects are populated with data depending in what situation they are used in.

Ian

fluxtah

  • EA User
  • **
  • Posts: 144
  • Karma: +0/-0
    • View Profile
Re: when to use bi-directional associations?
« Reply #3 on: May 24, 2004, 11:47:23 am »
Ah, one thing does spring to mind why I did worry a bit  :o

I am going to over simplify the example by deliberately short fusing the bi directional association by instantiating both classes where they are declared.

Code: [Select]

public class A{
  public B B = new B();
  public A{
  }
}

public class B{
  public A A = new A();
  public B{
  }
}


Now this will cause undesirable effects, however I might need to do this at a deeper level, some how A would need to know if its already been instantiated by B, and same with B for A.

hmmm confusing   :-[

angel-o-sphere

  • EA User
  • **
  • Posts: 112
  • Karma: +0/-0
    • View Profile
Re: when to use bi-directional associations?
« Reply #4 on: May 25, 2004, 01:52:53 am »
If you think you need to do that, you have to change your design :D

Your example means you have created two bi recursive functions like this:
Code: [Select]

initA() {
  initB();
}

initB() {
  initA();
}


Regardless wich you call first .... the program will end with a stack overflow.

Code: [Select]

public class A{
  public B *b* =  null;
  public void setB(B x) { b = x; }
  public A{
  }
}

public class B{
  public A *a* = null;
  public void setA(A x) { a = x; }
  public B{
  }
}  


Now you can do this:

Code: [Select]

A myA = new A();
B myB = new B();
  myB.setA(myA);
  myA.setB(myB);


In case you fear someone might forget to call the setter, wrap the code above into a factory method. One of the two classes could have a static method for that.

One or both classes could have a second constructor which accepts an object of the other type.
e.g.:
Code: [Select]

A myA = new A();
B myB = new B(myA);


The constructor of B is responsible to call A's setB method.

BTW: I'm not sure if you dont have a conceptual error in your mind.

You usually do not "instanciate classes", you instanciate "objects". So I corrected your code, note the stars. Your code likely would not have compiled because you named a variable like a class.

Regards,
   angel'o'sphere

fluxtah

  • EA User
  • **
  • Posts: 144
  • Karma: +0/-0
    • View Profile
Re: when to use bi-directional associations?
« Reply #5 on: May 25, 2004, 05:14:02 am »
ah cool thanks angel-o-sphere,

infact in .NET it allows you to name an object instance of the same name of the class and compiles fine, not sure why though :/

regards

Ian

sargasso

  • EA Practitioner
  • ***
  • Posts: 1406
  • Karma: +1/-2
  • 10 COMFROM 30; 20 HALT; 30 ONSUB(50,90,10)
    • View Profile
Re: when to use bi-directional associations?
« Reply #6 on: May 25, 2004, 05:42:26 am »
y not - try this for size:
Code: [Select]
using y;
namespace y {
     public class y {
           public y(y y) {
                 y = new y(y);
           }
           public y() {}
           public y Y{
                 get{
                       return new y();
                 }
           }
     }
}

;D
"It is not so expressed, but what of that?
'Twere good you do so much for charity."

Oh I forgot, we aren't doing him are we.

fluxtah

  • EA User
  • **
  • Posts: 144
  • Karma: +0/-0
    • View Profile
Re: when to use bi-directional associations?
« Reply #7 on: May 25, 2004, 10:01:17 am »
i have been known to ask the weirdest questions, and occasionally do stupid things :(

still, consider the following, c# allows this btw :

Code: [Select]

public class Customer{

   private Account _Account;
   public Account Account{
       get{
           return _Account;
       }
       set{
           _Account = value;
       }
       
   }
   public Customer(){
   }
}


FxCop does'nt complain about this either, still.. would you recommend me to avoid this? :|

however, fxcop does complain about this

Code: [Select]

public class Customer{

   private Account _account;
   public Account account{
       get{
           return _account;
       }
       set{
           _account = value;
       }
       
   }
   public Customer(){
   }
}


regards

Ian
« Last Edit: May 25, 2004, 10:03:37 am by fluxtah »