SQLAlchemy实现的模式

When I first created SQLAlchemy, I knew I wanted to create something significant. It was by no means the first ORM or database abstraction layer I’d written; by 2005, I’d probably written about a dozen abstraction layers in several languages, including in Java, Perl, C and C++ (really bad C and even worse C++, one that talked to ODBC and another that communicated with Microsoft’s ancient DB-LIB directly). All of these abstraction layers were in the range of awful to mediocre, and certainly none were anywhere near release-quality; even by late-90’s to early-2000’s standards. They were all created for closed-source applications written on the job, but each one did its job very well.

当我第一次创建SQLAlchemy时,我知道我想创建一些重要的东西。 这绝不是我编写的第一个ORM或数据库抽象层。 到2005年,我可能已经用几种语言编写了大约十二层抽象层,包括Java,Perl,C和C ++(确实很糟糕的C,甚至更糟糕的C ++,其中一个与ODBC通讯,另一个与Microsoft古老的DB-LIB通信)直)。 所有这些抽象层都在可怕到中等水平的范围内,并且肯定没有哪个接近发布质量。 甚至从90年代末到2000年代初的标准。 它们都是为在工作中编写的封闭源应用程序而创建的,但是每个人都做得很好。

It was the repetitive creation of the same patterns over and over again that made apparent the kinds of things a real toolkit should have, as well as increased the urge to actually go through with it, so that I wouldn’t have to invent new database interaction layers for every new project, or worse, be compelled by management to use whatever mediocre product they had read about the week before (keeping in mind I was made to use such disasters as EJB 1.0). But at the same time it was apparent to me that I was going to need to do some research up front as well. The primary book I used for this research was Patterns of Enterprise Archictecture by Martin Fowler. When reading this book, about half the patterns were ones that I’d already used implicitly, and the other half were ones that I was previously not entirely aware of.

是一次又一次地重复创建相同的模式,这使一个真正的工具箱应该具有的东西变得显而易见,并且增加了实际使用它的冲动,因此我不必发明新的数据库对于每个新项目,或更糟的是,每个新项目的交互层都被管理层强迫使用他们上周阅读的平庸产品(请记住,我不得不使用EJB 1.0这样的灾难)。 但同时,对我来说,很明显,我也需要预先进行一些研究。 我用于这项研究的第一本书是Martin Fowler撰写的《企业架构模式》 。 在阅读本书时,大约一半的模式是我已经隐式使用的模式,另一半是我以前并不完全了解的模式。

Sometimes I read comments from new users expressing confusion or frustration with SQLAlchemy’s concepts. Maybe some of these users are not only new to SQLAlchemy but are new to database abstraction layers in general, and some maybe even to relational databases themselves. What I’d like to lay out here is just how many of POEAA’s patterns SQLAlchemy is built upon. If you’re new to SQLAlchemy, my hope is that this list might help to de-mystify where these patterns come from.

有时我会读一些新用户的评论,表达对SQLAlchemy概念的困惑或沮丧。 也许其中一些用户不仅是SQLAlchemy的新手,而且通常还是数据库抽象层的新手,甚至某些用户甚至是关系数据库本身。 我想在这里介绍的是SQLAlchemy建立在POEAA模式上的模式。 如果您是SQLAlchemy的新手,我希望此列表可以帮助您弄清楚这些模式的来源。

These links are from Catalog of Patterns of Enterprise Architecture.

