Is it considered bad practice to make a DAO call in an object’s constructor?

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:

  1. 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.
  2. 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.
  3. 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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public SomeObject getSomeObject(){ // this DAO method called by Foo class
data1 = getData1();
data2 = getData2();
return new SomeObject(data1,data2);
}
</code>
<code>public SomeObject getSomeObject(){ // this DAO method called by Foo class data1 = getData1(); data2 = getData2(); return new SomeObject(data1,data2); } </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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;
}
}
</code>
<code>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; } } </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class SomeObject {
...
public SomeObject(DAO dao) {
data1 = dao.getData1();
data2 = dao.getData2();
}
...
}
</code>
<code>public class SomeObject { ... public SomeObject(DAO dao) { data1 = dao.getData1(); data2 = dao.getData2(); } ... } </code>
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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class SomeObjectRepository {
SomeObjectDAO sod;
...
SomeObject getSomeObject() {
return new SomeObject(sod.getData1(), sod.getData2());
}
}
</code>
<code>class SomeObjectRepository { SomeObjectDAO sod; ... SomeObject getSomeObject() { return new SomeObject(sod.getData1(), sod.getData2()); } } </code>
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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật