luvies / deno_di Goto Github PK
View Code? Open in Web Editor NEWA dependency injection (IoC) module for deno
License: MIT License
A dependency injection (IoC) module for deno
License: MIT License
I am getting the following error when try to import di module in sample app
deno --version output is following
deno 1.2.3
v8 8.6.334
typescript 3.9.2
Basically just a class to wrap a symbol and a Type so you can get inference through with your types.
e.g.
const token ExampleToken = new Token<IExample>("example") // Internally just has a Symbol("example")
interface IExample {
run(): void;
}
@Service()
class A implements Iexample {
public run(): void {
console.log("A");
}
}
// Service collection setup.
const serviceCollection = new ServiceCollection();
serviceCollection.addTransient(ExampleToken, A); // IExample is inferred as T from Token<T>
const services = serviceCollection.get(ExampleToken); // sevices is inferred as IExample
Has anyone tried using this inside js
generated with deno bundle
? It appears to me Inject
does not work in a bundle.
Here is the TS code:
import "https://cdn.pika.dev/@abraham/reflection@^0.7.0";
import { Inject, Service, ServiceCollection } from "https://deno.land/x/[email protected]/mod.ts";
@Service()
class A {}
// Constructor injection
@Service()
class B {
constructor(public a: A) {}
}
// Property injection.
@Service()
class C {
@Inject(A)
a!: A;
}
// Service collection setup.
const serviceCollection = new ServiceCollection();
serviceCollection.addTransient(A);
serviceCollection.addTransient(B);
serviceCollection.addTransient(C);
const b = serviceCollection.get(B);
console.log(b instanceof B); // true
console.log(b.a instanceof A); // true
const c = serviceCollection.get(C);
console.log(c instanceof C); // true
console.log(c.a instanceof A); // true with deno run, false in bundle
Saved to a test.ts
file the following works (prints true for all four console logs):
deno run -c tsconfig.json ./test.ts
but try generating a bundle you will get three true
s and a false
.
deno bundle -c tsconfig.json test.ts | node
Hello,
I've been using your example of abstract classes and I'm experincing what seems like constructor parameters not being injected/passed along. The following is a modified version of the example you have posted. I'm inexperienced with Deno/Typescript enough to be uncertain if this is a bug or something I'm misunderstanding of how the language works.
Thank you for any insight you can provide.
import "https://cdn.skypack.dev/@abraham/[email protected]";
import { assert } from "https://deno.land/[email protected]/testing/asserts.ts";
import { Service, ServiceCollection } from "https://deno.land/x/di/mod.ts";
abstract class IA {
abstract foo(): string;
}
abstract class IB {
abstract IndirectFoo(): string;
abstract bar(): string;
}
@Service()
class A implements IA {
public foo(): string {
return "foo";
}
}
@Service()
class B implements IB {
constructor(
private a: IA
) {}
public IndirectFoo(): string {
return this.a.foo();
}
public bar(): string {
return "bar";
}
}
const serviceCollection = new ServiceCollection();
serviceCollection.addTransient(IA, A);
serviceCollection.addTransient(IB, B);
const ib = serviceCollection.get(IB);
Deno.test("Is expected type", () => {
assert(ib instanceof B);
});
Deno.test("Expects 'bar'", () => {
assert(ib.bar() == "bar");
});
Deno.test("Expects 'foo'", () => {
assert(ib.IndirectFoo() == "foo");
});
Error message:
Check file:///Users/jason/Code/example/example.ts
running 3 tests from file:///Users/jason/Code/example/example.ts
test Is expected type ... ok (6ms)
test Expects 'bar' ... ok (4ms)
test Expects 'foo' ... FAILED (9ms)
failures:
Expects 'foo'
TypeError: Cannot read property 'foo' of undefined
at B.IndirectFoo (file:///Users/jason/Code/example/example.ts:28:23)
at file:///Users/jason/Code/example/example.ts:51:15
at asyncOpSanitizer (deno:runtime/js/40_testing.js:35:15)
at resourceSanitizer (deno:runtime/js/40_testing.js:72:13)
at exitSanitizer (deno:runtime/js/40_testing.js:99:15)
at runTest (deno:runtime/js/40_testing.js:215:13)
at Object.runTests (deno:runtime/js/40_testing.js:283:28)
at async file:///Users/jason/Code/example/832dba8c-7c32-4f9d-a6da-f1050ff07807$deno$test.js:4:7
For example:
const types = {
IA: Symbol("IA")
};
// Interfaces
interface IA {
foo(): void;
}
// Implementations
@Service()
class A implements IA {
public foo(): void {
console.log("A");
}
}
@Service()
class B implements IA {
public foo(): void {
console.log("B");
}
}
// Service collection setup.
const serviceCollection = new ServiceCollection();
serviceCollection.pushTransient<IA>(types.IA, A);
serviceCollection.pushTransient<IA>(types.IA, B);
const services = serviceCollection.getMany(types.IA);
is there an injection feature in the method class? like :
thanks.
@Service()
class Money {
}
@Service()
class Wallet {
@Inject
public add(money: Money) {
}
}
Maybe bumping the version to 1.6.2 would be a good idea to check that everything is working as expected.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.