Friday, May 24, 2013

Static dependency Injection vs Direct deferred binding

Hi every,

Recently I am working on a i18n project in which I created some utility class to provide localized content like the following:

public class Foo {
           
    @Inject
    private static MyConstantsWithLookup constants;
  
   
    public static String getLocalizedContent(String rawContent) {
        return constants.getString(rawContent);
    }

}

Where MyConstantsWithLookup is an interface extending ConstantsWithLookup. And I have the following in my GinModule:

    requestStaticInjection(Foo.class);

    @Provides
    @Singleton
    public MyConstantsWithLookup getMyConstantsWithLookup() {
        MyConstantsWithLookup constants = GWT.create(MyConstantsWithLookup.class);
        return constants;
    }




However, my teammate doesn't agree my use of @Inject here, he argued that I should use GWT.create(MyConstantsWithLookup.class) in Foo directly so MyConstantsWithLookup can be resolved in compile time and got certain optimizations from deferred binding,  instead of letting it be handled in runtime by Gin. He also said it's generally undesired to do static injection. But I do like the brevity of @inject and MyConstantsWithLookup is not something I usually need to mock out. So I wonder what are the exact pros and cons here? Thanks a lot!

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment