gpt4 book ai didi

c# - 为什么 EF 4 中的 Persistence Ignorant POCO 需要 "Fixup"?

转载 作者:可可西里 更新时间:2023-11-01 07:46:00 29 4
gpt4 key购买 nike

Entity Framework 4 备受期待的功能之一是能够以无持久性的方式使用 POCO(普通旧 CLR 对象)(即,它们“不知道”它们是通过 Entity Framework 与一些其他机制)。

我正在努力思考为什么有必要在我的“普通”业务对象中执行关联修复并使用 FixupCollection。该要求似乎暗示业务对象毕竟不能完全忽略持久性机制(事实上,“修复”一词听起来像是需要修复/更改某些内容才能与所选的持久性机制一起工作)。

具体来说,我指的是由 ADO.NET POCO 实体生成器生成的 Association Fixup 区域,例如:

    #region Association Fixup

private void FixupImportFile(ImportFile previousValue)
{
if (previousValue != null && previousValue.Participants.Contains(this))
{
previousValue.Participants.Remove(this);
}

if (ImportFile != null)
{
if (!ImportFile.Participants.Contains(this))
{
ImportFile.Participants.Add(this);
}
if (ImportFileId != ImportFile.Id)
{
ImportFileId = ImportFile.Id;
}
}
}

#endregion

还有FixupCollection的使用。其他常见的不了解持久性的 ORM 没有类似的限制。

这是由于 EF 中的基本设计决策造成的吗?即使在更高版本的 EF 中,是否仍存在某种程度的非无知?有没有一种聪明的方法可以对 POCO 开发人员隐藏这种持久性依赖性?

这在实践中是如何实现的,端到端的?例如,我知道最近才添加了对 ObservableCollection 的支持(这是 Silverlight 和 WPF 所需要的)。与 EF 兼容的 POCO 对象的设计要求在其他软件层中是否存在问题?

最佳答案

找到了一些解释 - 检查它们!

POCO Template Code Generation Options (EF 团队博客)

Fixup

A fixup method is written for everynavigation property on an entity andis called from the setter of thenavigation property whenever its valuechanges. Its purpose is to ensure thateach end of a bidirectionalrelationship stays in sync with theother. For example, in a one-to-manyrelationship between Cutomer andOrder, whenever Order.Customer is set,the fixup method ensures that theOrder is in the Customer’s Orderscollection. It also keeps thecorresponding foreign key propertyviz. Order.CustomerID in sync with thenew Customer’s primary key (ID) value.This logic can be useful if the POCOentities are used independently of theEF stack, like for writing testsagainst them which don’t hit thedatabase. Fixup ensures that theobject graph is connected in the sameway as you would expect while usingthem with EF. Fixup methods are a bitcomplex to write and hence it isuseful to have them auto-generated ifyou are planning on using the entitiesin an EF independent scenario.

同时查看这个 POCO in the Entity Framework Part 1其中还有一些部分介绍了修复是什么以及它们需要做什么。

关于c# - 为什么 EF 4 中的 Persistence Ignorant POCO 需要 "Fixup"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2945278/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com