Coder Social home page Coder Social logo

crystal-di's Introduction

Lightweight DI Container for Crystal

Build Status

alt text

Crystal-DI is a flexible DI-container with simple DSL, auto-injection, memoization, lazy evaluation and contextual bindings.

Installation

Add this to your application's shard.yml:

dependencies:
  di:
    github: funk-yourself/crystal-di
    version: ~> 0.2.1

Usage

It's as simple as:

require "di"

module Container # may be a class as well
  include DI::ContainerMixin

  register Foo, Foo.new
end

Container.resolve(Foo)

You can also use blocks:

register Bar, Bar.new

register Foo do
  logger = Logger.new(STDOUT)
  logger.level = Logger::WARN
  Foo.new(resolve(Bar), logger)
end

Auto-injection

module Container
  include DI::ContainerMixin

  register AppService, AppService.new
end

class AppService
end

class Controller
  include DI::AutoInject(Container)

  def initialize(@app_service : AppService)
    p @app_service
  end
end

Controller.new

Memoization

Sometimes you need to be sure that there's only one instance of some service. You can achieve that with memoize option:

# Will be evaluated only one time
register AppService, AppService.new, memoize: true

Contextual bindings

You can bind container items to any class with context option:

register AppService, AppService.new, context: Controller

It means that you can resolve this item by calling

Container.resolve(AppService, context: Controller)

But the main purpose is ability to auto-inject different implementations of abstract class/interface into different classes:

require "di"

module Container
  include DI::ContainerMixin
  register Storage, RedisStorage.new, context: UsersController
  register Storage, MemcachedStorage.new, context: PostsController
end

abstract class Storage
end

class RedisStorage < Storage
end

class MemcachedStorage < Storage
end

class UsersController
  include DI::AutoInject(Container)

  def initialize(@storage : Storage)
    p @storage
  end
end

class PostsController
  include DI::AutoInject(Container)

  def initialize(@storage : Storage)
    p @storage
  end
end

UsersController.new # will print RedisStorage
PostsController.new # will print MemcachedStorage

Development

Run tests:

crystal spec

Contributing

  1. Fork it ( https://github.com/funk-yourself/crystal-di/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

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.