What is the correct pattern to use in this case?

I’m sure this scenario has arisen before, and I want to know what experience has taught to be the best solution.

I have a number of classes that are all of a kind. Say all the objects are “Content”. They may be “Article”, or “Book” for example.

The reason I want the “Content” abstraction is because I want to define a number of behaviours for all “Content” objects and not have to build a new DB Table and 10 classes of essentially the same code for each type of “Content”. For example, to attach a “Tag” or a “Premise” to a content object would be much nicer if, say, I just had two columns one for ContentID and one for TagID.

A solution I’ve played around with is to have a Content table with a unique ID, and then to have foreign key references on all the other tables (Book, Article, etc). This has actually proven quite solid, but I’m just not sure about it.

Do you know how to call this described pattern?

2

You can map inheritance to database tables in the following ways,

  • Map the entire class hierarchy to a single table: All attributes of the classes are stored in one table. This is a good strategy for simple and/or shallow class hierarchies where there is little or no overlap between the types within the hierarchy.

  • Map each concrete class to its own table: A table is created for each concrete class, each table including both the attributes implemented by the class and its inherited attributes. This is a good strategy when changing types and/or overlap between types is rare.

  • Map each class to its own table: Create one table per class, with one column per business attributes and any necessary identification information (as well as other columns required for concurrency control and versioning). This is a good strategy when there is significant overlap between types or when changing types is common..

As said, none of these mapping strategies are ideal for all situations. Experts recommend to start with one table per hierarchy at first, then if needed refactor schema accordingly.

Do you know how to call this described pattern?

Catalog of Patterns of Enterprise Application Architecture calls this Inheritance Mappers.

2

I have been running into something similar, with a base table of Persons, a useful abstraction, and then other tables depending on whether they are Contractor, Client, etc.

For the table design, if the “sub table” (in your case, Book, Article) cannot exist without being Content, then I’d go with your design, and use the Content_ID as the key for Book and Article as well.

Since I use PHP, I have a class that handles Content, then a class that extends that to handle Books/Articles.

If you are using the database, I’d recommend a VIEW which SELECTS and JOINs the Content-Book tables, and another with does Content-Articles for ease of use.

If you know in advance the entire set of possible types of content you could use table-per-hierarchy and stick all of the books, articles etc. sub-classes of Content into one table,
If, on the other hand, you may (or already know you will) need new subclasses of Content later on I’d use table-per-subclass (like you are thinking about)

So, the question in my opinion is to ask: Is my set of Content subclasses complete or should it be easily extendable later?

5

Your model is solid because you have a strong idea about the different types of content you want to manage (books, articles, etc.). Having a table for each type of content defines the different attributes. You can probably predict over 90% of the types you’ll need for several years.

If you put them in one large table, you will have several columns that will be null. There are some databases that have improved the handling of sparse data and can save on space. From a data query perspective, you can include different types of content very easily.

I’ve seen several systems (mainly enterprise apps) where they provide greater flexibility for user-defined fields. Unlike your situation, they can’t predict all the entities. A flexible way to do this is with a Entity Attribute Value Model. The joining of extra tables and possibly transposing row data into columns, performance is sacrificed over user flexibility. Basically, do you want your app to be programmer/code driven or user/data driven?

You may want to consider how you’re going to handle searching. There are tools out there to make this easier. I’ve you’re going to build your own, you have to deal with multiple tables.

Maybe a NoSQL solution should be considered?

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