为什么" Fixup时"需要持久性无知POCO在EF 4? [英] Why is "Fixup" needed for Persistence Ignorant POCO's in EF 4?

查看:256
本文介绍了为什么" Fixup时"需要持久性无知POCO在EF 4?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个实体框架4的备受期待的特性是持久性无知的方式使用POCO(普通旧CLR对象)(即他们不知道他们正在坚持用实体框架与能力一些其他的机制)。

One of the much-anticipated features of Entity Framework 4 is the ability to use POCO (Plain Old CLR Objects) in a Persistence Ignorant manner (i.e. they don't "know" that they are being persisted with Entity Framework vs. some other mechanism).

我试图总结我的周围为什么它是需要进行关联的修正,在我的普通的业务对象使用FixupCollection头。这一要求似乎暗示业务对象不能完全无知的持久性机制毕竟(其实这个词修正听起来好像需要修复/改变与所选择的持久性机制工作的东西)。

I'm trying to wrap my head around why it's necessary to perform association fixups and use FixupCollection in my "plain" business object. That requirement seems to imply that the business object can't be completely ignorant of the persistence mechanism after all (in fact the word "fixup" sounds like something needs to be fixed/altered to work with the chosen persistence mechanism).

具体来说,我指的是年代由的 ADO.NET实体POCO发电机的,如所产生的协会Fixup时区:

Specifically I'm referring to the Association Fixup region that's generated by the ADO.NET POCO Entity Generator, e.g.:

    #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的。其他常见的持续性昧奥姆斯没有类似的限制。

as well as the use of FixupCollection. Other common persistence-ignorant ORMs don't have similar restrictions.

这是由于EF基本的设计决策?在这里非无知一定程度上保持甚至在EF的更高版本?有一个聪明的方法来隐藏从POCO开发这个持久性依赖?

Is this due to fundamental design decisions in EF? Is some level of non-ignorance here to stay even in later versions of EF? Is there a clever way to hide this persistence dependency from the POCO developer?

在实践中如何做到这一点的工作了,终端到终端?例如,我的理解是最近才增加了对的ObservableCollection(这是需要Silverlight和WPF)的支持。是否有其他软件层陷阱从EF-兼容POCO对象的设计要求。

How does this work out in practice, end-to-end? For example, I understand support was only recently added for ObservableCollection (which is needed for Silverlight and WPF). Are there gotchas in other software layers from the design requirements of EF-compatible POCO objects?

推荐答案

发现了一些解释 - 检查他们出来了!

Found a few explanations - check them out!

POCO模板代码生成选项(EF团队博客)

Fixup时

一个修正方法是在实体和

导航属性写入从
导航属性的setter方法​​被称为每当其价值
变化。的其目的是确保与其他的
保持​​同步该$ B $为b的双向
关系每一端。例如,在一到多$ B CUTOMER和
阶之间$ b的关系,每当Order.Customer设置,
的修正方法,确保
订单是客户的订单
收藏。它还保留了
对应的外键属性
即。 Order.CustomerID与
新客户的主键(ID)值同步。如果POCO
实体独立使用的
EF栈,就像对他们编写测试
不打
数据库
这种逻辑可能是有用的。修正确保
的对象图被连接在同一
的方式,你会同时使用
他们EF期待。修正的方法是有点
复合体来写,因此它是
有用的,让他们自动生成的,如果
你在使用实体
在EF独立的情景规划。

A fixup method is written for every navigation property on an entity and is called from the setter of the navigation property whenever its value changes. Its purpose is to ensure that each end of a bidirectional relationship stays in sync with the other. For example, in a one-to-many relationship between Cutomer and Order, whenever Order.Customer is set, the fixup method ensures that the Order is in the Customer’s Orders collection. It also keeps the corresponding foreign key property viz. Order.CustomerID in sync with the new Customer’s primary key (ID) value. This logic can be useful if the POCO entities are used independently of the EF stack, like for writing tests against them which don’t hit the database. Fixup ensures that the object graph is connected in the same way as you would expect while using them with EF. Fixup methods are a bit complex to write and hence it is useful to have them auto-generated if you are planning on using the entities in an EF independent scenario.

和还检查了这个的 POCO实体框架中第1部分其中也有对是什么的修正某些部分,他们在做什么所需要的。

And also check out this POCO in the Entity Framework Part 1 which also has some sections on what fixups are and what they're needed for.

这篇关于为什么" Fixup时"需要持久性无知POCO在EF 4?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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