How would you model/program the following:

We have items in our domain, let's say cars. We also have categories for cars. A category has certain characteristics.

So for example we could have a car named Model A and place it in the category Sedan. The Sedan category has the characteristics 'five-doors' and 'five adults' (I'm just making it up as I go).

So our Model A car has these two characteristics, just by the virtue of being added to the Sedan category.

But now we want to add certain characteristics on top of that, even if they are from other categories. That's the easy part, just link the car to a category and link the car to separate characteristics.

But what if we want to override one of the characteristics from the Sedan category by removing it?

So in essence, we want this:

  • Model A
    • Sedan category
      • five-doors
      • five adults // we never want this characteristic
    • extra characteristics:
      • sporty // we've added this

I hope this is clear. I easily see how we can link a car to a category and link a car to characteristics (via many-to-many relationships, both in code and database). But how would we remove one of the characteristics of a category the car is linked to?

Is this a good solution?

  • Model A
    • Sedan category
      • five-doors
      • five adults
    • extra characteristics:
      • sporty // we've added this
    • removed charcteristics:
      • five adults // we never want this
    • GetCharacteristics // implement logic to combine the above info
  • It depends, in this simple example I would duplicate the category references into the modal a. So use the categories as a helper to automatically insert them but then the association becomes part of modal a. When adding a sporty add it to all (if desired by user). Removing is a bit more difficult. You could save with the association a flag: auto-added which you can remove automatically. But manually user added ones stay for example. Also for this more input is needed on the real business case. If you go for the proposed model you get very complex queries for looking up anything. – Luc Franken Sep 13 '16 at 7:10
  • Yes, but using the category just as a helper makes us lose the category reference once we've added the characteristics. Something I will have to check with the customer to know if that is a problem. – Peter Sep 13 '16 at 7:18
  • You can still have an reference to the category, no need to remove it. You can even store the data in model a like: model A - sedan - five doors, model A - null - Sporty. as separate rows. – Luc Franken Sep 13 '16 at 7:21
  • If cars from the Sedan category can seat 5 adults and Model A looks like a Sedan but can't seat those 5 adults then either 1) Model A doesn't actually belong in the Sedan category but in a different, similar category (e.g. Small Sedan or Sports Sedan), or 2) The characteristic that a Sedan category can seat 5 adults is wrong and you need another category to put that characteristic under. – Bart van Ingen Schenau Sep 13 '16 at 9:47
  • 1
    @Peter: I don't think your analogy is falling apart. When you have an item that almost, but not quite, fits in your existing categories, then that is a very good moment to look critically at your categories if they are still right. You (and your client) might come to the conclusion that some attributes were added to a category because it was convenient and not because those attributes really belong to that category. Or you could come to the conclusion that you missed some categories because they were not needed until now. – Bart van Ingen Schenau Sep 13 '16 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.