作成されたオブジェクトへのアシスト注入を介してオブジェクトを作成したメソッドの名前を発見/注入できるようにしたいと思います。
私がやりたいことの例:
// what I want guice to create the implementation for this
interface Preferences {
Preference<String> firstName();
Preference<String> lastName();
// other preferences possibly of other types
}
// my interfaces and classes
interface Preference<T> {
T get();
void set(T value);
}
class StringPreference implements Preference<String> {
private final Map<String, Object> backingStore;
private final String key;
@Inject StringPreference(@FactoryMethodName String key,
Map<String, Object> backingStore) {
this.backingStore = backingStore;
this.key = key;
}
public String get() { return backingStore.get(key).toString(); }
public void set(String value) { backingStore.put(key, value); }
}
// usage
public void exampleUsage() {
Injector di = // configure and get the injector (probably somewhere else)
Preferences map = di.createInstance(Preferences.class);
Map<String, Object> backingStore = di.createInstance(...);
assertTrue(backingStore.isEmpty()); // passes
map.firstName().set("Bob");
assertEquals("Bob", map.firstName().get());
assertEquals("Bob", backingStore.get("firstName"));
map.lastName().set("Smith");
assertEquals("Smith", map.lastName().get());
assertEquals("Smith", backingStore.get("lastName"));
}
残念ながら、これを実装するためにこれまでに考えた唯一の方法は、
- アシスト インジェクションを (コピー アンド ペーストで) 拡張して機能を追加する
- 私のためにそれを行う補助注射に非常に似たものを書いてください
- 偽りの助けを借りずにこれを行うボイラープレートをたくさん書く
次のような解決策を探しています。
- これを行ういくつかのguice構成またはパターン
- これを行う拡張機能
- これを自分で書くのに役立つドキュメント/場所の例
- 私がやりたいことを達成するためのサンプルアプリの代替パターン