I use C# to write a (hypothetical) application - online store.
I have a database of products, each product has following information associated with it:
- Product Number (required)
- Name (required)
- Price (required)
- Rating (optional)
- Sold Quantity (optional) -- this is total sale of this product
I have 4 pages that show filtered list of products. Pages show different information for each product:
- 1st page : PN, Name, Price
- 2nd page : PN, Name, Price, Rating
- 3d page : PN, Name, Price, Sold Quantity
- 4th page : PN, Name, Price, Rating, Sold Quantity
My question is, how do I design data structures to accommodate all my pages with little duplication?
Brute force approach is to create a type for each page:
IList<Product>
IList<ProductWithRating>
IList<ProductWithSoldQuantity>
IList<ProductWithRatingAndSoldQuantity>
later 3 can derive from Product
but due to lack of multiple inheritance ProductWithRatingAndSoldQuantity
can't derive from both Rating and SoldQuantity products.
In a dynamic language I would just add whatever fields I need and be happy.
So I could simulate a dynamic-language-approach by storing extra information (rating, sold quantities) in separate dictonaries, e.g.:
{
IList<Product> Products;
IDictionary<Product, Rating> ProductRatings;
IDictionary<Product, SoldQuantity> ProductSoldQuantities;
}
// is equivalent to
IList<ProductWithRatingAndSoldQuantities>
Building a Product structure that includes everything and then pass around a partially initialized object is not a solution I am looking for.
Any suggestions?