请求有关将对象从动态语言持久保存到文档数据库的建议

发布于 2024-08-23 04:22:41 字数 738 浏览 3 评论 0原文

您对在文档数据库中保存来自动态语言的对象的最优雅的方法有什么见解吗?

我有扎实的 C# 背景,并且刚刚开始使用 Python 编程。同时我也在尝试学习 MongoDB 的诀窍。

现在我想知道:将数据保存到 MongoDB 数据库的最优雅的方法是什么?我考虑了几种方法:

  1. 让我的所有 Python 类都能够创建代表其状态的字典和列表的图表。此外,让他们能够从这样的图表初始化他们的状态。当我想要持久化一个对象时,我会询问它的图形表示并持久化它。当我想要获取一个对象时,我将检索一个文档图并将其提供给我的类的 __init__ 方法。

  2. 创建一个单独的 Mapper 类,能够检查给定的对象并创建字典和列表的图形,然后我可以将其存储在 MongoDB 中。映射器还负责创建其数据已从数据库检索的对象。

  3. 我尝试了 mongoengine,一个文档对象映射器。然而,当它迫使我从特定类(文档)派生我的类时,我感到很失望。这让我想起了微软的Entity Framework 1.0以及缺乏POCO支持。我不想被迫从某个特定的类派生。感觉不对,但我不确定这是否真的是动态语言中的问题。

我的C#背景是否阻碍了我的思维?我确信我还没有理解动态语言提供的灵活性,因此任何有关最佳实践的建议或提示将不胜感激。

谢谢。

Do you have any insights into the most elegant way of persisting objects from a dynamic language in a document database?

I have a solid background in C# and have just started programming in Python. At the same time I am trying to learn the ropes of MongoDB.

Now I am wondering: what is the most elegant way to persist my data to the MongoDB database? I have considered several approaches:

  1. Make all my Python classes able to create a graph of dictionaries and lists representing their state. Moreover, make them able to initialize their state from such a graph. When I want to persist an object, I will ask it for its graph representation and persist that. When I want to get an object, I will retrieve a document graph and provide this to the __init__ method of my class.

  2. Create a separate Mapper class capable of inspecting a given object and creating a graph of dictionaries and lists, which I may then store in MongoDB. The mapper would also be responsible for creating objects whose data has been retrieved from the database.

  3. I tried out mongoengine, a document-object mapper. However, I was disappointed when it forced me to derive my classes from a particular class (Document). It reminded me of Microsoft's Entity Framework 1.0 and the lack of POCO support. I don't want to be forced to derive from a particular class. It doesn't feel right, but I am unsure whether this is really a problem in a dynamic language.

Is my thinking being hindered by my background in C#? I am sure I haven't grokked the extent of the flexibility that a dynamic language provides, so any advice or hints at best practices would be greatly appreciated.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晌融 2024-08-30 04:22:41

Python 定义了一些特殊方法,例如 getstate 和许多其他允许您的类准确定义如何最好地序列化和反序列化其实例。它们都由 pickle 模块内部使用(然后该模块使用此信息生成“blob”,即一串字节,并从此类 blob 中恢复对象),但是,如果您想要更好的话通过直接存储图形而不是通过不透明的 blob 获得索引,这基本上是一个调整 pickle 过程以在将图形转换为 blob 之前停止的问题。我认为您必须通过复制粘贴编辑 pickle.py 来完成此操作(因为它不是设计为通过更优雅的方法(例如子类化)以这种方式自定义的),但这仍然应该为您节省大量从头开始重做的工作。

我相信这种方法介于您的选项 1 和 2 之间——类只需要为了响应特定需求而定义此类特殊方法,并且编排各种可能性所需的大部分工作将由您的 pickle 变体处理(就像对于序列化形式是 blob 的“正常”情况,它由 pickle 本身处理。

Python defines several special methods such as getstate and many others to allow your classes to define exactly how best to serialize and de-serialize their instances. They're all used internally by the pickle module (which then uses this information to produce a "blob", i.e. a string of bytes, and restore objects from such blobs), but, if you want better indexing obtained by storing graphs directly rather than via opaque blobs, it's basically a question of tweaking the pickle procedures to stop just before turning the graphs into blobs. I think you'll have to do it by copy-paste-edit of pickle.py (as it's not designed to be customized in this way by more elegant methods such as subclassing), but that should still save you lots of work wrt redoing it all from scratch.

I believe this approach lies somewhere between your options 1 and 2 -- classes need to define such special methods only in response to specific needs, and most of the work needed to orchestrate the various possibility will be handled by your pickle-variant (much as it's handled by pickle itself for the "normal" case where the serialized form is a blob).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文