I'll be coding up both to test them, but I was curious on first blush what the folks here thought.
我将编写这两个来测试它们,但我很好奇第一次脸红的人们在想什么。
I've got an array of data that has been posted to my shopping cart controller. The array includes an entry for each of the product's potential options.
我已经将一系列数据发布到我的购物车控制器中。该阵列包括每个产品的潜在选项的条目。
To get the options into an array for presentation I can either parse the array looking for arrays with keys that begin with "options-" or I can make a database call and find out what options are available for that product.
要将选项放入数组进行演示,我可以解析数组,查找带有以“options-”开头的键的数组,或者我可以进行数据库调用并找出该产品可用的选项。
We are talking about at most 5 items at this time.
我们目前最多讨论的是5件物品。
Thoughts?
思考?
2 个解决方案
#1
3
I think the keyword here is that the data is POST-ed to your controller.
我认为这里的关键字是数据被POST到您的控制器。
You should never trust user data. Always verify user data with real data. So, if a user adds something to a shopping cart, make sure you go back to your database and ensure that what was added really does exist. Since you will be making the query at that time, best to rely on the data from your database.
您永远不应该信任用户数据。始终使用实际数据验证用户数据。因此,如果用户向购物车添加了某些内容,请确保返回到您的数据库并确保添加的内容确实存在。由于您将在那时进行查询,因此最好依赖数据库中的数据。
Otherwise, data you already have in memory is certainly faster than going to a DB. Typically, you want to avoid making additional queries that are not needed.
否则,您在内存中已有的数据肯定比转到数据库要快。通常,您希望避免进行不需要的其他查询。
#2
0
Pros of Hard coding
硬编码的优点
- More readable code
- 代码更易读
- Less DB use. Though you could use memcached to store a key:value part of options.
- 减少数据库使用。虽然您可以使用memcached存储密钥:值的一部分选项。
Cons of Hard Coding versus DB call
硬编码与数据库调用的缺点
- Typos will bring down the cart.
- 错别字将推倒购物车。
- If you need to reference the options in several locations, you will make errors
- 如果您需要在多个位置引用选项,则会出错
#1
3
I think the keyword here is that the data is POST-ed to your controller.
我认为这里的关键字是数据被POST到您的控制器。
You should never trust user data. Always verify user data with real data. So, if a user adds something to a shopping cart, make sure you go back to your database and ensure that what was added really does exist. Since you will be making the query at that time, best to rely on the data from your database.
您永远不应该信任用户数据。始终使用实际数据验证用户数据。因此,如果用户向购物车添加了某些内容,请确保返回到您的数据库并确保添加的内容确实存在。由于您将在那时进行查询,因此最好依赖数据库中的数据。
Otherwise, data you already have in memory is certainly faster than going to a DB. Typically, you want to avoid making additional queries that are not needed.
否则,您在内存中已有的数据肯定比转到数据库要快。通常,您希望避免进行不需要的其他查询。
#2
0
Pros of Hard coding
硬编码的优点
- More readable code
- 代码更易读
- Less DB use. Though you could use memcached to store a key:value part of options.
- 减少数据库使用。虽然您可以使用memcached存储密钥:值的一部分选项。
Cons of Hard Coding versus DB call
硬编码与数据库调用的缺点
- Typos will bring down the cart.
- 错别字将推倒购物车。
- If you need to reference the options in several locations, you will make errors
- 如果您需要在多个位置引用选项,则会出错