DB Roundtrips of entity updating in the repository pattern

I’m trying to wrap my head around OOP when building simple CRUD systems, using the Repository Pattern to handle object retrieval/saving to persistent storage.

I’ve already designed and implemented a system that doesn’t use OOP, just straight up get, insert, update and delete functions that contain SQL queries.

Non-OOP way

For example, for updating a customer I would call this function:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// PUT customer
function updateCustomer(idCustomer, data) {
db("customer").update(data).where("idCustomer", idCustomer);
}
</code>
<code>// PUT customer function updateCustomer(idCustomer, data) { db("customer").update(data).where("idCustomer", idCustomer); } </code>
// PUT customer
function updateCustomer(idCustomer, data) {
  db("customer").update(data).where("idCustomer", idCustomer);
}

OOP way

Now, to implement this in OOP using the Repository Pattern I would do something like:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// PUT customer
customer = customerRepo.getById(idCustomer);
customer.setData(data);
customerRepo.update(customer);
</code>
<code>// PUT customer customer = customerRepo.getById(idCustomer); customer.setData(data); customerRepo.update(customer); </code>
// PUT customer
customer = customerRepo.getById(idCustomer);
customer.setData(data);
customerRepo.update(customer);

OOP way seems to require an extra DB roundtrip

This OOP pattern seems to require DB roundtrips since I need to:

  • Instantiate a new customer object from the DB using the repo
  • Update the data in the object
  • Save the object back to the DB using the repo

Question

Isn’t my current, non-OOP way of doing things more efficient? If yes, are there any common solutions to these roundtrips when following the 2nd OOP method?

3

This isn’t right. You’re comparing two different things. A more fair and perfectly equal method for the second example would be:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// PUT customer
customer = Customer();
customer.setId(customerId);
customer.setData(data);
customerRepo.update(customer);
</code>
<code>// PUT customer customer = Customer(); customer.setId(customerId); customer.setData(data); customerRepo.update(customer); </code>
// PUT customer
customer = Customer();
customer.setId(customerId);
customer.setData(data);
customerRepo.update(customer);

And in that scenario, you’re also only doing one query (the update) but it’s still more object-oriented. Your problem is that you’re creating a “strawman” argument, suggesting that getting the customer from the database first is somehow intrinsic to object-orientation and the pattern, but that’s not true. OOP, Repositories, and whether or not to get the data from the database first have nothing to do with each other.

13

I believe that for the simplest of CRUD models, even Repository is an overkill.

The main purpose of repository pattern is to allow to query rich object model, that can then be manipulated in OOP way. This sacrifices some DB queries to gain ability to manipulate data in “better” way than plain SQL.

But in your case, it seems that there is no need for rich domain model, as you seem to be fine working with plain SQL.

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