这些链接来自《企业体系结构模式目录》

  • Data Mapper – The key to this pattern is that object-relational mapping is applied to a user-defined class in a transparent way, keeping the details of persistence separate from the public interface of the class. SQLAlchemy’s classical mapping system, which is the usage of the mapper() function to link a class with table metadata, implemented this pattern as fully as possible. In modern SQLAlchemy, we use the Declarative pattern which combines table metadata with the class’ declaration as a shortcut to using mapper(), but the persistence API remains separate.
  • Unit of Work – This pattern is where the system transparently keeps track of changes to objects and periodically flushes all those pending changes out to the database. SQLAlchemy’s Session implements this pattern fully in a manner similar to that of Hibernate.
  • Identity Map – This is an essential pattern that establishes unique identities for each object within a particular session, based on database identity. No ORM should be without this feature, as working with object structures and applications of the most moderate complexity is vastly simplified and made more efficient with this pattern in place.
  • Metadata Mapping – this chapter in the book is where the name MetaData comes from. The exact correspondence to Fowler’s pattern would be the combination of mapper() and Table.
  • Query Object – Both the ORM Query and the Core select() construct are built on this pattern.
  • Repository – An interface that serves as the gateway to the database, in terms of object-relational mappings. This is the SQLAlchemy Session.
  • Lazy Load – Load a related collection or object as you need it. SQLAlchemy, like Hibernate, has a lot of options in how attributes can load things.
  • Identity Field – Represent the primary key of a table’s row within the object that represents it.
  • Foreign Key Mapping – Database foreign keys are represented using relationships in the object model.
  • Association Table Mapping – A class can be mapped that represents information about how two objects are related to each other. Use the Association Object for this pattern.
  • Embedded Value – a value inline on an object represents multiple columns. SQLAlchemy provides the Composite pattern here.
  • Serialized LOB – Sometimes you just want to stuff all the objects into a BLOB. Use the PickleType or roll a JSON type.
  • Inheritance Mappers – Represent class hierarchies within database tables. See Inheritance Mapping.
  • Optimistic Offline Lock – Set up a version id on your mapping to enable this feature in SQLAlchemy.
  • 数据映射器 –此模式的关键在于,对象关系映射以透明的方式应用于用户定义的类,从而将持久性细节与类的公共接口分开。 SQLAlchemy的经典映射系统是使用mapper()函数将一个类与表元数据链接在一起,它尽可能充分地实现了这种模式。 在现代SQLAlchemy中,我们使用将表元数据与类声明结合在一起的Declarative模式作为使用mapper()的快捷方式,但是持久性API仍然是单独的。
  • 工作单元 –这种模式是系统透明地跟踪对象更改并定期将所有那些未决的更改刷新到数据库中的地方。 SQLAlchemy的Session以类似于Hibernate的方式完全实现了这种模式。
  • 身份映射 –这是一种基本模式,可基于数据库身份为特定会话中的每个对象建立唯一身份。 没有ORM应该没有此功能,因为使用这种模式,极大地简化了对象结构和具有最适度复杂性的应用程序,并使其效率更高。
  • 元数据映射 –书中的这一章是“ 元数据 ”名称的来源。 与Fowler模式完全对应的是mapper()Table的组合。
  • 查询对象 – ORM 查询和Core select()构造均基于此模式。
  • 存储库 –就对象关系映射而言,充当数据库网关的接口。 这是SQLAlchemy 会话
  • 延迟加载 –根据需要加载相关的集合或对象。 与Hibernate一样,SQLAlchemy在属性加载方式方面有很多选择
  • 标识字段 –表示表行的主键(代表该行)。
  • 外键映射 –数据库外键使用对象模型中的关系表示。
  • 关联表映射 –可以映射一个类,该类表示有关两个对象如何相互关联的信息。 将关联对象用于此模式。
  • 嵌入值 –对象上的内联值表示多个列。 SQLAlchemy在此处提供了Composite模式。
  • 序列化LOB –有时您只想将所有对象填充到BLOB中。 使用PickleType或滚动JSON类型
  • 继承映射器 –表示数据库表中的类层次结构。 请参阅继承映射
  • 乐观离线锁定 –在映射上设置版本ID ,以在SQLAlchemy中启用此功能。

Thanks for reading!

谢谢阅读!

翻译自: https://www.pybloggers.com/2012/02/patterns-implemented-by-sqlalchemy/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值