隐藏类是 Active Record 的事实

发布于 2024-08-16 05:21:42 字数 273 浏览 1 评论 0原文

我正在使用 Castle ActiveRecord,但将其包装在我自己的持久层中,因为我想从应用程序代码中隐藏这一事实。但是,我的实体都继承自 ActiveRecordBase,因此我选择的 ORM 会泄漏到应用程序。我特别不喜欢的是大量暴露我的实体的方法。

如何在不继承 ActiveRecordBase 的情况下使用 ActiveRecord?

我知道抽象的某些部分会泄漏,因为我的实体是用 ActiveRecord 属性装饰的,但是我认为这部分没有意义。

I'm using Castle ActiveRecord, but wrapping it in my own persistence layer, because I want to hide this fact from application code. However, my entities all inherit from ActiveRecordBase<T>, so my choice of ORM is leaking to the application. What I especially don't like is the slew of methods this exposes off my entities.

How can I use ActiveRecord without inheriting from ActiveRecordBase?

I know some part of the abstraction will leak because my entities are decorated with ActiveRecord attributes, however I don't consider this part meaningful.

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

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

发布评论

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

评论(3

芸娘子的小脾气 2024-08-23 05:21:42

使用 Castle ActiveRecord 时,您的实体不必从基类继承。您可以改用 ActiveRecordMediator。也许手册中的这篇文章会有所帮助。

When using Castle ActiveRecord your entities do not have to inherit from the base class. You can use ActiveRecordMediator<T> instead. Perhaps this article from the manual will help.

×眷恋的温暖 2024-08-23 05:21:42

您可以使用组合而不是继承 - 您的业务对象基本上包装了活动记录对象并将持久性职责委托给它。

You can use composition instead of inheritance - your business object basically wraps your active record object and delegates persistence responsibilities to it.

梦旅人picnic 2024-08-23 05:21:42

如果您只使用普通的 NHibernate,则不必使用装饰器等,您只需要汇总自己的 hbm.xml 文件,这些文件非常简单,并且有很多工具可以提供帮助。

请注意,虽然这并不能直接回答你的问题,但我把它放在那里是因为使用你自己的 NHibernate 抽象并很好地学习 NH 是一项很棒的技能,一旦你投入时间,你将永远不会再使用 AR(嗯,除非它是一个小项目,你想要快速推出)。

我有自己的自定义 NH 抽象层,您可以在我的博客上找到一些代码示例等:
http://www.picnet.com.au/blogs/Guido/post/2009/07/08/Code-Generate-DAL-%28Data-Access-Layer%29- using-ORM-Article-1.aspx

虽然这可能有些夸张,但它确实向您展示了对 NH 的良好理解可以让您在未来做些什么。

吉多

If you use just plain NHibernate you dont have to use decorators, etc you just need to roll up your own hbm.xml files which are very simple and there are plenty of tools out there to help.

Note, whilst this does not answer your question directly I put it out there because using your own NHibernate abstraction and learning NH well is a great skill and once you invest the time you will never use AR again (Well, unless its a small project you want to roll out quickly).

I have my own custom NH abstraction layer, you can find some code samples etc on my blog here:
http://www.picnet.com.au/blogs/Guido/post/2009/07/08/Code-Generated-DAL-%28Data-Access-Layer%29-using-ORM-Article-1.aspx

Whilst this is probably over the top it does show you what a good understanding of NH can allow you to do in the future.

Guido

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