代码生成是一件坏事吗? [英] Is Code Generation a Bad Thing?

查看:76
本文介绍了代码生成是一件坏事吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个主观问题.

最近,我一直在尝试学习NHibernate.问题在于至少2倍:

Recently I have been trying to learn NHibernate. The problem with it is at least 2 fold:

  1. 需要手动编码的映射xml文件
  2. 需要手动编码的实体类

虽然NHibernate是一个很棒的框架,但是编写映射文件和创建模仿数据库表的实体类的需求使编写过程非常麻烦.有一些工具可以缓解此问题,例如Castle ActiveRecord或NHibernate Fluent.

While NHibernate is a wonderful framework, but the need to write mapping file and to create entity classes that mimic the database tables are pain in neck to write. There are a few tools that can alleviate this problem, such as Castle ActiveRecord or NHibernate Fluent.

但是,尽管这些工具可以减少代码重复的问题,但仍然需要编写一些重复代码,或者必须添加一些装饰性属性.我现在正在尝试一些代码生成工具,该工具可让我直接从数据库生成映射文件和实体类.对我来说,这是一个更加省力的任务.

But as much as those tools can reduce code repeatation problem, there are still some duplication code that you have to write, or some decorative attributes that you have to put on. I am now trying some code generation tool that allows me to generate the mapping files and the entity classes straight from the database. To me this is a more effort saving task.

您怎么看?

推荐答案

通常来说,您不想在生成的代码生成后对其进行编辑(特别是如果代码生成器的输入很复杂且/或可能进行更改).

Generally speaking, you don't want to be editing generated code after it has been generated (particularly if the input to the code generator is complex and/or likely to change).

我发现使用代码生成作为构建过程的一部分通常是可以的(因此,生成的代码永远不受版本控制).

I find that it's usually OK to use code generation as part of a build process (so the generated code is never under version control).

我对一次性"代码生成(生成代码然后手动进行调整和扩展)的经验是,这是痛苦的根源.如果您想返回并更改生成代码的数据,则几乎总是不得不手动合并自定义更改...

My experience of "one-shot" code generation (where the code is generated and then tweaked and extended manually) is that it's a source of pain. If you ever want to go back and change the data from which the code was generated, you nearly always end up having to manually merge your custom changes...

这篇关于代码生成是一件坏事吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