Skip to content

Instantly share code, notes, and snippets.

@sebersole
Created May 17, 2011 15:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebersole/976704 to your computer and use it in GitHub Desktop.
Save sebersole/976704 to your computer and use it in GitHub Desktop.
metadata (building) options
// Option 1 - Options part of MetadataBuilder which is more "natural" i think, but maybe not
// feasible if the binding code does know about MetadataBuilder (in which case see option 2).
public interface MetadataBuilder {
public static interface Options {
public boolean useNewIdentifierGenerators();
...
}
...
public MetadataBuilder withNewIdentifierGeneratorsEnabled(boolean enabled);
public Options getMetadataBuilderOptions();
}
public class MetadataBuilderImpl implements MetadataBuilder {
...
private static class OptionsImpl implements Options {
private boolean useNewIdentifierGenerators; // I believe the default is currently false
...
public boolean useNewIdentifierGenerators() {
return useNewIdentifierGenerators;
}
}
private final OptionsImpl options = new OptionsImpl();
public withNewIdentifierGeneratorsEnabled(boolean enabled) {
this.options.useNewIdentifierGenerators = enabled;
}
...
public Options getMetadataBuilderOptions() {
return options;
}
}
// Option 2 - Options part of Metadata, specifically to make it easier to pass along to binding code afaiu.
// A pro of this approach is that the options used to construct a Metadata are available from it after it is built.
public interface MetadataBuilder {
...
public MetadataBuilder withNewIdentifierGeneratorsEnabled(boolean enabled);
}
public class MetadataBuilderImpl implements MetadataBuilder {
...
private static class OptionsImpl implements Metadata.Options {
private boolean useNewIdentifierGenerators; // I believe the default is currently false
...
public boolean useNewIdentifierGenerators() {
return useNewIdentifierGenerators;
}
}
private final OptionsImpl options = new OptionsImpl();
public withNewIdentifierGeneratorsEnabled(boolean enabled) {
this.options.useNewIdentifierGenerators = enabled;
}
...
public Metadata buildMetadata() {
return new MetadataImpl( ..., options );
}
}
public interface Metadata {
public static interface Options {
public boolean useNewIdentifierGenerators();
...
}
public Options getMetadataOptions();
}
@jpav
Copy link

jpav commented May 17, 2011

I prefer option 1. The binding code should still be able to get a hold of the options via the builder passed into the metadata impl.

I also assume the Options interface would include a buildMetadata() method that would call the like-named method on MetadataBuilder.

@sebersole
Copy link
Author

No Options would not have a buildMetadata() method. Its purpose is a view of the config options targeting building metadata. Options is simply providing access to these config options.

@sebersole
Copy link
Author

I did not add withNewIdentifierGeneratorsEnabled() to the interface, just the impl (which I have corrected), so maybe that caused some confusion

@jpav
Copy link

jpav commented May 17, 2011

Oh, right. You didn't put a return type for the withNew... method, but now I see if that returns the builder, the options wouldn't need the method I mentioned.

@sebersole
Copy link
Author

I expanded out the second proposal some more.

@hferentschik
Copy link

I think I prefer option 2, but it might help if you add some pseudo client code for the two options.

@sebersole
Copy link
Author

The client code is the same in either case:

new MetadataSource(...)
        .getMetadataBuilder()
        .withNewIdentifierGeneratorsEnabled( true )
        ...
        .buildMetadata();

@hferentschik
Copy link

Still option 2 :-) I don't think that once Metadata is build there should be any reference to the builder.
How does this tie in with other options? I think we talked about whether we create a setter for all options or only several and have an additional property container for everything else. I guess I am talking about org.hibernate.cfg.Settings.

@jpav
Copy link

jpav commented May 18, 2011

I'd agree, but that's exactly why I like option 1. Seems like the builder options should be accessed via the builder the user already has a reference to. What use-case might drive wanting to access these options via the Metadata interface? I'm not saying there isn't one; I just don't know what it might be.

@hferentschik
Copy link

You will need these options during the binding phase

@sebersole
Copy link
Author

sebersole commented May 18, 2011 via email

@jpav
Copy link

jpav commented May 18, 2011

Yes, but that's only in the impl and only during construction time.

@sebersole
Copy link
Author

sebersole commented May 18, 2011 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment