What is the best pattern/design for keeping pre-defined values that affect the behaviour of an app?
For example, say I have an e-shop system, where each purchase is fulfiled by calling to certain API. There are different providers, each with their own APIs, options and requirements, and each product in the store has been assigned a provider.
The providers could be stored in different ways:
- In a separate table in the database and referenced in the products table via foreign key
- In the same products table with a MySQL ENUM or
varchar
and then defined as constants in the code itself
Keeping the data in the database seems like the cleanest solution, however, as it's affecting the code, that means that values will be hard-coded in the code. For example:
if($product->provider === "MyProviderOne") { ... }
if($product->provider === "MyProviderTwo") { ... }
// or
if($product->provider->id === 1) { ... }
if($product->provider->id === 2) { ... }
Which seems like bad design. It could be also defined as constants and used as
if($product->provider === Providers::MyProviderOne) { ... }
if($product->provider === Providers::MyProviderTwo) { ... }
But there are many arguments against using ENUM types in MySQL as well as it's harder to maintain, as the data has to be changed in both code and the database. I do not like this link between database and code, that has to be maintained manually. As the values affect the different logic in the code, it makes sense to keep the values just in the code, but then the code becomes the store of the data.
What would be the best design/pattern for such situations?
Aucun commentaire:
Enregistrer un commentaire