POCO的说明 [英] Explanation of POCO

查看:132
本文介绍了POCO的说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人可以给出POCO(普通旧CLR对象)的可靠解释(带有示例).我在Wikipedia上找到了简短的解释,但实际上并没有给出可靠的解释.

I'm wondering if anyone can give a solid explanation (with example) of POCO (Plain Old CLR Object). I found a brief explanation on Wikipedia but it really doesn't give a solid explanation.

推荐答案

与其将它们称为 POCO ,不如将它们称为持久性无知对象.

Instead of calling them POCOs, I prefer to call them persistence ignorant objects.

因为他们的工作很简单,所以他们不必关心自己的用途或使用方式.

Because their job is simple, they don't need to care about what they are being used for or how they are being used.

就个人而言,我认为POCO只是具有简单属性的公共类的另一个流行语(例如Web 2.0-不要让我开始这样做).

Personally I think POCOs are just another buzzword (like Web 2.0 - don't get me started on that) for a public class with simple properties.

我一直在使用这些类型的对象来保持业务状态.

I've always been using these type of objects to hold onto business state.

当您开始使用诸如存储库模式,ORM和依赖项注入之类的东西时,POCO的主要好处是真正体现出来的.

The main benefits of POCOs are really seen when you start to use things like the repository pattern, ORMs and dependency injection.

换句话说,您可以创建一个ORM(例如EF),该ORM从某处(数据库,Web服务等)中提取数据,然后 project 变成对象(PO​​CO).

In other words - you could create an ORM (let's say EF) which pulls back data from somewhere (db, web service, etc), then project this data into objects (POCOs).

这些对象可以在应用程序堆栈中进一步传递到服务层,然后传递到Web层.

These objects can be passed further down the app stack to the service layer, then onto the web tier.

然后,如果有一天您决定切换到nHibernate,则完全不必触摸POCO,唯一需要更改的就是ORM.

Then if one day you decide to switch over to nHibernate, you should not have to touch your POCOs at all, the only thing that should need to be changed is the ORM.

因此有持久性无知"一词-他们不在乎它们的用途或用法.

Hence the term 'persistence ignorant' - they don't care what they're being used for or how they are being used.

总而言之,优点:

  • 允许简单的数据存储机制,简化序列化/层遍历
  • 与去污注射,存储库模式和ORM紧密结合.灵活性.
  • 最小化复杂性和对其他层的依赖. (较高的层仅关心POCO,POCO不关心任何东西).松耦合
  • 简单的可测试性(域测试无需存根).

希望有帮助.

这篇关于POCO的说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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