Comments (3)
@Service(...)
boilerplate
The fact that you have to declare all services twice (once in the array of the @service([]) decorator and again in the constructor) is a bit off putting to me cause it's going to add a lot of boilerplate and it'll require quite a bit of effort to migrate.
It definitely means more boilerplate, but in my experience it's a lot easier to maintain than the original TypeDI. Issues like interface
s being represented as Object
in the reflect metadata were initially quite hard to debug, and there were a lot of other pitfalls to the DX.
I'm not really sure what a better solution is, as of now; the current implementation is a lot easier in a few ways due to its avoidance of "magic" (e.g. reflect metadata), which can fail in unexpected and mysterious ways.
Typing / Runtime errors
Yeah, agreed -- this definitely needs an eager eye. Due to backwards compatibility with the original TypeDI (and due to TypeScript bugs), the Service
decorator doesn't ensure the signature of the implementation's constructor aligns with that of the dependencies passed -- TypedService
does, so it's a pretty welcome addition here.
Stage 3 Decorators
Is this a technical limitation of Stage 3 decorators or more of a design decision?
A design decision thus far. I haven't investigated much of the Stage 3 decorator surface, so I'm not entirely sure how well they jibe with experimental decorators' type metadata (and how well it works in general). I'm really looking to avoid the pitfalls of prior solutions altogether, as they can be very frustrating to debug (and can't really be flagged at compile time).
The package does support ES Decorators, but not property injection, etc. It just wraps the Service
function to provide compatibility with the new decorator API.
inject(...)
@Service()
class RootService {
private logger = inject(LogService);
}
I'm very heavily against this approach.
That links to a discussion I had regarding inject
a while ago.
It's effectively the service locator pattern: you're obscuring the dependencies of your class
by taking in a container and retrieving dependencies from it manually. That's the framework's
job, and in most DI solutions it does a much better job of it.
There are a few (practical, not just theoretical) issues with inject
:
- The dependencies of services are obfuscated; now it takes in a container and does... something (this is called the service locator pattern). This is generally a bad idea, especially when working with external modules.
- It makes instantiating these classes (manually) much more confusing: do you need to setup global state? The state required now isn't explicitly in the class's signature, so you need to read the source code to understand how it works (eww).
- The framework has absolutely no idea what the class depends upon prior to instantiating it, so you lose out on any capacity to perform checks before accepting the class into a container.
- The beauty of this package is each class is dependencies in, service out: obscuring that contract is an anti-pattern, and it's something I'd rather not do just to save a few keystrokes.
- Some global state trickery would be required to use
inject
with non-default containers. It might even mean introducing a custom class instantiation function into the package, to set which containerinject
uses -- this is pretty gross.
So, inject
is pretty much a no-go. Just too many pitfalls.
Interested in alternatives to cut down on boilerplate though.
Nevertheless, if you're hellbent on destruction, you could use HostContainer
:-)
freshgum
from typedi.
You seem to be set on your way so I'm going to close this :)
Feel free to report any issues / ask more questions though!
from typedi.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
from typedi.
Related Issues (20)
- API Reference not present at typedi.js.org HOT 3
- Development Mode Ideas
- Documentation on visitors is completely missing
- Lazy-loaded services
- Regression in unreleased 0.6.0's Container dispose functinality HOT 2
- Container not registering classes defined in external directories HOT 4
- Documentation needs an explicit Container.set example with a service
- Interface support HOT 14
- Unable to build node.js typescript project using the library: Missing exported members HOT 4
- Types marked with @internal are excluded from typings, but still imported HOT 3
- Confusion regarding moduleResolution: NodeNext requirement
- Are Stage 3 Decorators supported? HOT 2
- Confusion regarding arguments passed to service factory tuples HOT 2
- Old inaccurate docs are still present HOT 1
- Lack of API Reference documentation HOT 1
- Misuse of resolution constraints is not checked HOT 2
- Explicit Resource Management
- Document Commit Conventions
- Update to 0.5.1 HOT 2
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 typedi.