Skip to content
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

Feature Request: Support binding factory methods to different types with annotation #177

Open
jbarr21 opened this issue Jan 22, 2020 · 3 comments

Comments

@jbarr21
Copy link
Contributor

jbarr21 commented Jan 22, 2020

I noticed that in some scopes, I find myself needing to create many class implementations for types with lots of parameters, but I only want to expose their interface type for encapsulation.

Given interface Foo and concrete implementation FooImpl with 5+ constructor parameters, instead of needing to write a Scope like:

abstract FooImpl fooImpl();

@Expose
abstract Foo foo(FooImpl impl);

It'd be cleaner and less verbose when we have a lot of these in our scope to be able to use an annotation to override the type the newly created object is bound to:

@Expose
@BindTo(Foo.class)
abstract FooImpl fooImpl();

This would use the primary constructor in FooImpl to instantiate a new instance, but only add it to the DI graph as if a new instance of Foo was created.

@Leland-Takamine
Copy link
Collaborator

Leland-Takamine commented Jan 22, 2020

Use case makes sense. Discussion from here is whether is warrants a new API. Some questions around this API:

It's confusing whether the @Expose applies to Foo or FooImpl or both. Is there a version of this that is not ambiguous?

@jbarr21
Copy link
Contributor Author

jbarr21 commented Jan 22, 2020

The @Expose would only expose the type provided by the @BindTo which overrides the type from the factory. Would that maybe be clearer by using private?

@Expose
@BindTo(Foo.class)
private abstract FooImpl fooImpl();

I think max clarity would probably provided by this, but not sure if you want to duplictate the expose semantics in addition to adding this new functionality:

@BindTo(Foo.class, expose=true)
private abstract FooImpl fooImpl();

@andreasnomikos
Copy link

andreasnomikos commented Jan 22, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants