简体   繁体   English

什么是使用Castle Active Record和Straight NHibernate的利弊?

[英]Whats the pros and cons of using Castle Active Record vs Straight NHibernate?

Assuming that writing nhibernate mapping files is not a big issue....or polluting your domain objects with attributes is not a big issue either.... 假设编写nhibernate映射文件不是一个大问题....或者使用属性污染域对象也不是一个大问题....

what are the pros and cons? 优缺点都有什么?

is there any fundamental technical issues? 有什么基本的技术问题吗? What tends to influence peoples choice? 什么往往会影响人们的选择?

not quite sure what all the tradeoffs are. 不太清楚所有的权衡是什么。

The biggest pro of AR is that it gives you a ready-made repository and takes care of session management for you. AR最大的优势在于它为您提供了现成的存储库,并为您提供会话管理。 Either of ActiveRecordBase<T> and ActiveRecordMediator<T> are a gift that you would have ended up assembling yourself under NHibernate. ActiveRecordBase<T>ActiveRecordMediator<T>中的任何一个都是你最终在NHibernate下自己组装的礼物。 Avoiding the XML mapping is another plus. 避免XML映射是另一个优点。 The AR mapping attributes are simple to use, yet flexible enough to map even fairly 'legacy' databases. AR映射属性易于使用,但又足够灵活,甚至可以映射相当“遗留”的数据库。

The biggest con of AR is that it actively encourages you to think incorrectly about NHibernate. AR的最大特点是它积极鼓励你错误地思考NHibernate。 That is, because the default session management is session-per-call, you get used to the idea that persisted objects are disconnected and have to be Save() d when changes happen. 也就是说,因为默认会话管理是每次调用会话,所以您会习惯于持久化对象已断开连接,并且在发生更改时必须为Save() d。 This is not how NHibernate is supposed to work - normally you'd have session-per-unit-of-work or request or thread, and objects would remain connected for the lifecycle of the session, so changes get persisted automatically. 这不是NHibernate应该如何工作的 - 通常你有每个工作单元的会话或请求或线程,并且对象在会话的生命周期中保持连接,因此更改会自动保持。 If you start off using AR and then figure out you need to switch to session-per-request to make lazy loading work - which is not well explained in the docs - you'll get a nasty surprise when an object you weren't expecting to get saved does when the session flushes. 如果你开始使用AR,然后弄清楚你需要切换到每个请求的会话以使延迟加载工作 - 这在文档中没有很好地解释 - 当你没有期待的对象时,你会得到一个令人讨厌的惊喜当会话刷新时保存。

Bear in mind that the Castle team wrote AR as a complementary product for Castle Monorail, which is a Rails-like framework for .NET. 请记住,Castle团队将AR作为Castle Monorail的补充产品,这是一个类似于Rails的.NET框架。 It was designed with this sort of use in mind. 它的设计考虑到了这种用途。 It doesn't adapt well to a more layered, decoupled design. 它不能很好地适应更分层,分离的设计。

Use it for what it is, but don't think about it as a shortcut to NHibernate. 使用它是什么,但不要把它当作NHibernate的快捷方式。 If you want to use NH but avoid mapping files, use NHibernate Attributes or better, Fluent NHibernate. 如果你想使用NH但是避免映射文件,请使用NHibernate Attributes或更好的Fluent NHibernate。

I found ActiveRecord to be a good piece of kit, and very suitable for the small/medium projects I've used it for. 我发现ActiveRecord是一个很好的工具包,非常适合我用它的小/中项目。 Like Rails, it makes many important decisions for you, which has the effect of keeping you focused you on the meat of the problem. 像Rails一样,它为您做出了许多重要的决定,这可以让您专注于解决问题。

In my opinion pro's and cons are: 在我看来,职业和缺点是:

