Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,14 @@ Diosaur is a small dependency injection solution written in Typescript for Deno
of code, avoiding obvious bindings and other repetitive stuff. It internally depends on `reflect-metadata` to guess
the maximum indications out of your code, but still allows you for finer definition of your services.

## Reflect Metadata
As Diosaur relies on `reflect-metadata` and this library was not officially ported to Deno yet, you'll need
to import it manually in your project.


## Example
```typescript
/** Deno **/
// Import reflect-metadata
import 'https://raw.githubusercontent.com/rbuckton/reflect-metadata/master/Reflect.js';

// Import diosaur
import { Service, Parameter, Inject, setParameter, getContainer } from 'https://raw.githubusercontent.com/ovesco/diosaur/master/mod.ts';

/** Node **/
// Import reflect-metadata, first install it `npm install --save reflect-metadata`
import 'reflect-metadata';

// Import diosaur
import { Service, Parameter, Inject } from 'diosaur';

Expand Down Expand Up @@ -58,12 +48,25 @@ getContainer().then((container) => {
});
```

## How does it work
Generally speaking, a dependency injection library handles the lifecycle of your
services, which means that you don't have to create or remove them, it's handled
by the container. In Diosaur, services are Typescript `class` decorated with the
`@Service` decorator as illustrated in the upper example.

# Using it in Deno

Currently use of decorator is not allowed by default in deno.
To run a module requiring diosaur you will need to provide a tsconfig.jsonconfig
file specifying `"experimentalDecorators": true`.

For example to run the above example in deno:
```bash
git clone https://github.com/ovesco/diosaur
cd diosaur/demo
deno run --config tsconfig.json deno_demo.ts
```
![image](https://user-images.githubusercontent.com/6702424/83362772-13bf1a80-a394-11ea-8bca-6312539641f8.png)

### Injecting services
Another purpose of dependency injection is actually managing your dependencies for you.
You can as such inject other services into your service using the `@Inject` decorator.
Expand Down
27 changes: 27 additions & 0 deletions demo/deno_demo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Service, Parameter, Inject, setParameter, getContainer } from '../mod.ts';

@Service()
class Doggo {
constructor(@Parameter('doggoName') private name: string) {}

bark() {
return this.name.toUpperCase();
}
}

@Service()
class JonSnow {

@Inject()
private doggo: Doggo;

yell() {
return `I'm Jon with my doggo ${this.doggo.bark()} !`;
}
}

setParameter('doggoName', 'Ghost');
getContainer().then((container) => {
const jon = container.get(JonSnow);
console.log(jon.yell());
});
12 changes: 12 additions & 0 deletions demo/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"lib": ["dom", "ES2015"],
"esModuleInterop": true,
"experimentalDecorators": true,
"strictPropertyInitialization": false,
"emitDecoratorMetadata": true,
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true
}
}
1 change: 1 addition & 0 deletions deno_dist/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { IContainer } from "./Container.ts";
import Registrer from "./Metadata/Registrer.ts";
import { Constructor, ServiceClassIdentifier, ServiceIdentifier } from "./Types.ts";
import { FunctionFactory } from "./Factory.ts";
import "https://raw.githubusercontent.com/rbuckton/reflect-metadata/master/Reflect.js";

import {
Service,
Expand Down
1 change: 1 addition & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IContainer } from "./Container";
import { Constructor, ServiceClassIdentifier, ServiceIdentifier } from "./Types";
import "reflect-metadata";
import { Service, Inject, InjectAll, Factory, Parameter } from './Decorators';
export { Service, Inject, InjectAll, Factory, Parameter, IContainer, };
export declare const getContainer: () => Promise<IContainer>;
Expand Down
1 change: 1 addition & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
};
import Registrer from "./Metadata/Registrer";
import { FunctionFactory } from "./Factory";
import "reflect-metadata";
import { Service, Inject, InjectAll, Factory, Parameter, } from './Decorators';
export { Service, Inject, InjectAll, Factory, Parameter, };
export const getContainer = () => __awaiter(void 0, void 0, void 0, function* () {
Expand Down
2 changes: 1 addition & 1 deletion mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//Automatically generated by denoify. It is important not to edit this file.
// #{"srcDirPath":"src","denoDistPath":"deno_dist","tsconfigOutDir":"./dist"}#
// #{"srcDirPath":"src","denoDistPath":"deno_dist","tsconfigOutDir":"dist"}#

export * from "./deno_dist/index.ts";
47 changes: 29 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 12 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
{
"devDependencies": {
"@types/node": "^14.0.6",
"denoify": "^0.2.18",
"reflect-metadata": "^0.1.13"
"denoify": "^0.2.21",
"reflect-metadata": "^0.1.13",
"typescript": "^3.9.3"
},
"name": "diosaur",
"version": "0.0.1",
"author": "guillaume hochet",
"license": "MIT",
"keywords": ["inversion", "of", "control", "dependency", "injection", "typescript"],
"keywords": [
"inversion",
"of",
"control",
"dependency",
"injection",
"typescript"
],
"description": "A small dependency injection library in typescript",
"scripts": {
"build": "tsc && npm run denoify",
"denoify": "npx denoify"
"build": "tsc && denoify"
},
"main": "dist/index.js",
"denoPorts": {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { IContainer } from "./Container";
import Registrer from "./Metadata/Registrer";
import { Constructor, ServiceClassIdentifier, ServiceIdentifier } from "./Types";
import { FunctionFactory } from "./Factory";
import "reflect-metadata";

import {
Service,
Expand Down
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"dist",
"node_modules",
"./deno_dist",
"./mod.ts"
"./mod.ts",
"./demo"
]
}
}