al6x / micon

Silent killer of dependencies and configs
29 stars 2 forks source link

Documentation: http://alexeypetrushin.github.com/micon

Silent killer of dependencies and configs

Micon allows You easilly and transparently eliminate dependencies and configs. Usually, when You are building complex system following tasks should be solved:

By component I mean any parts of code logically grouped together.

Micon solves all these tasks automatically, and has the following price - You has to:

That's all the price, not a big one, compared to the value, eh? It's all You need to know about it to use 95% of it, there are also 2-3 more specific methods, but they are needed very rarelly.

Techincally Micon is sort of Dependency Injector, but because of its simplicity and invisibility it looks like an alien compared to its complex and bloated IoC / DI cousins.

Install Micon with Rubygems:

gem install micon

Once installed, You can proceed with the examples below.

The project hosted on GitHub. You can report bugs and discuss features on the issues page.

Basic example

require 'micon'
require 'logger'

# Registering `:logger` component.
micon.register(:logger){Logger.new STDOUT}

class Application
  # Whiring the `:logger` component and application together.
  inject :logger

  # Now You can use `:logger` as if it's an usual class member.
  def run
    logger.info 'running ...'
  end
end

# Running our application, type:
#
#     ruby docs/basics.rb
#
# And You should see in the console something like this:
#
#     [2011-08-16T19:09:05.921238 #24944]  INFO -- : running ...
#
Application.new.run

Advanced example

It's hard to see advantages of Dependency Injection using trivial example, so this example is more complicated.

Let's pretend that we are building the Ultimate Web Framework, RoR Killer. There will be lot's of modules and dependencies, let's see how Micon can eliminate them.

We build our framework in two steps:

You can compare these two examples and see advantages of using Dependency Injection.

If You are interested in more samples, please take a look at the Rad SBS it's build using Micon.