radbas-container

Version, currently 0.3.02 versions

github.com/radbas/container.cr

DI container for Crystal.

0 stars
0 dependents
License: MIT

Nothing has been indexed for 0.3.0 yet. The tag is recorded, its shard.yml has not been read, so the manifest and dependency list below are empty because they are unknown rather than because they are absent.

Installation

# Add this to your shard.yml
dependencies:
  radbas-container:
    github: radbas/container.cr
    version: ~> 0.3.0

Then run:

shards install

shard.yml

No shard.yml has been indexed for 0.3.0. You can read it on the repository.

Dependencies

Unknown: the shard.yml for this version has not been read yet.

README

This README is the one indexed from the repository at its latest ref, not from the tag for this version.

Container.cr

Simple and small DI Container.

Installation

  1. Add the dependency to your shard.yml:

    dependencies:
      radbas-container:
        github: radbas/container.cr
  2. Run shards install

Usage

Extend the abstract Radbas::Container class and register/autowire your services:

require "radbas-container"

class MyService
  def initialize(
    @dep: DependencyService
  ); end
end

class MyContainer < Radbas::Container

  # autowire does let you register namespaces
  # from which classes get resolved automatically
  autowire(MyApp, CustomNamespace)

  # softmap registers all subclasses of a given abstract class
  softmap(MyAbstractClass, params: {}, public: false)

  # register a single service
  register(DependencyService)

  # a factory can be used for setup
  register(MyService, factory: ->{

    # you can call get inside a factory to resolve dependencies
    MyService.new(get(DependencyService))
  }, public: true)

   # params can be used to configure constructor params
  register(MyService, params: { dep: get(DependencyService) })
end

By default, all registered services are private. Normally you would register a single class as a public entrypoint and call get on the container:

class MyContainer < Radbas::Container
  register(MyApplication, public: true)
end

container = MyContainer.new
app = container.get(MyApplication)
app.run
# ...

Contributing

  1. Fork it (https://github.com/radbas/container/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