I am trying to figure out the best design for instantiating an object which requires two separate calls to the data layer through a DAO. The object is not usable until these calls have been made (because it requires the data retrieved from the database). I have come up with three possible solutions:
- Create a method in the DAO itself that gets the necessary data from the database, and then pass the data to the object’s constructor. The object is passed back as the return value.
- Create a method in the class that is using the object. This method would make both calls to public methods in the DAO, then instantiate and return the object as in step 1.
- Pass the DAO as a parameter to the constructor of the object. The constructor would then make both necessary calls to the DAO, setting its own member variables without the need for Setters. The DAO is not used other than in the constructor.
Examples, if SomeObject is the object I want to create, and Foo is the class that will make use of it:
1 – DAO Contains the logic:
public SomeObject getSomeObject(){ // this DAO method called by Foo class
data1 = getData1();
data2 = getData2();
return new SomeObject(data1,data2);
}
2 – Class that uses object contains the logic:
public class Foo {
...
public void useSomeObject() {
SomeObject obj = createSomeObject();
obj.doStuff();
}
private SomeObject createSomeObject() {
data1 = dao.getData1();
data2 = dao.getData2();
SomeObject someObject = new SomeObject(data1,data2);
return someObject;
}
}
3 – SomeObject takes DAO as parameter and uses it in constructor:
public class SomeObject {
...
public SomeObject(DAO dao) {
data1 = dao.getData1();
data2 = dao.getData2();
}
...
}
Hopefully these examples are sufficient to convey my point. Are any of these solution considered better design than the others? If not, what solution is considered best practice?
Thanks.
6
In classical OOP, the notion of encapsulation embodies the idea of combining data with code; that is, providing methods that act specifically on the data they represent. So, for example, a Customer object contains only those methods which pertain specifically to customer “behavior.”
Persisting objects is an orthogonal concern.
The Customer object should not know anything about how it is being saved or retrieved from a database, XML file, or whatever (the concept is called Persistence Ignorance). This is why the preferred approach, especially in a large system, is to utilize some generalized persistence mechanism like an Object Relational Mapper to store and retrieve objects, and then to draw that functionality out through Repository methods that return objects or perform some business action on the data.
This relieves you of the responsibility of having to code a persistence implementation for each and every class you create.
Further Reading
The Unit Of Work Pattern And Persistence Ignorance
6
Your approach in example 1, make the DAO call the constructor, gets very close to the Repository Pattern.
However, it makes more sense to have an actual repository do the coordination.
class SomeObjectRepository {
SomeObjectDAO sod;
...
SomeObject getSomeObject() {
return new SomeObject(sod.getData1(), sod.getData2());
}
}
The DAO gets bound to the repository (a singleton) when it is created. Foo
then calls the repository method to get the object.
This way, the object becomes decoupled from the DAO, the DAO performs only basic operations and they are more easily tested with respect to each other.
3
I would go with your third option. It allows you to pass whatever DAO you want, and that could be very useful for testing. It also makes your code a bit more flexible: in case the details of data retrieval change, you only have modify the DAO class, and SomeObject
should work more or less the same.
The first option isn’t that bad. The data retrieval methods are within SomeObject
so now your class is a business object and a DAO.
The second option could lead to a situation where createSomeObject
gets copied and pasted around, as the need to create a SomeObject
increases and spreads throughout your application (I know, it might not happen, but it might). And when that happens, you’ll wish you had the data retrieval code in a central place, such as the constructor for SomeObject
, which is where you started.
1