配置类 - Guice 的最佳实践
背景:我正在使用 Google Guice,因此通过配置类更容易,但我认为这不是最好的方法。
我有一个存储一些路径的配置类:
class Configuration{
String getHomePath();
String getUserPath();
}
我还有一个需要“homepath”的类“a”和一个需要“userpath”的类“b”。
通过a类和b类的构造函数传递配置类好还是只通过特定路径传递好?
Background: I'm using Google Guice and so it's easier to pass through the configuration class but I think this is not the best way.
I have a configuration class which stores some paths:
class Configuration{
String getHomePath();
String getUserPath();
}
Also I have a class "a" which needs the "homepath" and a class "b" which needs the "userpath".
Is it better to pass the configuration class through the constructor of class a and b or only pass through the specific path?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您确实正确使用 Guice,那么您的所有配置都应该出现在模块的
configure
方法中。所以:HomePath
和UserPath
。getHomePath()
将其替换为名为 homePath 的字符串字段成员。getUserPath()
时,将其替换为名为 userPath 的字符串字段成员。@Inject
注释(应该已经是),并接受一个String参数,分别用@HomePath
和@UserPath<注释/code> 并分配注入值的字符串字段成员。
.annotatedWith()
在模块的配置方法中创建绑定 定义正确的值;如果它们仅在运行时可用,请绑定提供者。EG
如果创建注释对您来说工作量太大,请使用 @Named 注释 Guice 附带。
If you're really using Guice correctly all your configuration like this should appear in modules'
configure
method. So:HomePath
andUserPath
.getHomePath()
replace that with a String field member named homePath.getUserPath()
replace that with a String field member named userPath.@Inject
annotated (should already be) and take in a String parameter, respectively annotated with@HomePath
and@UserPath
and assign the String field member that injected value..annotatedWith()
which define correct values; if they're only available at run time, bind a provider.E.G.
If creating annotations is too much work for you, use the @Named annotation Guice ships with.
您的问题没有单一的答案,只有根据您的具体情况进行选择的选项。
如果您知道您的
Configuration
类将会增长并且如果您的A
和B
类可能会使用更多信息,然后将整个Configuration
对象传递给其构造函数。注意:我知道这违反了 YAGNI 原则,但有时你可能知道你会需要它;-)否则,你可以考虑使用
@Named
注入路径,以便减少A
和B
类的依赖性降至最低,这是一个很好的设计实践。There's no single answer to your question, there are only options to choose from, based on your specific situation.
If you know your
Configuration
class is going to grow AND if it's likely for yourA
andB
classes will use more from it, then pass the wholeConfiguration
object to their constructors. NB: I know this is against the YAGNI principle but sometimes you may know you're gonna need it ;-)Otherwise, you can consider using
@Named
injection of your paths so that you reduceA
andB
classes dependencies to their minimum, which is a good design practice.一般规则是使依赖图(哪些类了解或依赖于其他类/接口)的代码尽可能简单、规则和固定。
如果不传递 Configuration 类会使 a 或 b 对用户编写的类具有零依赖性,或者有必要避免依赖性循环,则使用单独的路径字符串。否则,如果“这个类可以以将来可能改变的方式访问配置信息”更有意义,则传递该类。
我会避免使用单例方法,特别是如果您已经设置了 Guice。
The general rule is code to make the dependency graph (which classes know about or depend on other classes/ interfaces) as simple, regular and fixed as possible.
If not passing the Configuration class makes a or b have zero dependencies on on user-written classes, or is necessary to avoid a dependency loop, then use the individual path strings. Otherwise, if it makes more sense to say 'this class has access to configuration info, in a way that may change in the future', pass the class.
I'd avoid the singleton approach, especially if you already have Guice set up.