Giter Club home page Giter Club logo

dim's Introduction

DIM: Dependency Injection - Minimal

DIM is Jim Weirich's minimalistic dependency injection framework, maintained in gem form by Mike Subelsky.

Dependency injection lets you organize all of your app's object setup code in one place by creating a container. Whenver an object in your application needs access to another object or resource, it asks the container to provide it (using lazily-evaluated code blocks).

When testing your code, you can either stub out services on the container, or you can provide a substitute container.

Example

The following could be in a "lib.init.rb" file or in a Rails app, "config/initializers/container.rb":

require "dim"
require "logger"
require 'game'
require 'event_handler'
require 'transmitter'

ServerContainer = Dim::Container.new

ServerContainer.register(:transmitter) { |c| Transmitter.new(c.logger) }

ServerContainer.register(:event_handler) do |c|
  eh = EventHandler.new
  eh.transmitter = c.transmitter
  eh.logger = c.logger
  eh
end

ServerContainer.register(:listening_host) { "0.0.0.0" }
ServerContainer.register(:listening_port) { "8080" }

ServerContainer.register(:game) do |c|
  game = Game.new
  game.logger = c.logger
  game.event_handler = c.event_handler
  game.host = c.listening_host
  game.port = c.listening_port
  game
end

ServerContainer.register(:root_dir) do |c|
  Pathname.new(File.expand_path(File.dirname(__FILE__) + "/.."))
end

ServerContainer.register(:log_file_path) do |c|
  "#{c.root_dir}/log/#{c.environment}.log"
end

ServerContainer.register(:logger) do |c|
  Logger.new(c.log_file_path)
end

# attempts to read ENV["API_PASSWORD"], otherwise makes sure that the parent container has
# a service named api_password registered
ServerContainer.register_env(:api_password)

Using the above code elsewhere in the app, when you want a reference to the app's logger object:

ServerContainer.logger.info("I didn't have to setup my own logger")

Or if you wanted access to the game instance created during setup (which already is configured with everything it needs):

current_game = ServerContainer.game

If you don't like creating even the one dependency on the global constant ServerContainer, you could inject ServerContainer itself into your objects like so:

World.new(GameContainer)

More Background

Jim wrote a nice article explaining the rationale for this code and how it works. Also check out his slides.

License

Dim is available under the MIT license (see the file LICENSE for details).

dim's People

Contributors

jimweirich avatar subelsky avatar

Stargazers

 avatar  avatar

Watchers

 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.