Comments (7)
This I can add this week, no problem. Also, the new docs will have some more high level information on architecture.
from dependency-injection.
Rob, this is awesome. This will just leverage more conventions during class activation and adding dynamic metadat.
Can't wait to test thisπ
from dependency-injection.
I think we can just provide a hook on the DI container called onActivate
that gets called any time a new instance is created. Then you can just configure that with your own custom activation code to inspect object instance and perform custom cross-cutting behavior. I think onActivate
should probably return an instance, so that the hook can even have the opportunity to provide alternate instances or Proxies in the future. Any thoughts on what you would need specifically? Can you share a bit about what you want to do so that I have some concrete use cases?
from dependency-injection.
@EisenbergEffect this is awesome news, the point for us is to be able to manipulate the class, one example we are trying to do now is to auto add common decorators to the class, or dynamically add properties or methods( again decorators is good for that).
from dependency-injection.
Ok, adding a class activation hook was quite a bit of trouble. So, I decided not to do it, but to add a lower level hook. There's a new api in the next release on the container: setHandlerCreatedCallback(onHandlerCreated: (handler: InvocationHandler) => InvocationHandler)
The container uses an InvocationHandler
to invoke the function/constructor. This structure contains the function (fn), its inject array (called dependencies) and an invoker
which does the actual invocation. By using this hook, you will be notified each time an InvocationHandler is created. This is once per function that needs to be invoked (cached). Once you have this, you can monkey-patch the invoke method of the handler to add custom activation logic. The nice thing about this is that you can decided whether or not to alter the invoke by examining the dependencies, once, when the handler is created, rather than having to run that decision logic each time the function is invoked.
from dependency-injection.
sounds like a step in the right direction thx @EisenbergEffect
from dependency-injection.
For those interested, here's some sample code that I put together to illustrate what I think Tomasz can now do, based on my discussion with him:
container.setHandlerCreatedCallback(handler => {
//this callback is called once per type that the container will instantiate
//so, for Foo, it will be called once during the application lifetime
//but you can inspect what the container finds, change it and...
//control how instances of that type are created thereafter
//so we only have to change the invocation when the type actually has a dependency on Dispatcher
let index = handler.dependencies.indexOf(Dispatcher);
if(index !== -1) {
handler.dependencies[index] = new DispatcherResolver();
let invoke = handler.invoke;
handler.invoke = function(container, dynamicDependencies) {
let instance = invoke.call(handler, container, dynamicDependencies);
container._lastDispatcher.connect(instance);
container._lastDispatcher = null;
return instance;
};
}
return handler
});
//a custom resolver's get is invoked by the container to "resolve" an instance
//we use a special resolver so we can capture the instance for later use in our invoke fixup
@resolver
class DispatcherResolver {
get(container) {
//not sure how you want to create it, but do that here
//and then store it temporarily on the container, so you can grab it later
return container._lastDispatcher = container.get(InstanceDispatcher);
}
}
class InstanceDispatcher {
connect(instance) {
//do what you want with the connected instance
}
}
I noted to Tomasz that he probably doesn't want to use _lastDispatcher
but instead may need to push/pop from a stack. And, actually, in that case, it could be a singleton that is shared and then it doesn't need to be stored on the container as an ad hoc variable.
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.