-
Notifications
You must be signed in to change notification settings - Fork 43
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
Creatable / ScopeFactory Proposal #125
Comments
👍 from me. |
Yay for not having to reference generated code! 👍 |
The only concern with this proposal is that it deals with only abstracting the input but not the output of the Scope in theory we would like to implement the following libraries /impl /usage this can handle the ServiceLocator pattern for motif scopes in a nice concise api However this can also be achieved with another plugin system on top of motif that just operates as a ServiceLocator Pattern but it still needs to understand how to pass the dependencies to the motif constructor however since we are already creating a reflection api to do that seems a prime opportunity to consider merging this into the main framework. |
There's a lot to consider when thinking about surfacing an API to instantiate an implementation of an interface without explicit knowledge of the implementation. As you mentioned, what you're describing is something analogous to a Java |
Understood the only limiting/conflicting factor I see with this proposal is that reusing the Creatable concept to specify that api might make sense however there is nothing precluding the usage of a different api to specify that. e.g. the following does make sense too
|
On another note since we are using reflection already we can avoid the creation of the helper class in favor of directly using the primary constructor in the FooScopeImpl |
Excited. I have a code lab to reproduce google/auto#660, let me know if you want to validate this case. |
The service locator functionality is something that would be an opt-in, which means that the addition of that API should be a non-breaking change (separate API).
I've thought about this as well. The benefit is that for those "root" scopes that use the |
Spoke offline with @andreasnomikos and we agreed on moving forward with the current approach since we believe there are non-breaking paths forward to addressing his concerns/use-cases. |
An update on this - The final implementation is in fact going with @andreasnomikos's suggestion: The scope implementation does not generate the inner Dependencies class if the Scope extends Creatable. The generated constructor takes in the user-defined dependencies class that is the type argument to the Creatable generic superclass. |
This proposal supersedes #108 and addresses the API issues mentioned in the comments of that proposal.
This proposal introduces 2 new APIs:
Creatable<D>
: Replaces@Dependencies
ScopeFactory
: Alternative tonew FooScopeImpl(dependencies)
Creatable<D>
The
Creatable<D>
API allows explicit declaration of dependencies expected from the parentScope
.Existing Pattern
Limitations
Dependencies
interface outside of the Scope. This is problematic if you want to create a library that instantiates a Scope under the hood, but would like to hide that implementation detail from consumers.Proposed API
Benefits
ScopeFactory
API below by specifyingDependencies
interface via generics.Dependencies
interface to be defined outside of theScope
class.Trade-offs
@Dependencies
APIScopeFactory
The
ScopeFactory
API enablesScope
instantiation without referencing generated code.This does not affect the usage of child methods.
Existing Pattern
Limitations
The pattern above requires you to reference generated code (
FooScopeImpl
andFooScopeImpl.Dependencies
). There are a few problems with this:New API
Benefits
You no longer need to reference generated code which unlocks the following benefits:
Trade-offs
Advantages over #108
This proposal couples a
Scope
to a single dependencies interface. This allows us to keep the exact same semantics as the current@Dependencies
API which allows for an automatable migration path. The API proposed in #108 had slightly different semantics and would have been more difficult to migrate to.Implementation
FooScopeFactoryHelper
class which allows us to instantiateFooScope
givenFooDependencies
.ScopeFactory.create
method calls the generatedFooScopeFactoryHelper.create
method reflectively.Generated Code
Generated constructor takes in user-defined
FooDependencies
interface.New Framework Classes
The text was updated successfully, but these errors were encountered: