究竟什么是“坚持无知"? [英] What exactly is "persistence ignorance"?

查看:36
本文介绍了究竟什么是“坚持无知"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

持久性无知通常被定义为持久性和持续性的能力.检索标准的 .NET 对象(或 POCO,如果你真的坚持给它们一个名字).并且看似广为接受的标准 .NET 对象定义是:

Persistence ignorance is typically defined as the ability to persist & retrieve standard .NET objects (or POCOs if you really insist on giving them a name). And a seemingly well accepted definition of a standard .NET object is:

...普通课程,您专注于手头的业务问题,而不会出于与基础架构相关的原因添加内容..."

"...ordinary classes where you focus on the business problem at hand without adding stuff for infrastructure-related reasons..."

然而,我看到人们将 NHibernate 描述为一个允许持久性无知的框架,然而它是一个不能在任何标准 .NET 对象上工作的框架,只能在遵守特定的标准 .NET 对象上工作设计要求,例如(source):

However, I see people describing NHibernate as a framework that allows persistence ignorance, and yet it is a framework that cannot work on any standard .NET object, only standard .NET objects that adhere to particular design requirements, for example (source):

  • 所有类必须有一个默认构造函数
  • 除非类被解封并且所有成员都是虚拟的,否则某些功能将无法使用
  • 除非您滥用 Equals/GetHashCode,否则对象标识无法正常工作

(旁白:在任何人生气之前,我并不是要在这里挑 NHibernate,它只是一个经常被引用的框架示例,据说允许持久性无知.我相信类似的论点可以应用于其他声明相同的 ORM.)

现在,虽然类本身没有任何持久性框架特定的属性或基类等,但对我来说,它并不是真正的无知持久性",因为它必须遵循一组设计指南来促进 由选定的持久性框架使用.您必须牢记持久性框架的要求来设计和实现该类;如果您对此一无所知,该课程可能无法使用它.

Now although the class in itself does not have any persistence-framework-specific attributes or base classes etc., to me it is not really "persistence ignorant" because it must follow a set of design guidelines to facilitate use by the chosen persistence framework. You must design and implement the class with the requirements of the persistence framework in mind; if you are ignorant of it the class may not work with it.

我对持久性无知"/POCO"的定义有疑问的地方在于,我不明白从概念上讲,这与添加诸如 [Serializable] 之类的属性有何不同?code> 或 [DataContract][XmlType] 或任何其他持久性框架特定的注释,促进实体的持久性和检索使用那个框架.

Where I'm having trouble with the definition of "persistence ignorance"/"POCO" is that I don't see how, conceptually, this is really any different to adding attributes such as [Serializable] or [DataContract] or [XmlType] or any other persistence-framework-specific annotations that facilitate the persistence and retrieval of the entity using that framework.

那么,究竟什么是持久性无知"?

So, what exactly is "persistence ignorance"?

很明显,将其定义为能够持久化普通类"是一个谬论,因为 NHibernate 的那些只是在不引用特定于框架的类方面是普通的,而它们是非凡的,因为它们需要不寻常的设计选择,例如默认构造函数和全虚拟成员以及可变类型的 Equals/GetHashCode 实现.

Clearly the definition of it as being able to persist "ordinary classes" is a fallacy because the NHibernate ones are only ordinary insofar as not referencing framework-specific classes, whereas they are extraordinary inasmuch as they require unusual design choices such as default constructors and all-virtual members and Equals/GetHashCode implementations on mutable types.

因此,当对象促进持久性框架的使用(在设计和结构中或通过使用特定于框架的注释)但本身不执行任何持久性逻辑时,是否可以说持久性无知"是正确的?

Is it therefore reasonable to say that "persistence ignorance" is true when objects facilitate the use of a persistence framework (either in design and structure or by use of framework-specific annotations) but do not perform any persistence logic themselves?

推荐答案

我认为,像大多数事情一样,它是一个滑动比例.有些东西我们想要拥有持久性.一方面,这个东西拥有所有的胆量、依赖项和代码,这些代码都是定制的,可以以特定的方式坚持这件事.在天平的另一端是神奇地发生的事情,所有这些都不需要我们做更多的事情,而不仅仅是添加一个令牌或在某处设置一个属性来导致该事情只是持续存在".为了达到规模的神奇一面,有一些框架、设计指南、约定等可以帮助神奇的发生.我认为您可能会争辩说,可以生产出比 NHibernate 具有更少要求和限制但追求相同目标的工具;该假设工具将进一步符合我们的规模.

I would claim that, like most things, its a sliding scale. There are things that we make that want to have the property of persistence. On one end of the scale is this thing having all of the guts, dependencies, and code that is custom built to persist just this one thing in its particular way. On the other end of the scale is something that just magically happens, all without us doing much more than adding a token or setting a property somewhere that causes that thing to 'just persist'. In order to get to the magical side of the scale, there are frameworks, design guidelines, conventions, etc that assist the magic in happening. I think you could argue that a tool could be produced that had fewer requirements and restrictions than NHibernate but pursued the same goal; that hypothetical tool would be further along our scale.

我不知道我这么喜欢坚持无知"这个词;它实际上是关于一个对象不知道实现、后备存储、缓存之类的东西——一个对象通常知道它是否是持久的.但这只是语义.

I don't know that I like the term 'persistence ignorance' so much; its really about an object being ignorant of the implementation, the backing store, the cache, that sort of thing - an object is typically aware of whether or not it is persistent, though. But that's just semantics.

这篇关于究竟什么是“坚持无知"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