创建与 NHibernate 和 EF 4.1 兼容的 POCO 类

发布于 2024-11-13 21:30:32 字数 226 浏览 8 评论 0原文

我知道NHibernate的POCO类的所有成员都必须定义为virtual,而在EF(代码优先)中,您只需将集合/引用对象设置为virtual以进行延迟加载。

因此,如果我想创建与 EF 和 NH 兼容的 POCO 对象,我是否应该将 POCO 类的每个成员声明为虚拟成员?

想要这个的原因是我们目前正在评估 EF 和 NHibernate ORM,如果我们最终改变我们的计划,我们不想更新我们的 POCO

I know that all members of a POCO class for NHibernate must be defined as virtual, and in EF (code first), you only need to set the collection / reference objects as virtual for lazy loading.

So if I want to create POCO objects that are compatible with both EF and NH should I just declare every member of my POCO classes as virtual??

The reason for wanting this is that we're currently evaluating both EF and NHibernate ORM's and if we end up changing our plans down the road, we don't want to have to update our POCO's

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

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

发布评论

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

评论(3

乖不如嘢 2024-11-20 21:30:32

EF 需要虚拟导航属性来进行延迟加载,但它也使用虚拟标量/复杂属性来进行动态更改跟踪,因此如果将所有属性标记为虚拟,这将是正确的方法。

不管怎样,你先评估一下。如果您稍后决定更改 ORM,virtual 属性将是最小的问题 - 您会发现更困难的问题。

EF needs virtual navigation properties for lazy loading but it also uses virtual scalar/complex properties for dynamic change tracking so if you mark all properties as virtual it will be correct approach.

Anyway do you evaluation upfront. virtual properties will be minimal issue if you decide to change ORM later - you will find much harder problems.

北陌 2024-11-20 21:30:32

实际上,您不必将新罕布什尔州的所有财产都声明为虚拟财产。如果关闭延迟加载行为(默认为“打开”),则不再需要声明所有这些“虚拟”,并且可以根据具体情况指定延迟加载的属性。

因此,如果您愿意,您可以拥有一个仅包含 NH 和 EF 中虚拟引用对象的 POCO。

旁注:无论如何,只是为了它的努力,我将声明所有属性为虚拟并完成它:)

Actually you're not forced to declare every property as virtual in NH. If you turn-off the lazy load behaviour ('on' by default) you no longer need to declare all those "virtuals", and may specify case-by-case which properties are lazy-loaded.

So, if you want, you may have a POCO with just reference objects as virtual in both NH and EF.

Sidenote: Anyway, and just for the effort of it, I would declare all properties as virtual and be done with it :)

恰似旧人归 2024-11-20 21:30:32

或者,如果您为 POCO 创建接口或抽象基础,则可以使用 NHibernate 中的 属性,这也消除了需要对于虚拟属性,因为 NH 会推迟从代理而不是直接从 POCO 创建其子类。

Or alternatively, if you create an interface or abstract base for your POCO's, you can use the <class name="MyPoco" proxy="IMyPoco"/> attribute in NHibernate which also eliminates the need for virtual properties, since NH then defers to creating its subclasses from the proxy rather than from your POCO directly.

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