Automatic Associations

Table of contents
No headers

The modeller has the ability to generate code that maintains bi-directional associations automatically.  For example, imagine you have a Customer class that has a list of Orders, and the Order has a reference to a Customer.  When you assign to Order.Customer, the order will automatically be added to the Customer.Orders list.  If you add to Customer.Orders, the Customer property of the order will be set.

Normally automatic associations work well to keep your model consistent.  However, in some cases, this can cause undesired behavior that can lead to performance and memory problems.  For example, imagine that we have thousands of orders for a customer, and then we make a new order, setting the Customer property.  This will load the Orders list from the database, with the thousands of orders, even though you just wanted to save the new single order.  There are three basic ways to work around this.

  1. Turn off the Many side of the relationship.  It's frequently the case that you never really need to navigate from the one side to the many side, so it's best to simplify the relationship and turn off property generation for the many side.  Navigation and querying can still easily be performed using the one side.  This option is the safest option.
  2. Turn off the automatic association update so that changing the Customer property doesn't update the Orders collection of that customer.  Note that this has the problem that it potentially makes your model inconsistent.  You should generally ensure that you're not working with the Orders collection in the same session that you're assigning the Customer property (unless you do the add manually).  Using this option makes it more difficult to guarantee correctness, so it should only be used when the first is not an option.
  3. PMODEL-236 added another alternative, a one-way setter, for when you want to keep the automatic association but not update it for certain calls.  A "Set{Property}" method is added for these circumstances (e.g. "SetCustomer") which can be called with a "SetOptions" parameter where you can set SetOptions.Bidirectional to false.  For this call only, the parent collection will not be updated.  This has some of the shortcomings of method 2, in that the collection will need to be updated manually, but can be used in high-performance areas only.
Tag page
You must login to post a comment.