英文:
Using org.springframework.data.jdbc.core.convert.SqlGenerator in my own package throw composition design
问题
我目前正在开发一个项目,该项目利用Spring Data JDBC自定义仓库。在我的自定义仓库中,我已经实现了自己的DataAccessStrategy, AggregateRoot, AggregateExecutor和SqlParameterSource来生成SQL脚本。然而,我想要使用SqlGenerator类来生成SQL,但该类具有包私有访问修饰符。
为了解决这个问题,我计划通过在org.springframework.data.jdbc.core.convert中创建一个公共类,并使用SqlGenerator类型的私有最终字段来使用组合。
我的问题是:是否将包私有的SqlGenerator类的方法开放供我的库使用是一个好主意?
任何建议或建议将不胜感激!
英文:
I'm currently working on a project that utilizes Spring Data JDBC custom repository. In my custom repository, I have implemented my own DataAccessStrategy, AggregateRoot, AggregateExecutor, and SqlParameterSource to generate SQL scripts. However, I would like to use the SqlGenerator class to generate SQL, which has a package-private access modifier.
To get around this, I plan on using composition by creating a public class in org.springframework.data.jdbc.core.convert and using a private final field with the SqlGenerator type.
My question is: would it be a good idea to open up the methods of the package-private SqlGenerator class for use in my library?
Any advice or suggestions would be greatly appreciated!
答案1
得分: 1
是否打开包私有
SqlGenerator
类的方法供我的库使用是个好主意吗?
对于这个问题有几种不同的解释,但我认为答案总是:不。
-
Spring Data 团队是否应该开放这些方法?
不,他们不应该。将方法公开访问需要额外的维护和设计工作。
SqlGenerator
只是用于 Spring Data JDBC 内部重用的,不打算供外部重用。 -
我是否应该修改 Spring Data JDBC 的版本,以使这些方法可访问?
不,这个补丁需要重新应用到每个新版本,会在你这一侧创建大量的维护工作,可能会阻止你或你的团队升级到最新版本的 Spring Data JDBC,这可能会影响升级其他 Spring 版本和库的能力,最终可能会影响应用非常重要的安全问题升级的能力。
在一个不受你控制的包中创建类的整个想法是一种破解,我建议不要这样做。
相反,我建议将 SqlGenerator
复制到你的包中并与复制一起使用。这样,你可以完全控制一切,而且很明显是你的控制,因为它在你的源路径下。
另一种选择是使用反射来访问现有的类。
英文:
> would it be a good idea to open up the methods of the package-private SqlGenerator
class for use in my library?
There are a couple of different interpretations to that question, but I think the answer is always: No.
-
Should the Spring Data Team open up the methods?
No, they shouldn't. Making methods publicly accessible would require additional maintenance and design work.
SqlGenerator
simply isn't intended for reuse outside of Spring Data JDBC itself. -
Should I patch my version of Spring Data JDBC to make these method accessible?
No, the patch needs to be reapplied to every new version, creating significant maintenance work on your side and possibly preventing you or your team from upgrading to the latest version of Spring Data JDBC, which might affect the ability to upgrading other Spring versions and libraries and will eventually effect the ability to apply upgrades that are very important due to security issues.
The whole idea of creating a class in a package that isn't controlled by you is a hack and I would recommend against it.
Instead I'd just copy SqlGenerator
into your package and work with the copy. This way you have everything under you control and it is also obvious that it is your control since it is in your source path.
An alternative, would be to use reflection to access the existing class.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论