使用 NHibernate 和 Codesmith 生成 ORM
刚刚开始学习NHiberate。 为每个数据库表编写正确的 hbm.xml 配置文件似乎相当乏味。
问题是 NHibernate 能否读入表并自动检索关系和所需的配置文件以及数据库映射器类? 看来程序员一开始还是要做很多基础工作。 然后我遇到了Codesmith,它有NHiberate的模板? 它填补了空白吗?
Just started learning NHiberate. It seems rather tedious to write proper hbm.xml config files for each database table.
The question is can NHibernate read into tables and automatically retrieve relationships and needed config file as well as database mapper classes for you? It seems that the programmer still has to do a lot of ground work initially. Then I came across Codesmith which got templates for NHiberate? Does it fill the gap?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用代码模板,但这仍然会带来痛苦。 最好的方法是使用 Fluent nHibernate。 他们有一个新的自动映射器功能,该功能按惯例使用配置。 没有映射文件,它只是假设您使用相似的名称,并且可以定义如何将实体名称转换为表名称的规则。
You can use code templates, but this still creates a pain IMHO. The best way is to use Fluent nHibernate. They have a new auto-mapper feature, that uses configuration by convention. No mapping files, it just makes assumptions that you use similar names and can define rules of how to convert an entity name to a table name.
代码生成可以很好地填补这一空白,我开始使用 CodeSmith,但后来他们将其作为付费项目,所以我转移到 MyGeneration< /a>. 我的方法是首先攻击数据库,代码生成映射文件,然后生成类(如果它们尚不存在)。 这使得事情进展得非常快,非常适合原型和创建测试数据。
然后通常手动更改映射文件、类和数据库。 在某些情况下,我尝试过让代码生成作为 CI 的一部分来工作,但事实证明很难做到这一点,除非您严格遵守手工代码更改的部分。 部分类是有帮助的,但仍然是让整个团队在同一页面上的一个问题,没有人喜欢他们的代码被工具覆盖,即使你可以回滚和合并。
Code generation can fill that gap really well, I started using CodeSmith, but then they made that a pay item so I moved to MyGeneration. My approach is to attack the DB first, code gen the Mapping files, and gen classes if they don't already exist. That gets something going really quickly, great for a prototype and creating test data.
The mapping files, classes and DB are often then changed by hand. In some instances I've tried getting the code generation to work as part of CI - but it's proven hard to do that, unless you're disciplined about where you part you're hand crafted code changes. Partial classes were a help, but still a prob to get the whole team on the same page, no-one likes to have their code overwritten by a tool, even if you can then rollback and merge.
我为 CodeSmith 工作...请记住,MyGeneration 尚未永远更新,这意味着它也缺乏支持。 CodeSmith 完全支持其产品和模板。 这意味着任何模板错误也将由 Codesmith 修复和验证。
谢谢
-布莱克·尼米斯基
I work for CodeSmith... Please keep in mind that MyGeneration hasn't been updated in forever which means it lacks support too. CodeSmith fully supports its products and templates. This means any template bugs will also be fixed and verified by Codesmith.
Thanks
-Blake Niemyjski