If you can abstract the kinds of configuring that you need to do as
"configuration elements", you can use Multibinder.

In modules that contribute to the configuration (e.g., your ModuleA and
ModuleB):

Multibinder.newSetBinder(binder(), ConfigurationElement.class)
    .addBinding().toInstance(someConfigElement);

In module that provides Configuration binding (your DatabaseModule):

@Provides Configuration configuration(Set<ConfigurationElement> elements) {
    Configuration config = new Configuration();
    config.setDefaults();
    for (ConfigurationElement element : elements) {
        config.applyElement(element);
    }
}

and then you can depend on Configuration anywhere:

@Provides Database database(Configuration config) {
    return new Database(config);
}

--tim


On Thu, Dec 19, 2013 at 7:08 PM, Martin Kersten <martin.kersten...@gmail.com
> wrote:

> I am struggling with how to implement configuration contribution. By
> configuration contribution I simply mean
> having a service being created *after* all modules have contributed their
> share to a certain configuration.
>
> For example having a database module generating a configuration object and
> two other modules providing additional
> information by taking the Configuration object and manipulate it. When the
> final Database instance is created all those
> modules have contributed their configuration changes to the Configuration
> instance used by the Database instance to
> initialize itself.
>
> DatabaseModule {
>
>    1. configuration = new Configuration()
>    2. configuration.setDefaults();
>
>    3. new Database(configuration)
> }
>
> ModuleA requires DatabaseModule {
>     configuration.setDatabaseUrl("....");
> }
>
> ModuleB requires DatabaseModule {
>     configuration.setUsernameAndPassword("user", "password");
> }
>
>
> Thats just a made up example but I need such pattern very often where you
> have to configure a kind of builder
> or build receipt that is turned into the actual singleton or per thread
> scoped object.
>
> Any idea how to achieve this? I need to use custom objects as build
> receipt.
>
>
> Cheers,
>
> Martin (Kersten),
> Germany
>
> --
> You received this message because you are subscribed to the Google Groups
> "google-guice" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to google-guice+unsubscr...@googlegroups.com.
> To post to this group, send email to google-guice@googlegroups.com.
> Visit this group at http://groups.google.com/group/google-guice.
> For more options, visit https://groups.google.com/groups/opt_out.
>

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

Reply via email to