Comments (4)
Option number one:
AutoInjectModule(new(MyServer),
// This argument is a mapping of field names to annotations.
// Omitted fields imply inject.AutoInject annotation.
struct {
AuthClient Auth
FrontendClient Frontend
}{}
)
This method implies that the user defines how to provide a type, not the type's author. This method makes it possible and natural to separate the type and the module for providing it into two separate libraries and encourages to have multiple modules for providing the same type for different purposes.
This method requires the user to know the internals of the type and the user's code will break when they change.
from go-inject.
Option number two (thanks @GeniyX):
type MyServer struct {
AuthClient grpc.Client
// ${Field}Annotation field declares the annotation for the ${Field} field.
// Omitting this field implies inject.AutoInject annotation.
AuthClientAnnotation Auth
FrontendClient grpc.Client
FrontendClientAnnotation Frontend
}
Optionally, allow annotation fields to be private, which is possible.
This method implies that there is only one canonical way to provide a type, that the type's author pre-defined for everyone. It's still possible to write a custom provider to override this though.
This method is consistent with annotation providers, that have to be named ProvideAnnotaionXxx
as opposed to ProvideXxx
in normal providers.
from go-inject.
Option number three:
/// Implement this interface to make the type auto-injectable.
type AutoInjectable interface {
/// Returns a mapping of field names to annotations.
/// Omitted fields imply inject.AutoInject annotation.
/// Not implementing this method implies all fields having inject.AutoInject annotation.
ProvideAutoInjectAnnotations() interface{}
}
func (self MyServer) ProvideAutoInjectAnnotations() interface{} {
return struct {
AuthClient Auth
FrontendClient Frontend
}{}
}
This method implies that there is only one canonical way to provide a type, that the type's author pre-defined for everyone. It's still possible to write a custom provider to override this though.
This method can be used together with the first method: the interface implementation will define the default way to provide the type and the user will be able to override specific fields.
from go-inject.
It was decided to implement (1) as the most generic approach and (3) for non-general-purpose libraries.
from go-inject.
Related Issues (6)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-inject.