-
Notifications
You must be signed in to change notification settings - Fork 96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DropwizardEnvironmentModule - Class<? extends Configuration> support #2
Comments
@btiernay I absolutely agree and would love to change it. The reason it was done this way was because we are creating the injector before the run call so we don't have access to the environment or configuration. Would you like to take a stab at it and send us a pull request? If you notice we create the module during the initialize phase then set the environment and configuration in the run method. This doesn't let us do anything during the configure method. |
The version of GuiceBundle.java lines 71-97 in 0.6.1.1 seems to have access to the configuration object in the run method:
|
I guess you haven't seen the latest iteration. :( Let me ask @axiak about it. |
We need to create the injector early in order to use guice for database configuration during the bootstrap phase. The only thing I can think of is asking for Class explicitly in the builder, and binding based off of that. |
I understand how you have 2 phases to building the injector and the constraints it imposes. What about https://github.com/jhalterman/typetools Class parameterizedType = (Class) ((ParameterizedType) getClass() This technique works where the type parameter is defined on the immediate superclass, but it fails if the type parameter is defined elsewhere in the type hierarchy. |
One more thing, Guice provides TypeLiteral for this type of thing: http://google-guice.googlecode.com/git/javadoc/com/google/inject/TypeLiteral.html This illustrates what you can do: http://google-guice.googlecode.com/git/javadoc/com/google/inject/Binder.html (see bind(new TypeLiteral<PaymentService>() {}).to(CreditCardPaymentService.class); ) Essentially in GuiceBundle's init, call new TypeLiteral(){} (notice the braces!) and pass that to the DropwizardEnvironmentModule constructor. |
Another option is to use a child injector in the run method. Not sure if this would break other dependencies. Out of curiosity, what type of database access has to be done so early? And how are you configuring the connections without a Configuration instance? |
I think the TypeLiteral is a bit overkill here. We would use the Key for this anyway, but I think it's easier just to ask for Class in the builder (and typesafe too). I don't think the typetools or getGenericSuperclass() will work since we're not asking people to create concrete subclasses of GuiceBundle. Again, it's be simpler just to add a .setConfigurationClass(Class clazz) in the builder. Essentially any time we need to use guice for creating a bundle, it needs to be during the bootstrap phase. At our company we have lots of existing configuration stuff based off of guice that we want to leverage here. |
The TypeLiteral is just a convenient way to access the generic type without requiring anything of the client (see http://stackoverflow.com/questions/3370641/how-does-guices-typeliteral-work for an explanation). However, I don't think GuiceBundle's type parameter is bound by the caller. This would actually be typesafe since the user can't accidentally pass the wrong class instance dynamically as you are recommending. However, I think your solution is fine if this doesn't work out. Just curious, are you creating your own Configuration instance in initialize some where to configure your bundles? I might need to do this at some point and it would be good to know :) |
I understand what TypeLiteral does and how it works (incidentally, the fact that it requires subclassing is why we can't access T in our GuiceBundle without subclassing). I'm just saying that if we ask for a TypeLiteral, we might as well ask for the more generic Key (http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/inject/Key.html) class, since one can build a Key from a typeliteral or anything else that can identify an instance. As for the Configuration creation: right now we're leaving it up to the clients. We have a separate database configuration system (see https://github.com/HubSpot/Mothra) and we're going to ship a library to create a DatabaseConfiguration object given some zookeeper key. |
This was resolved in the PR3. |
Is there any reason why DropwizardEnvironmentModule uses a Configuration @Provider instead of an application derived class? This mean that all dependent classes must downcast to use this dependency which is less than ideal. For value objects, it really makes sense to used the most derived type.
Perhaps also binding to an instance with a derived class object would help, by using configuration.getClass():
The text was updated successfully, but these errors were encountered: