2

Let me demonstrate a simple example,

Class Organization

  • ID type Number,
  • Name type String,
  • HAS A -> Collection

Class Facility

  • ID type Number,
  • Name type String,
  • Location String ,
  • HAS A -> Organization

I am demonstrating a simple circular reference between two objects of an anemic data model. Assuming they are mapped to an ORM tool, a collection of Facility can be fetched lazily from an Organization, and likewise an Organization can be fetched lazily from a Facility.

I understand the convenience of this, at least on the middle tier. With a single Facility I can immediately obtain information about its Organization from code. My question then is:

Are there ANY other advantages to circular references in ORM mapped entities?

Despite my intense emotions on this subject right now, I am trying my best to not load the question...

The prime infuriating disadvantage I am sufferring through at this moment is when I am trying to serialize these objects into XML and send them across a web service, far from the lofty comforts of its benevolent ORM session. I can't serialize them as is because the ORM proxies cannot be sent, I must either eagerly fetch the child entities and send a MASSIVE object tree, or strip out the proxies from the graph.

Fair enough, so I wrote a utility to simply remove proxies, Great! It serializes and performance is salivatingly good, however when I get this object back from an UpdateEntity web service, it is missing the rest of the object graph, and now if I were to update this entity through the ORM, it thinks I have deleted all of these objects missing from the tree. No good.

I can't help but feel that circular references present a massive disadvantage in this scenario and I can't really seem to think of a good way to overcome this problem and the crippling performance problems I am tasked with solving without a data model redesign and massive refactoring.

What are your opinions on this? Is this indeed a design-smell?

1 Answer 1

1

When you serialize entities with circular references, you just ask the serializer to remove one of the dependencies. In your example, it would be Facility → Organization, unless you serialize only one Facility and don't need any other of the same organization.

It's technically possible and easy to do in C#. I expect the situation be quite similar in Java.

when I get this object back from an UpdateEntity web service, it is missing the rest of the object graph

It's not clear what are you trying to achieve here. Before accessing your data, you must first validate it, especially when it comes from user input, i.e. a source expected to be never trusted. You can't just flush all the data from your database to the user, then to receive the updated data and flush it back into the database.

Are there ANY other advantages to circular references in ORM mapped entities?

I don't think so. Maybe there are some advantages for the ORM, at validation level or to optimize performance, but I'm pretty unsure about this; in all cases, the primary goal of circular references in ORMs is to simplify the manipulation of data in business layer.

2
  • You can't just flush all the data from your database to the user, then to receive the updated data and flush it back into the database I suppose this is the real problem. There are tens of thousands of lines of code just throwing whatever is received directly into the database, and hundreds of unique hacks in both the server and client side to make that work. Even if I refactored it to where data access, business logic and client side business logic weren't so highly coupled then I still have no unit tests to verify that I didn't break something.
    – maple_shaft
    Nov 4, 2011 at 16:49
  • This is looking pretty grim... I am going to reprioritize this sprint, it seems I grossly miscalculated how much work would be needed to improve performance.
    – maple_shaft
    Nov 4, 2011 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.