从域实体对象调用数据库

发布于 2024-11-09 00:18:29 字数 443 浏览 5 评论 0原文

我正在尝试在一个项目上使用 PetaPoco,而不是我通常使用的 NHIbernate,并且我有一个关于集合以及从域模型中使用它们的问题。

假设我有一个带有名称和文本的 BlogPost,然后带有名称、作者、文本的评论

我想将 BlogPost 与评论相关联。我已经使用 Blogposts_Comments 表在 SQL 中完成了此操作。在 NHibernate 中,我只在 BlogPost 上有一个 Comments 集合并对其进行映射,然后将其检索或标记为 Lazy。

我想我可能会延迟加载这些评论,因此为此,我会在博客文章 GetComments() 方法中调用我的数据访问对象 GetCommentsForBlogPost(this) 。我的问题是,这被认为是好的做法吗?让您的域实体调用数据访问层?在我的 NHibernate 项目中,它们只处理自己,就像 NHibernate 代理/魔法处理其余的事情一样。

I'm having a go at using PetaPoco on a project rather than NHIbernate which I normally do, and I have a question about collections and using them from a domain model.

Lets say I have a BlogPost with Name and Text, and then Comments with Name, Author, Text

I want to associate BlogPost with Comments. I've done this in SQL with a Blogposts_Comments table. In NHibernate I'd just have a Comments collection on BlogPost and map that, which would then be retrieved or marked as Lazy.

I am thinking I might Lazy Load these Comments, so to do that, I'd call my data access object GetCommentsForBlogPost(this) in a Blog Post GetComments() method. My question is, is this considered good practice? Having your domain entities call the data access layer? in my NHibernate projects they just dealt with themselves as the NHibernate proxies/magic did the rest.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可是我不能没有你 2024-11-16 00:18:29

在我看来,如果你这样做,你就不再拥有 POCO 对象..这是否是一件坏事真的取决于你。这实际上是保持域对​​象干净(可以说更易于维护)和让调用者的生活稍微轻松一点之间的权衡。

In my opinion, if you do this, you no longer have POCO objects.. whether or not that is a bad thing is really up to you. It's a tradeoff really between keeping your domain objects clean (arguably more maintainable), or making life only slightly easier for the caller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文