Netflix / archaius

Library for configuration management API
Apache License 2.0
2.46k stars 485 forks source link

Introduce static Property implementation #564

Closed akhaku closed 2 years ago

akhaku commented 5 years ago

A StaticProperty is useful for cases where we don't want a property to change, for example in unit tests.

akhaku commented 5 years ago

@twicksell - we discussed adding a method to the Property interface, but I'm not too sure how I feel about adding a factory (default) method to an interface, so I opted to stick with the separate implementation.

elandau commented 5 years ago

Can you provide additional information about the use case here. Property is normally created by a PropertyRepository and not directly so it feels strange to be creating a static property instance outside of a PropertyRepository.

akhaku commented 5 years ago

Absolutely, so a couple different use cases:

  1. In tests, to test methods that use a Property, it's helpful to pass in a static property instead of mocking or having an overloaded method.
  2. In some non-DI cases (eg static instantiation for local development), we don't have Guice/other DI and so don't have a way of fetching a PropertyRepository. For those cases, it makes sense to have a static property to get the code working, albeit with default values for properties.
elandau commented 5 years ago

I'd be concerned about introducing this given that Archaius is all about dynamic configuration and I don't feel that this is the proper abstraction for introducing 'immutable' configuration. I'd also recommend not relying on the Property interface directly in your code to avoid coupling your business logic specifically with Archaius. There are several alternatives to achieving this,

  1. Property implements Java's Supplier so you can just code to Supplier. For testing you can easily create an immutable supplier like this, () -> CONSTANT.
  2. Define a configuration interface that is bound to archaius in the normal case or manually constructed for testing.
  3. Create an immutable Config for your tests and bind that to a PropertyRepository or a configuration interface as a means to achieve immutable configuration.
akhaku commented 5 years ago

Hm, interesting - we've done something similar in our code, where we have an interface that is implemented using an Archaius PropertyRepository. For non-DI (static creation for local development) we don't pass in a RepositoryFactory and instead initialize properties to static values, but we could instead use a DefaultPropertyFactory and have that use a SettableConfig. I'll give that a shot and add you to the PR so you can take a look, if you're curious.