GithubHelp home page GithubHelp logo

magnodi-tolearn's Introduction

MagnoDI Logo

💉MagnoDI is a lightweight dependency injection package that simplifies managing dependencies in your projects, making them more scalable and maintainable.

Features

  • Lightweight
  • Constructor based injection
  • Property based injection
  • Easy usage with TS decorators

Installation

Using npm:

npm install magnodi

Using yarn:

yarn add magnodi

Using pnpm:

pnpm add magnodi

You need to enable emitting decorator metadata in your Typescript config. Add these two lines to your tsconfig.json file under the compilerOptions key:

"emitDecoratorMetadata": true,
"experimentalDecorators": true

Constructor Based Injection

import { Container, Injectable } from 'magnodi'

@Injectable()
class ExampleInjectedService {
  sayHi() {
    console.log('Hi from injected service!')
  }
}

@Injectable()
class ExampleService {
  constructor(private injectedService: ExampleInjectedService) {}

  greet() {
    this.injectedService.sayHi()
  }
}

const exampleService = Container.resolve<ExampleService>(ExampleService)

exampleService.greet()
// Output: 'Hi from injected service!'

Property Based Injection

import { Container, Injectable, Inject } from 'magnodi'

@Injectable()
class UserService {
  private users = [{ id: 1, name: 'John Doe' }]

  getUsers() {
    return this.users
  }
}

@Injectable()
class App {
  @Inject()
  private userService!: UserService

  printUsers() {
    const users = this.userService.getUsers()
    console.log(users)
  }
}

const app = Container.resolve<App>(App)

app.printUsers()
// Output: [{ id: 1, name: 'John Doe' }]

Contributing

  1. Fork this repository.
  2. Create a new branch with feature name.
  3. Create your feature.
  4. Commit and set commit message with feature name.
  5. Push your code to your fork repository.
  6. Create pull request.

License

MIT

magnodi-tolearn's People

Contributors

canccevik avatar c4nzin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.