Pros 优点

  • Lets you focus on problem in hand, because many decisions are made for you. 让您专注于手头的问题,因为许多决定都是为您做出的。
  • Includes mature, very usable infrastructure classes (Repository, Validations etc) 包括成熟,非常有用的基础结构类(存储库,验证等)
  • Writing AR attributes are faster than writing XML or NHibernate.Mapping.Attributes IMHO. 编写AR属性比编写XML或NHibernate.Mapping.Attributes恕我直言更快。
  • Good documentation and community support 良好的文档和社区支持
  • It's fairly easy to use other NHibernate features with it. 使用其他NHibernate功能相当容易。
  • A safe start. 一个安全的开始。 You have a get-out clause. 你有一个get-out子句。 You can slowly back into a bespoke NHibernate solution if you hit walls with AR. 如果你用AR撞墙,你可以慢慢回到定制的NHibernate解决方案。
  • Great for domain-first development (generating the db). 非常适合域优先开发(生成数据库)。
  • You might also want to look up the benefits and drawbacks of the ActiveRecord pattern 您可能还想查找ActiveRecord模式优缺点

Cons 缺点

  • You can't pretend NHibernate isn't there - you still need to learn it. 你不能假装NHibernate不存在 - 你仍然需要学习它。
  • Might not be so productive if you already have a legacy database to work with. 如果您已经有一个遗留数据库可以使用,那么可能效率不高。
  • Not transparent persistence. 不透明的持久性。
  • In-built mappings are comprehensive, but for some projects you might need to revert to NHibernate mappings in places. 内置映射是全面的,但对于某些项目,您可能需要在某些地方恢复为NHibernate映射。 I haven't had this problem, but just a thought. 我没有遇到过这个问题,只是一个想法。

In general, I really like ActiveRecord and it's always been a time saver, mainly because I seem to happily accept the decisions and tools baked into the library, and subsequently spend more time focusing on the problem in hand. 总的来说,我真的很喜欢ActiveRecord,它总是节省时间,主要是因为我似乎乐于接受图书馆中的决策和工具,并且随后花更多的时间专注于手头的问题。

I'd give it a try on a few projects and see what you think. 我会尝试一些项目,看看你的想法。

When I started using NHibernate , I didn't learn about Castle ActiveRecord until I had written my Mapping files and made my classes. 当我开始使用NHibernate时 ,在我编写Mapping文件并制作课程之前,我没有学习Castle ActiveRecord。 At that point, I couldn't visibly discern what Castle Activerecord would give me, so I didn't use it. 那时,我无法辨别Castle Activerecord会给我什么,所以我没有使用它。

The second time I used NHibernate, I simply used myGeneration to make the mapping files and the classes just by having it look at my database. 我第二次使用NHibernate时,我只是使用myGeneration制作映射文件和类,只需查看我的数据库即可。 That saved a lot of time by itself, and allowed me to (once again) not worry about Castle Active Record. 这节省了很多时间,并允许我(再一次)不担心Castle Active Record。

In reality, most of your time is going to be spent making the custom queries, and Castle Active Record won't necessarily help with that -- if you were to use myGeneration with NHibernate, you'd bypass most of the work you'd need to do anyway. 实际上,大部分时间都花在制作自定义查询上,而Castle Active Record不一定有用 - 如果你将myGeneration与NHibernate一起使用,你将绕过大部分工作。无论如何都需要做。

Edit: I don't want to seem like a cheerleader for either myGeneration or NHibernate. 编辑:我不想看起来像myGeneration或NHibernate的拉拉队员。 I just use the tool that allows me to get my work done quickly and easily. 我只是使用这个工具,让我能够快速轻松地完成工作。 The less time I have to spend writing Data Access code, the better. 我花在编写数据访问代码上的时间越少越好。 It doesn't mean I can't do it -- but there's little sense in re-inventing the wheel each time you write a new application. 这并不意味着我无法做到 - 但每次编写新应用程序时重新发明轮子几乎没有意义。 Write SQL queries and Stored Procedures where needed, and no where else. 在需要的地方编写SQL查询和存储过程,而不是在其他地方编写。 If you're doing CRUD operations, an ORM is the way to go. 如果您正在进行CRUD操作,那么ORM就是您的选择。

Edit #2: Castle Active Record may bring more to the table than I realize -- I don't know much other than what's on their website , but if it does bring more to the table, then it would help potential adopters to be able to readily see that on their site. 编辑#2:城堡活动记录可能会带来比我意识到的更多 - 我不知道除了他们的网站上有什么 ,但如果它确实带来了更多的表,那么它将有助于潜在的采用者能够很容易在他们的网站上看到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM