是否有一种普遍接受的方法将元信息添加到数组元素以指示如何处理它们?
我使用的数组包含数据库查询的结果,稍后将其格式化为 html(对于 Web 应用程序)或 csv(用于在电子表格中导入)。 我想将信息附加到数组元素,其中包含一些有关如何使用该元素的数据的附加信息。
例如,数组元素数据
- ……可以显示为链接:那么我想要附加链接信息。从数组创建 html 的代码可以使用它来创建链接。
- ... 是
2009-09-14
形式的日期:然后我想以某种方式将其标记为日期。如果用于 html 页面,则可以显示得更漂亮一些,例如 Mo Sep 14 或 Today;如果收件人是 csv,最好保留它。
是否有解决此问题的最佳实践方法?
我确实想到了几种可能的解决方案,但想问是否有人已经知道“最佳实践”。从可能最好到最差:
- 将每个数组元素存储为自定义创建的对象(日期、可链接、文本...),而不是将数组元素存储为文本。可能提供默认的 .to_string() 方法。
- 将每个数组元素设置为哈希值,这样我就可以说
a[5][7]['text']
或a[5][7]['link'].
- 制作不同版本的数组,例如
textArray[5][7]
、linkArray[5][7]
创建 html 作为开始并仅使用文本版本似乎是一个坏主意,因为外观根据使用情况而有所不同(例如2009-09-14与Mo Sep 14)。
或者我只是问了错误的问题?
I am using an array which contains the results of a database-query, which is later formatted as html (for a webapplication) or as csv (for import in a spreadsheet).
I want to attach information to the array-element which has some additional information about how the data of this element can be used.
For instance, array-element-data...
- ... can be displayed as a link: then I want the link information attached. The code which creates html from the array can use it to create a link.
- ... is a date of the form
2009-09-14
: then I want to somehow flag it as being a date. If the usage is for a html-page, it can then be displayed somewhat more beautiful, e.g. Mo Sep 14 or Today; if the recipient is a csv it would be best to leave it.
Is there a best-practice way of solving this problem?
I did think of several possible solutions, but wanted to ask if someone already knows a "best practice". From possibly best to worst:
- Store each array-element as custom-created object (Date,Linkable,Text...), instead of having the array-element as text. Possibly provide a default
.to_string()
method. - Make each array-element a hash, so that I could say
a[5][7]['text']
ora[5][7]['link']
. - Make different versions of the array, e.g.
textArray[5][7]
,linkArray[5][7]
Creating the html as a start and just using the text-version seems like a bad idea, as the appearance differs depending on the usage (e.g. 2009-09-14 vs Mo Sep 14).
Or am I just asking the wrong question?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除非指定语言,否则 (1) 和 (2) 基本相同。对象或哈希,动态编程语言除了语法之外还有什么区别?在 Lua 中,一切都是字典。
(1)/(2) 通常优于 (3),因为它们通常使复制元素及其元数据变得更加容易。例如,排序时。
因此,在不特定于语言/环境的情况下,在没有任何特殊条件的情况下,最佳实践是以某种方式组合元数据和元素,并处理结果数据类型。您可以通过定义一个新类来包含两者、定义原始元素类型的又一个子类、使用通用对、使用通用字典或仅将元数据存储在原始对象中(这将例如,Javascript 中的显而易见的方法)。
Unless you specify a language, (1) and (2) are basically the same. An object or a hash, what's the difference in a dynamic programming language other than perhaps syntax? In Lua everything's a dictionary.
(1)/(2) are usually preferred to (3), since they generally make copying an element along with its meta-data much easier. When sorting, for instance.
So without being specific to a language / environment, best practice in the absence of any special conditions is to somehow combine the meta-data and the element, and deal in the resulting datatype. You could do this by defining a new class to contain both, defining one more more subclasses of your original element type, using a generic pair, using a general-purpose dictionary, or just storing the meta-data in the original object (which would the be obvious approach in, say, Javascript).
作为一般建议,数据最好不包含有关如何表示自身的信息。
相反,创建表示的应用程序部分应该将这些设置放在单独的数据结构中。将其视为一个 XML 文件和创建不同表示形式的各种 XSLT 文件。
但是,如果这是不可能的,或者当您必须将两个信息合并到一个数据结构中以进行实际转换时,我遵循以下经验法则:
不要太聪明,做您语言中最自然的事情!
我有时也做过“解决方案3”,但我总是后悔。这些结构往往会成为维护的噩梦,并且从数据角度和编码角度来看,您很可能会遇到同步问题。
As a general advice it's best if the data contains no information at all on how to represent itself.
Instead the part of the application creating the representation should have these settings in a separate data structure. Think of it as a XML file and various XSLT files creating different representations.
But in cases where this is not possible or when you have to merge the two information into one data structure for the actual conversion, I followed this rule of thumb:
Don't be to clever and do what's most natural in your language!
I've done "Solution 3" sometimes, but I always regretted it. These structures tend to become a maintenance nightmare and you'll most likely run into synchronization isses, from a data point of view and also from a coding point of view.
Web 框架中的一种常见方法是将记录映射到对象:数据库中的一条记录被读入一个对象,因此您的结果是一个对象数组。对于不同的表,您需要不同的类。这是许多 Web 框架中使用的模型视图控制器 (MVC) 模式的构建块之一。
例如,在 Ruby on Rails 中,表
users
由类User
处理。您可以使用脚手架创建两者。此处,日期、布尔值、字符串、文本、小数、整数是不同的数据类型。不幸的是 URL 不是,所以我必须使用 String 作为链接。
您可以像这样从数据库中读取用户:
用户对象的属性具有正确的类型来处理日期、数字等:
if 100.days.ago < @u.joined then ....
数据固有的逻辑在 User 类中实现。
用户列表可以使用如下视图以 HTML 形式显示:
显示数据所固有的逻辑在视图中实现。
对象的哪个属性将被视为链接或普通文本的知识驻留在视图中,而不是对象本身。
显示/输出与 cvs 相同的数据是通过创建 cvs-view 来完成的。
A common approach in web frameworks would be to map records onto objects: one record from the database is read into one object, so your result is an Array of Objects. For different tables you need different classes. This is one of the building blocks for the Model View Controller (MVC) pattern used in many web frameworks.
For example in Ruby on Rails the Table
users
is handled by a ClassUser
. You create both using a scaffold.Date, Boolean, String, Text, Decimal, Integer are distinct datatypes here. Unfortunately URLs are not, so I have to use String for the link.
You can read users from the database like this:
the attribute of the user object have the correct types to work with dates, numbers, etc:
if 100.days.ago < @u.joined then ....
Logic that is inherent to the data is implemented in the User class.
The list of users may be displayed in HTML using a view like this:
Logic that is inherent to displaying the data is implemented in the view(s).
The knowledge which attribute of the object is to be treated as a link or a normal text resides in the view, not in the object itself.
Displaying / Outputting the same data as cvs is done by creating a cvs-view.