Skip to content
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

Dependency injection as standalone library #26

Closed
b091 opened this issue Apr 24, 2015 · 3 comments
Closed

Dependency injection as standalone library #26

b091 opened this issue Apr 24, 2015 · 3 comments

Comments

@b091
Copy link

b091 commented Apr 24, 2015

Hi,

we are writing a library in TypeScript 1.5.
We would like to achieve dependency injection using ES7 proposal decorators.
Instead of writing it ourselves we found your DI component (aurelia-dependency-injection) and tried to use it in our enviroment.
We're faceing some problems. Did you predicted usage of aurelia-dependency-injection without entire aurelia framework?
If so, how can we do it?.. How to create objects without passing arguments to constructor?

index.html

<script src="jspm_packages/system.js"></script>
<script src="config.js"></script>
<script>
  System.import('jquery');
  System.import('scripts/Application');
</script>

scripts/SampleTest.ts

export class SampleTest {
  testName = 'Jon Doe test';
  constructor() {
    console.log('HEY 1', this);
  }
}

scripts/Application.ts

import {inject} from 'aurelia-dependency-injection';
import {SampleTest} from 'scripts/SampleTest';

@inject(SampleTest)
export class Application {
  constructor(simpletest) {
    console.log(this, simpletest); // result is: Application {},  undefined
  }
}

new Application(); // Is there other way to do this (initialize the app)?
@EisenbergEffect
Copy link
Contributor

You need to use the DI container to get the instance.

import {Container} from 'aurelia-dependency-injection';

var container = new Container(); //your global container for the app, created at startup
var app = container.get(Application);

The container has to be used as the source of the instance.

@b091
Copy link
Author

b091 commented Apr 24, 2015

Thank You. It works like a charm 👯

@EisenbergEffect
Copy link
Contributor

Note, that our next release will make this even better. There's an optional compilation flag for TS 1.5 that enables it to preserve some runtime type information, so we are going to support that metadata in our DI implementation. After the next release, you will be able to do this:

import {inject} from 'aurelia-dependency-injection';
import {SampleTest} from 'scripts/SampleTest';

@inject
export class Application {
  constructor(simpletest:SampleTest) {
   ...
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants