使用变质代码来减少样板代码

发布于 2024-09-12 17:21:28 字数 1149 浏览 12 评论 0原文

有没有人见过变质代码——即生成和运行指令的代码(包括 IL 和 Java 字节码,以及本机代码)——用于减少样板代码?

无论应用程序或语言如何,通常都有一些数据库代码来从数据库获取行并返回对象列表。当然,根据您的数据库连接器,有无数种方法可以做到这一点。您最终可能会通过索引访问该行的单元格(这很尴尬,因为将“SELECT Name, Age”更改为“SELECT Age, Name”会破坏您的代码,而且索引会混淆含义),或者使用 myObject.Age = resultRow。 getValue("Age") (很尴尬,因为这涉及简单地遍历每个字段以根据列设置其数据)。

与数据库主题保持一致,LINQ to SQL 非常棒。然而,定义数据模型就没那么棒了,尤其是当您的数据库有太多表以至于 SSMS 无法在对象浏览器中列出所有表时。另外,我不喜欢的不是存储过程的编写或 SQL 的参与;而是我不喜欢的。只是对象与数据库的连接。

我实习的公司里有人从我们的 SqlCommand 类(继承自 System 类)中编写了一个非常棒的方法,该方法使用 .NET 反射和 System.Reflection.Emit 来生成一个设置字段的方法(用包含列名称的属性)在任何具有空构造函数的模型对象上。我认为这种变形是因为程序的特定部分编写了新方法。

这种从数据库生成对象的模式只是一个示例。我两天前遇到的一个问题是对 SWT 的数据绑定支持(通过 JFace)。我使用 setAddress(Address address) 和 getName() 创建了这些完全干净的模型,现在我必须使用 PropertyChangeSupport fire-ers 污染 setter 并携带 PropertyChangeSupport 实例(即使它只是在抽象基类中)!然后我发现 PojoBindables 现在我感觉自己像一个 80 级的数据绑定程序,只是因为我需要编写较少的。

具体来说,使用本机代码的东西与这样的东西或 Java Agent 会非常好。

Has anyone seen metamorphic code -- that is, code that generates and runs instructions (including IL and Java Bytecode, as well as native code) -- used to reduce boilerplate code?

Regardless of the application or language, typically one has some database code to get rows from the database and return a list of objects. Of course, there are countless ways of doing this based on your database connector. You might end up accessing the cells of the row by index (awkward, because changing "SELECT Name, Age" to "SELECT Age, Name" would break your code, plus the indexes obfuscate meaning), or using myObject.Age = resultRow.getValue("Age") (awkward, because this involves simply going through every field to set its data based on the columns).

Keeping with the database theme, LINQ to SQL is awesome. However, defining data models is less awesome, especially when your database has so many tables that SSMS can't list all of them in the object browser. Also, it's not the stored procedure writing or the SQL involvement that I dislike; just the connection of objects to database.

Someone at the company at which I intern wrote a really awesome method from our SqlCommand class (which inherits from the System one) that uses .NET reflection, with System.Reflection.Emit, to generate a method that would set fields (decorated with an attribute containing the name of the column) on any model object with a nullary constructor. I would consider this metamorphic because a specific part of the program writes new methods.

This pattern of generating objects from the database is just one example. One which I came across two days ago was databinding support for SWT (via JFace). I made these perfectly clean models with setAddress(Address address) and getName() and now I have to pollute the setters with PropertyChangeSupport fire-ers and carry around a PropertyChangeSupport instance (even if it is just in an abstract base class)! Then I found PojoBindables and now I feel like a level 80 databinder, simply because I need to write less.

Specifically, things that use native code with something like this or a Java Agent would be really sweet.

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

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

发布评论

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

评论(1

£噩梦荏苒 2024-09-19 17:21:28

通用编程可能适合你。 Concept C++ 网站 有一个非常好的教程,涵盖了抽象和提升,以及以下想法:可以在任何语言中使用,并将样板代码转化为积极的力量。通过检查一堆几乎完全相同的样板方法,您可以得出一组在概念上统一代码的要求(“要使 X 发生,您必须执行 Y,因此要使 X1 发生,您必须执行 Y,但差异为 1”) 。从那里,您可以使用模板来捕获共性,并使用模板输入来指定差异。 C# 和 Java 目前有自己的泛型实现,因此可能值得一试。

Generic programming might up your alley. The Concept C++ website has a really good tutorial that covers abstraction and lifting, ideas that can be used in any language and turn boilerplate code into a positive force. By examining a bunch of boilerplate methods that are almost exactly the same, you can derive a set of requirements that unite the code conceptually ("To make X happen you must do Y, so make X1 happen you must do Y with difference 1"). From there you can use a template to capture the commonalities, and use the template inputs to specify the differences. C# and Java have their own generics implementations at this point, so it might be worth checking out.

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