Comments (8)
In this case you two options:
- Explicitly register App with the container using
registerTransient
. This tells the container that every time it is requested, a new instance should be returned. - Create a child container from the original using
createChild
This creates a new container that inherits the services of its parent. Manually register the App service with the child container as either singleton or transient and resolve App from the child.
Child containers are for more advanced scenarios such as wanting to have a parent container for your app, but a scoped child container for each web request. Child containers allow you to override services from the parent while still leveraging other instances managed by the parent. Usually if you just want to get new instances of a particular type, you just register it as a transient. So, options 1 is probably what you want. Another way you can accomplish transient registration automatically is to annotate the App class with the Transient annotation.
from dependency-injection.
I've added a few more tests. There is a test showing the API registerTransient
to go along with the annotations tests.
from dependency-injection.
Ok, thanks, it works for me:
class App {}
var container = new Container();
container.registerTransient(App, App);
var app1 = container.get(App);
var app2 = container.get(App);
expect(app1).not.toBe(app2);
Can you tell me what do that args mean (App, App)? Why App on two places?
from dependency-injection.
The first arg is the key or type to register as. The second arg is the type of instance that key should resolve to. This enables scenarios like this:
container.registerTransient(AbstractType, ConcreteType);
So whenever someone requests AbstractType, they will get an instance of ConcreteType. This is used by the framework internally to allow it to work against abstractions of key services. I was thinking today that there should probably be a shortcut like this:
container.registerTransient(ConcreteType);
So you can easily use this when you don't have an abstraction. What do you think?
from dependency-injection.
I think that shortcut is prettier in my case.
If it could be worth to you to hear beginner's experience, from my perspective, I have expected to get new instance by default. Ok, in this moment I don't have an wide look to framework and what other aurelia tools need from it, but just saying.
from dependency-injection.
I'll go ahead and add the shortcut. That should be easy. Regarding default behavior, I've seen DI frameworks that do either. For purposes of Aurelia, the singleton approach works best. Since we don't really have docs yet...I think providing those and making the default behavior known should solve the problem.
from dependency-injection.
ok, I will watch the project get notif about shortcut
from dependency-injection.
It's in master now. I've also added a few more tests and re-arranged them a bit. So, hopefully that will help you see some more details. I still need to add more, but it's a start.
I'm currently adding the api docs to the source code. I'll commit that shortly. Then, I'm going to try to write up some preliminary documentation. You will be able to find that in the doc
folder when it's pushed.
Hopefully, I will have another release of this module in the next day or two.
from dependency-injection.
Related Issues (20)
- @lazy not working with @autoinject HOT 7
- get rid of aurelia-pal dependency HOT 2
- Optional resolver doesn't work as expected HOT 2
- Proposal for easier way to inject via DI into a view-model HOT 10
- di, nodejs HOT 4
- Missing property in d.ts? HOT 5
- Arrays and the DI Container HOT 26
- Version 1.4.0 does not contain the dist directory HOT 6
- 1.4.0 release and typings. HOT 3
- Unable to jspm bundle aurelia-dependency-injection 1.4 HOT 4
- Injecting a string resolves an empty string instead of undefined HOT 3
- Bug: Cannot use spread with the injector (Injection With Inheritance in Aurelia) HOT 19
- No access to global Container when used in node npm module HOT 5
- 1.4.2 release typings HOT 25
- error TS1005: ';' expected in TS typings HOT 1
- dependency-injection 1.5.0 incompatible to aurelia-validation 1.4.0 HOT 9
- error TS1110: Type expected HOT 6
- Building project in babel-loader fails on const enum because of --isolatedModules flag HOT 1
- Bug: @newInstance() becomes container default instance if it's the first instance
- Backport `resolve` from Aurelia 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 dependency-injection.