POCO、Trackable Entities 和 EntityObjects 有何异同
尽管我有一些 .net o/rm 概念,但我对 EF4 术语中的三个术语感到困惑:可跟踪实体、EntityObjects 和 POCO。
1)你能说出他们的区别吗?
2)你能说出他们的相似之处吗?
3)什么时候应该使用它们?
4)什么时候不应该使用它们?
5)是否可以在一个项目中混合它们的组合?(即假设您已经编写了很多管理EntityObjects的代码,实现POCO是否容易?)
谢谢
Although I have some .net o/rm notions, I'm confused about three terms in EF4 parlance: Trackable entities, EntityObjects and POCOs.
1) Could you state their differences?
2) Could you state their similarities?
3) When should them be used?
4) When should them not be used?
5) Is it possible to mix combinations of them in a project?, (i.e. suppose that you have already a lot of code written to manage EntityObjects, is it easy to implement POCOs?)
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让我尝试一下:
EntityObject 是实体框架基类;默认情况下,当您创建新的实体框架模型时,这是表示您的表的所有类将继承的类。它包含执行所有 EF 魔法所需的所有逻辑和代码。
POCO 是普通旧式 CLR 对象 - 只是您拥有的普通对象,不依赖于 EF,只是普通类
可跟踪实体基本上是“立体类上的 POCO” - POCO 类具有跟踪其状态的附加功能(未修改、修改等)因此它们可以跨多个层发送(并返回),并且最终几乎像常规 EntityObjects 一样使用
。最容易使用的是 EntityObject 后代 - 您只需使用它们,一切都很棒并且有效。但是:这样做会将您与实体框架紧密结合,这可能是一个架构问题。
仅使用 POCO 是“最纯粹的”——您只处理普通类,EF 在“幕后”完成其所有魔力——但需要一些额外的代码和精力才能使其正常工作。
可追踪(或自追踪)实体似乎是两者之间的一个很好的妥协,但我还没有足够了解所有的机制和内部运作方式,还无法对这些提供任何合理的建议。
因此,我建议:
从常规实体框架数据模型、可视化设计器等开始,并使用 EntityObject 后代,然后开始使用 EF 及其工作原理
如果您觉得有必要,请在对 EF 有基本了解后更详细地探索 POCO 或 STE(自我跟踪实体)
Let me try:
EntityObject is a Entity Framework base class; by default, when you create a new Entity Framework model, this is the class all the classes representing your tables will inherit from. It contains all the logic and code needed to do all the EF magic.
POCO are Plain Old CLR Objects - just plain objects you have, no dependency on EF, just plain classes
Trackable Entities are basically "POCOs on stereoids" - POCO classes that have additional functionality to track their state (unmodified, modified etc.) so they can be sent across several tiers (and back) and be used almost like regular EntityObjects in the end
The easiest to use are EntityObject descendant - you just use them, everything great and works. However: doing this tightly binds you to Entity Framework, and this can be an architectural problem.
Using only POCOs is the "purest" - you're dealing with plain classes only, and EF does all its magic "behind the scenes" - but it takes a bit of additional code and effort to get this to work fine.
Trackable (or self-tracking) entities seems like a great compromise between the two, but I haven't had enough exposure to all the mechanics and inner workings yet to be able to give any reasonable recommendation on those.
So I would recommend:
start with regular Entity Framework data model, the visual designer and all, and use EntityObject descendants and just get into using EF and how it works
if you feel the need, explore POCO or STE (self-tracking entities) in more detail when you have a basic understanding of EF
以下是有关 EntityObject、可跟踪实体和 POCO 之间差异的一些附加信息。
1) EntityObject 是用于旧版 EF 应用程序的实体基类。您应该不惜一切代价避免它,因为它将您的实体与 EF 紧密耦合,这是一件坏事。而有了 EF POCO 支持,就完全没有必要了,因为 EF 现在提供了许多相同的功能,而无需基类。
2) POCO 类不了解持久性问题。它们不需要从基类派生或具有特殊属性。它们独立于数据访问 API,例如 EF。
3) 可跟踪实体是一种 POCO 类,具有一个或两个用于设置实体状态的附加属性: 未更改,添加,修改,删除。虽然这些属性通常与持久性有关,但它们不将实体耦合到任何特定的持久性框架,包括可跟踪实体。相反,它们使后端的某些持久性框架能够解释和应用这些属性,这些属性非常轻量级且独立于框架。
可跟踪实体的主要优点是,它是 Microsoft 现已不存在的 自我跟踪实体,它允许您通过与 WCF 或 Web API 服务的单次往返,在一个事务中持久保存对相关对象图的更改。例如,如果您的订单包含多个详细信息,其中一些已添加、修改或删除,则可以一次性发送订单和详细信息,并且所有更新都会自动进行。
可追踪实体还有其他好处,特别是在生产力方面。它作为一组 NuGet 包和 Visual Studio 扩展(2012、2013),让您可以在很短的时间内组合出一个 N 层解决方案。
干杯,
托尼
Here is some additional info on the differences between EntityObject, Trackable Entities, and POCO's.
1) EntityObject is an entity base class used for legacy EF apps. You should avoid it at all costs, as it tightly couples your entities to EF, which is a bad thing. And with EF POCO support, it is completely unnecessary, because EF now provides many of the same features without the base class.
2) POCO classes are ignorant of persistence concerns. They do not need to derive from a base class or have special attributes. They provide independence from the data access API, such as EF.
3) Trackable Entities are a type of POCO classes that have one or two additional properties for setting the entity state: Unchanged, Added, Modified, Deleted. While these properties do pertain to persistence in general, they do not couple entities to any particular persistence framework, including Trackable Entities. Rather, they enable some persistence framework on the back end to interpret and apply those properties, which are extremely lightweight and framework independent.
The main advantage of Trackable Entities, which is a replacement for Microsoft's now defunct Self-Tracking Entities, is that it allows you to persist changes to a graph of related objects in one transaction with a single round trip to a WCF or Web API service. For example, if you have an Order with multiple details, some of which are added, modified or deleted, the Order and details can be sent in one shot, and all the updates take place atomically.
Trackable Entities has other benefits as well, especially in terms of productivity. It is implemented as a set of NuGet packages and Visual Studio extensions (2012, 2013) that let you put together an N-Tier solution in a fraction of the time.
Cheers,
Tony