您能将 Fluent NHibernate 与 xml 配置映射文件进行比较吗?
如果我需要在 Fluent NHibernate 和 NHibernate 标准 xml 映射文件之间进行选择,我应该考虑什么?
您能比较一下我应该注意的显着差异吗?
What should I take into consideration if I need to choose between Fluent NHibernate and standard xml mapping files of NHibernate?
Can you compare the prominent differences that I should be aware of?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有几件事,正如前面提到的,这可能是一个重复的问题,我能想到的主要问题是,您可以在不重新编译代码的情况下更改 xml 文件,但您不能在不重新编译某些内容的情况下更改流利的映射,因为它们本质上是编译的。
也就是说,您可以拥有一个映射程序集,然后对其进行修补。显然,您所做的任何更改都需要通过集成测试来运行。
除此之外,Fluent Nhibernate 可能会让 NHibernate 用户感到困惑,因为他们将一些术语与自己的术语混合在一起,所以我总是说你应该从 XML 文件映射开始,直到你完全理解发生了什么,然后如果你不需要更改无需重新编译的映射,因为它读起来更好。
但是,不要忘记,您将在顶部添加一个抽象层,该抽象层具有自己的怪癖,这可以进行有趣的调试。
Several things and as mentioned this is possibly a duplicate question the main one I can think of is that you can change xml files without a code recompile but you cannot change mappings in fluent without recompiling something because they are by their nature compiled.
That said you can have a mappings assembly and then just patch that. Obviously whatever changes you make then need to be run through integration tests.
Aside from that Fluent Nhibernate can be confusing to NHibernate users because they mix some terms with their own so I always say you should start with XML file mappings until you totally understand whats going on then migrate to Fluent if you don't need to change the mappings without recompilation as it just reads better.
However don't forget you are then adding a layer of abstraction over the top with its own quirks which can make for interesting debugging.
注意:这更多的是一个意见答案。
实际上您错过了第三个选项。 NHibernate 3.2 附带了它自己的代码映射语法。
http://nhforge.org/blogs/nhibernate/archive/2011/09/05/using-nh3-2-mapping-by-code-for-automatic-mapping.aspx
个人我不喜欢这种语法,我是一个 Fluent NHibernate 粉丝,它对我有用。
我认为唯一真正需要考虑的是 krystan 已经提到的,即命名差异。
我个人对 XML 映射认输了,直到 Fluent NHibernate 出现才再次选择 NHibernate。
我认为您不需要了解有关 XML 映射的任何信息,如果您想学习 XML 映射,您可以导出 Fluent 映射并查看它们。但实际上 Fluent 映射非常简单。
Fluent NHibernate 的大多数问题都源于尝试使用自动映射,它映射了很多假设,如果您计划生成数据库模式,那很好,但如果您有现有模式,最好显式映射它们。我总是明确地写下我的地图。
Note: This is more of an opinion answer.
There's actually a 3rd option which you missed. NHibernate 3.2 shipped with it's own code mapping syntax.
http://nhforge.org/blogs/nhibernate/archive/2011/09/05/using-nh3-2-mapping-by-code-for-automatic-mapping.aspx
Personally I don't like the syntax, I'm a Fluent NHibernate fan and it works for me.
I think the only real thing to consider is what krystan already mentioned, which is the naming differences.
I personally threw in the towel with the XML mappings, and didn't pick NHibernate up again until Fluent NHibernate came out.
I don't believe you need to know anything about XML mappings, if you want to learn the XML mappings you can export Fluent mappings and look at them. But really the Fluent Mappings are dead simple.
Most problems from Fluent NHibernate steam from attempting to use Auto Mapping which maps a lot of assumptions, it's fine if you're planning on generating the DB schema, but if you've got an existing schema it's best to explicitly map them. I always explicitly write my maps tho.