实体框架中的 POCO
我已经在实体框架中阅读过很多次 POCO 的概念,但我并不理解它。虽然我已经阅读了有关 POCO 的链接,但我仍然需要一个清晰的解释。
I've read the concept of POCO in Entity Framework so many times but I didn't understand it. Although I've read links about POCO, I still need a clear explanation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
POCO 只是意味着您的实体类没有任何持久性逻辑。这意味着,如果您有一个 Order 类,它将永远不会包含任何用于从数据库获取数据或将数据保存到数据库的方法。 POCO 上永远不会有 Order.GetById() 或 Order.Save() 方法。您也不能从包含持久性逻辑的基类继承(这就是 EF1 失败的地方)。
您的实体类将具有一堆表示数据的属性,并且您可能会有一些验证方法,也许还有一些对订单数据进行操作的业务方法,但您不会有获取或保存数据的持久性方法。 POCO 架构中的持久性由单独的类(例如存储库或数据服务)处理。
如果您想更多地了解 POCO 是什么,我写了一篇博客文章,在这里给出了更长的解释 http://rlacovara.blogspot.com/2009/03/what-is-difference- Between-dto-and-poco.html。
您看到大量有关 POCO 和实体框架的文章的原因是 EF1 几乎不可能实现真正的 POCO 架构。许多关心 ORM 之类的开发人员希望使用 POCO 架构,所以这是一个大问题。通过 EF4,尤其是 EF4 CodeFirst,Microsoft 进行了大量更改,使 POCO 架构非常容易实现。
POCO just means that your entity classes don't have any persistence logic on them. That means that if you have an Order class it will never contain any methods that are used to get data from the db or save data to the db. You will never have an Order.GetById() or an Order.Save() method on a POCO. You also can't inherit from a base class that contains persistence logic (which is where EF1 fell down).
What your entity class will have is a bunch of properties that represent the data, and you'll probably have some validation methods, and maybe some business methods that operate on the order data, but you will not have persistence methods that get or save data. Persistence in a POCO architecture is handled by a separate class like a Repository or a DataService.
If you want more on what a POCO is I wrote a blog post that gives a longer explanation here http://rlacovara.blogspot.com/2009/03/what-is-difference-between-dto-and-poco.html.
The reason you see a lot written about POCO and Entity Framework is that EF1 made it almost impossible to implement a true POCO architecture. Many developers who care about things like ORMs want to use a POCO architecture so that was a big problem. With EF4 and especially EF4 CodeFirst Microsoft has made a lot of changes that make a POCO architecture pretty easy to implement.