OOP and relational databases [duplicate]

I see a lot of talk in the OOP world about principles and laws such as Open/Close and Loose Coupling, I can understand how they are so high valued, However I seem to have ran into a problem with applying these principles and laws once I start to include relational databases.

For example purposes, if I have a application which we are able to separate well into multiple components(Open/Close design) and allow them to work with each other when if needed, I start to run into problems when trying to enforce referential integrity.
If I enforce a relational setup, and enforce integrity through foreign keys and so on, the tables become dependent on each other, gradually my OOP application logic starts to fall apart and become one big system.

I can of course remove all references to tables, but then I risk making data redundant and start to repeat code/data, which then goes against the Don’t Repeat Yourself principle.

Have you came across such problems before? If so how did you handle it? Perhaps there is a way to achieve both referential integrity and OOP open/close components.

Thanks

1

My suggestion:

You have to enforce referential integrity with FK. Let the RDBMS do that work and let the classes throw exceptions.

Tables being related has nothing to do with coupling of classes or SOLID principles. That’s classes’s stuff.

Tables are orthogonal to classes.

Don’t overengineer things.

First of all: You are right, this is a real problem. As so often design issues surface when you try to test stuff. While testing is most of the time fairly easy if you have some design skills and OOP based code, things get tricky when the database gets involved.

One symptom is that if you want to have an entity A (i.e. an object persisted in a database) you need the full tree of dependencies and don’t have an easy way to mock those.

The reason for this kind of problem is that there is no separation between runtime and compile time in databases, thus a dependency you want to have/use in runtime is there all the time.

A theoretical solution is to create a database system that actually has something like interfaces. But that is probably out of scope for most of us.

The more realistic solution is to separate the database very strictly from the rest of the code, thus isolating the big convoluted blob that is commonly referred to as database schema from the nicely structured application.

The first step is to have Repositories that return your entities and leak no information what so ever where and how these entities get persisted. A good way to ensure this is to have two implementations of those: one actually using your database and one using for example simple HashMaps for storage. The later are also very useful for testing. Be wary of any frameworks bleeding into your domain code. Hibernate for example has the habit of doing that by means of lazy loading and similar mechanisms.

The second step is to identify aggregates in the sense of Domain Driven Design. I.e. bags of entities that get controlled by one entity and its repository, the entity root. For example LineItems of Orders should probably only loaded and updated via an Order, never on its own. But entities that belong to a different aggregate (for example customers) should get loaded via their own repository and not via some kind of magic lazy loading on navigation. This way you can store different entities in different datastores, your application doesn’t care about that. If they happen to be in the same database (as they probably do) they can have foreign keys, no problem.

There might be one technical challange with this approach caused by constraints: Normaly constraints get enforced on every single DML statement. If that causes you issues you might consider deferred constraints which only get enforced on commit. This might actually benefit performance, but also might make debugging more difficult, because now you might get foreign key violations on commit, long after your DML statements happened.

I think that OOP and RDBMS are not good friends. OOP seems to me more apt to deal with “rows” in each table. RDBMS deals with “sets of rows”.

Nevertheless, there are lots of frameworks that try to overcome these problems, but they lose the “purity” of OOP.

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

OOP and relational databases [duplicate]

I see a lot of talk in the OOP world about principles and laws such as Open/Close and Loose Coupling, I can understand how they are so high valued, However I seem to have ran into a problem with applying these principles and laws once I start to include relational databases.

For example purposes, if I have a application which we are able to separate well into multiple components(Open/Close design) and allow them to work with each other when if needed, I start to run into problems when trying to enforce referential integrity.
If I enforce a relational setup, and enforce integrity through foreign keys and so on, the tables become dependent on each other, gradually my OOP application logic starts to fall apart and become one big system.

I can of course remove all references to tables, but then I risk making data redundant and start to repeat code/data, which then goes against the Don’t Repeat Yourself principle.

Have you came across such problems before? If so how did you handle it? Perhaps there is a way to achieve both referential integrity and OOP open/close components.

Thanks

1

My suggestion:

You have to enforce referential integrity with FK. Let the RDBMS do that work and let the classes throw exceptions.

Tables being related has nothing to do with coupling of classes or SOLID principles. That’s classes’s stuff.

Tables are orthogonal to classes.

Don’t overengineer things.

First of all: You are right, this is a real problem. As so often design issues surface when you try to test stuff. While testing is most of the time fairly easy if you have some design skills and OOP based code, things get tricky when the database gets involved.

One symptom is that if you want to have an entity A (i.e. an object persisted in a database) you need the full tree of dependencies and don’t have an easy way to mock those.

The reason for this kind of problem is that there is no separation between runtime and compile time in databases, thus a dependency you want to have/use in runtime is there all the time.

A theoretical solution is to create a database system that actually has something like interfaces. But that is probably out of scope for most of us.

The more realistic solution is to separate the database very strictly from the rest of the code, thus isolating the big convoluted blob that is commonly referred to as database schema from the nicely structured application.

The first step is to have Repositories that return your entities and leak no information what so ever where and how these entities get persisted. A good way to ensure this is to have two implementations of those: one actually using your database and one using for example simple HashMaps for storage. The later are also very useful for testing. Be wary of any frameworks bleeding into your domain code. Hibernate for example has the habit of doing that by means of lazy loading and similar mechanisms.

The second step is to identify aggregates in the sense of Domain Driven Design. I.e. bags of entities that get controlled by one entity and its repository, the entity root. For example LineItems of Orders should probably only loaded and updated via an Order, never on its own. But entities that belong to a different aggregate (for example customers) should get loaded via their own repository and not via some kind of magic lazy loading on navigation. This way you can store different entities in different datastores, your application doesn’t care about that. If they happen to be in the same database (as they probably do) they can have foreign keys, no problem.

There might be one technical challange with this approach caused by constraints: Normaly constraints get enforced on every single DML statement. If that causes you issues you might consider deferred constraints which only get enforced on commit. This might actually benefit performance, but also might make debugging more difficult, because now you might get foreign key violations on commit, long after your DML statements happened.

I think that OOP and RDBMS are not good friends. OOP seems to me more apt to deal with “rows” in each table. RDBMS deals with “sets of rows”.

Nevertheless, there are lots of frameworks that try to overcome these problems, but they lose the “purity” of OOP.

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