STI什么时候有意义?我们为每种类型存储相同的信息,但使用方式不同
所以我知道性传播感染是有史以来最受谩骂的事情,但我有一个例子,我认为它可能确实有意义。我的应用程序正在解析一堆不同类型的 xml 文件。每个文件模型都存储完全相同的信息。只是有关它与哪个用户关联、上传时间以及在 S3 上存储位置的一些信息。
存储 xml 文件后,我会解析它以获取用于创建各种其他模型的信息。每种类型的文件都会创建不同的东西。可能有 100 种或更多不同类型的 xml 文件,尽管我不认为我会为那么多类型编写解析器。在这种情况下,STI 有意义吗?
我猜的缺点是模型都在一个目录中,因此除非破解 Rails 并将其粘贴到模型目录中的子目录中,否则它将淹没该目录。
另一个选择是我有一个 kind 字段,并将一些内容放入处理所有这些的 lib 目录中。或者我正在使用 resque,也许每个 xml 文件解析器都应该是它自己的工作。这样做也有缺点,比如在 Rails 控制台中强制执行工作有点尴尬。
So I know STI is the most reviled thing ever but I have an instance where I think it might actually make sense. My app is parsing a bunch of different types of xml files. Every file model stores the exact same information. Just some info about what user it is associated with, when it was uploaded, and where it is stored on S3.
After the xml file gets stored then I parse it for information which I use to create various other models. Each type of file is going to create different things. It is possible there could be 100 or more different types of xml files although I don't think I'm going to write parsers for that many. Does STI make sense in this case?
The downside I guess is models are all in one directory so it is going to flood that directory unless hack Rails and stick it in a subdir in models dir.
The other option is I have a kind field and put something in the lib directory that handles all this. Or I'm using resque, maybe every xml file parser should be it's own job. There are drawbacks to that though like it being kind of awkward to force a job in the rails console.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您的解释,“文件”模型仅存储文件上传过程的结果和关联的元数据。如果没有关于从解析的 XML 数据生成的其他类型模型的更多信息,我不明白为什么单表继承适用于这个用例。
From your explanation, the 'file' model is only storing the results of the file upload process and associated meta data. Without more information about the other kinds of models being generated from the parsed XML data, I don't see why single table inheritance applies to this use case.