配置类 - Guice 的最佳实践

发布于 2024-12-04 15:47:00 字数 268 浏览 1 评论 0原文

背景:我正在使用 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 技术交流群。

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

发布评论

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

评论(3

近箐 2024-12-11 15:47:00

如果您确实正确使用 Guice,那么您的所有配置都应该出现在模块的 configure 方法中。所以:

  1. 删除配置类。
  2. 创建注释类,可能称为 HomePathUserPath
  3. 其中类 a 使用 getHomePath() 将其替换为名为 homePath 的字符串字段成员。
  4. 当类 b 使用 getUserPath() 时,将其替换为名为 userPath 的字符串字段成员。
  5. 修改类a和b的构造函数为@Inject注释(应该已经是),并接受一个String参数,分别用@HomePath@UserPath<注释/code> 并分配注入值的字符串字段成员。
  6. 使用 .annotatedWith() 在模块的配置方法中创建绑定 定义正确的值;如果它们仅在运行时可用,请绑定提供者。

EG

class a {
  private String homePath;
  @Inject
  public a(@HomePath String homePath) {
    this.homePath = homePath;
  }
  public String tellMeAboutHome() {
    return "We live in a nice home called " + homePath;
  }
}

class customModule extends AbstractModule {
  public static final String userPath = "/home/rafael";

  public void configure() {
    bind(String.class).annotatedWith(HomePath.class).to("/home/");
    bind(String.class).annotatedWith(UserPath.class).to(userPath);
  }
}

如果创建注释对您来说工作量太大,请使用 @Named 注释 Guice 附带

If you're really using Guice correctly all your configuration like this should appear in modules' configure method. So:

  1. Remove the configuration class.
  2. Create annotation classes, probably called HomePath and UserPath.
  3. Where class a uses getHomePath() replace that with a String field member named homePath.
  4. Where class b uses getUserPath() replace that with a String field member named userPath.
  5. Modify the class a and b constructors to be @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.
  6. Create bindings in your module's configure method use .annotatedWith() which define correct values; if they're only available at run time, bind a provider.

E.G.

class a {
  private String homePath;
  @Inject
  public a(@HomePath String homePath) {
    this.homePath = homePath;
  }
  public String tellMeAboutHome() {
    return "We live in a nice home called " + homePath;
  }
}

class customModule extends AbstractModule {
  public static final String userPath = "/home/rafael";

  public void configure() {
    bind(String.class).annotatedWith(HomePath.class).to("/home/");
    bind(String.class).annotatedWith(UserPath.class).to(userPath);
  }
}

If creating annotations is too much work for you, use the @Named annotation Guice ships with.

墨落成白 2024-12-11 15:47:00

您的问题没有单一的答案,只有根据您的具体情况进行选择的选项。

如果您知道您的 Configuration 类将会增长并且如果您的 AB 类可能会使用更多信息,然后将整个 Configuration 对象传递给其构造函数。注意:我知道这违反了 YAGNI 原则,但有时你可能知道你会需要它;-)

否则,你可以考虑使用 @Named 注入路径,以便减少 AB 类的依赖性降至最低,这是一个很好的设计实践。

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 your A and B classes will use more from it, then pass the whole Configuration 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 reduce A and B classes dependencies to their minimum, which is a good design practice.

尛丟丟 2024-12-11 15:47:00

一般规则是使依赖图(哪些类了解或依赖于其他类/接口)的代码尽可能简单、规则和固定。

如果不传递 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.

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