Question

I can't seem to find the correct wording to search the web and get the right answer. So, every time I have an object that has a collection of other objects I get stuck on figuring out how to design the relationships.

For instance, a very rudimentary example would be.. enter image description here

So, I have a Person who can have many addresses. I know it would be incorrect to have the PersonDAO also create Addresses and put them in the Person object so how would I go about having one method (listAll()) for Person objects but have them come back with all of their addresses as well?

I hope this makes sense, please let me know if I need to clarify.

Also, the only thing I could find online that looked somewhat accurate was to use a CollectionsDAO but I wasn't sure how that would work so I threw it in there in red.

Was it helpful?

Solution

One solution would be to have the PersonDAO call the AddressDAO to get the Addresses and put them in the Person object(s) it returns. This would go inside a separate listFull() method or something of the sort. If an "Address" is a part of a "Person", I don't know that it is conceptually wrong for the PersonDAO object to also know how to populate Person instance with Address records. And making PersonDAO call AddressDAO to do the actual data access would seem to provide good separation of concerns (each DAO accesses it's own table and delegates to other DAOs where it needs to get more data to return more complex results).

As @Thieson alludes to, you have to ask yourself why you are bothering to derive. If there is functionality that you really do want to inherit and reuse between them, then fine. But otherwise there may be no point. I've seen a number of systems with a large quantity of objects where there is no direct hierarchy between the various DAO objects (broadleaf for example).

You'll probably get several answers here telling you to simply do whatever makes the most sense, and that's definitely good advice.

OTHER TIPS

You don't have to necessarily follow your entity relationship schema on your DAOs.

You can simply add a method called listAllWithAddress, for example, in the PersonDAO or create a separated DAO called PersonAddressDAO to represent their relationship.

There are no rules regarding that, but your own sense of judgement.

Regardless your comment about not adding the method, i would add the method in the PersonDAO, because it is going to return to me Person entities anyway, even if the addresses are populated.

My advice to you is to worry more about making sense then following restrictive rules

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top