I am working on the domain model of an application that stores information provided by sellers about the goods they sell on a platform. The application just acts as a data store for this information. The information is called as an "ProductFact", and may be diverse. Each example below represents one "ProductFact" instance.
- the selling price of the item for general public.
- the discount the seller can offer for some specific customers (an absolute amount, or percentage of the selling price).
- the minimum quantity a customer has to buy to avail free shipping.
The above are some cases that are known as of now, but there might be a few more in the future, with no guarantee of sharing any property with the already existing "ProductFact"s. Different clients will be calling this application, each one looking for a specific type of "ProductFact".
We will be using a NoSQL backend, so db schema is not an issue. The problem is with the application layer. I am not sure how to model this particular object. I have been trying to model it as :
public class ProductFact {
private ProductFactType productFactType; //enum for different types like SELLING_PRICE, DISCOUNTS, MIN_QUANT, etc.
private ProductFactData productFactData; // for representing the actual data.
}
It would have been ideal if ProductFactData had a behavior, so I could have used it as an interface and created implementations or each use-case. But this is just data, and inheritance seems to be a hacky approach.
Another approach could be:
public class ProductFact {
private ProductFactType productFactType;
private SellingPriceFactData sellingPriceFactData;
private DiscountFactData discountFactData;
.
.
.
private SomeProductFactData someProductFactData;
}
The client could check for the value of "productFactType" and then call the getter for the appropriate FactData type.But even this approach seems hacky.
What would be a good way of modelling this scenario?
Edit
It seems the problem statement could use some more clarity. Discount and Price are two of the many pieces "ProductFact"s that the application could store. There could be others in the future, including:
- Information about express delivery eligibility of the product, say "ExpressDeliveryProductFact" - which areas is it available for, what is the minimum quantity required.
- Information about the condition of the product - is it used, new?
There would be different clients querying this application, each looking for the type of ProductFact it are interested in. E.g., a DeliveryService client would be interested in the "ExperssDeliveryProductFact".
As can be seen, these pieces of information seemingly don't have anything in common, which is posing problems in the domain model.
private SellingPriceFactData sellingPriceFactData
and notprivate decimal sellingPrice
?ToString()
override. Other than that, their only relationship is that they are both participants in a composition relationship withProduct
, meaning that they are properties of that type. That is all. Unless there is some other requirement to treat them the same?decimal
(probably). The way you prevent the caller from assigning a Price to something that isn't supposed to have a price is: Don't give anything a Price property that isn't supposed to have one. The way to prevent the caller from assigning the wrong property to a Product is: only give Product the properties that belong to a Product. I don't understand this obsession with creating a special type for it. The composition relationship has nothing to do with the types of the properties being composed.