lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hello,

Gear is available at github[1] or as lua-rock [2].

Inversion of Control (IoC) implementation in lua; if you know Spring
Framework in Java, than Gear a bit similar. IoC allows to get rid
of singleton anti-pattern, and more easily manage runtime dependencies.

The Dependency Injection is pluggable, in underlying Object Oriented
library supports that.

Example of solving Chicken-Egg problem with gear:

==========================
local Gear = require "gear"
local gear = Gear.create()
gear:declare("chicken", {
  dependencies = {"egg"},                                            -- optional
  constructor  = function() return { class = "chicken" } end,
  initializer  = function(g, instance, egg) instance.egg = egg end,  -- optional
})
gear:declare("egg", {
  dependencies = {"chicken"},
  constructor  = function() return { class = "egg" } end,
  initializer  = function(g, instance, chicken) instance.chicken = chicken end,
})

-- solved!
local chicken = gear:get("chicken")
print(chiken.class)
print(chiken.egg.class)
local egg = gear:get("egg")
print(egg.class)
print(egg.chiken.class)
==========================


In the new version the "provides" prorerty has been added, i.e. now it
is possible to do something like that:

==========================
local gear = Gear.create()
gear:declare("nuclear-reaction", {
  dependencies = {"Uranium-238"},
  provides     = {"Thorium-234", "Helium-4"},
  constructor  = function() return "process/nuclear-reaction" end,
  initializer  = function() return "element/Thorium-234",
"element/Helium-4" end,
})
gear:declare("Uranium-238", {
  constructor  = function() return "element/Uranium-238" end,
  initializer  = function() end,
})

-- Oh, yes!

local thorium = gear:get("Thorium-234") -- element/Thorium-234
local helium = geag:get("Helium-4")     -- "element/Helium-4"
==========================



[1] https://github.com/basiliscos/lua-gear
[2] https://luarocks.org/modules/basiliscos/gear