lua-users home
lua-l archive

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

On Sun, Jun 8, 2014 at 4:06 AM, Michal Kottman <> wrote:
>> anybody knows some good dependency injection?
> Some helpful reading:

Wasn't really sure what dependency injection was but now I think I
have an idea -- that article make it sound like allowing for move
semantics (if you come from a C++ background).  To me it sounds like

Normally a constructor might take something like an int and "konstrukt
u an abject" that contains an array with the length you passed in that
int.  Dependency injection sounds like you write the constructor in a
way that lets you pass in a pre-constructed array into the containing
object.  The article says 'dependencies' are just another useless
marketing term for members of the class/type/object/instance.

SOooo... Let's consider something more interesting related to that.
Let's say you have an object type that allows for "dependency
injection".  The containing type calls an interface off of the
contained dependency.

Let's say the dependency you injected respects the length operator
being called on it.  (#object.array -> object.array.__length(object))

The original design behind the object type might have been oriented
for working with a real array and returning its raw length, but now
because of dependency injection you can pass in array-like things that
respect the same interface.  Maybe you want the array to be an object
that represents a row in a database (potentially network-connected),
and calling the length operator on that object gets the total number
of elements in that row.  Dependency injection sounds like a technique
for hooking in vastly different control flow, behind the same
interface the containing type uses.

Anyway, I imagine in C++ "dependency injection" would have more
limitations -- you'd have to go to greater lengths to support moving
in dependencies of polymorphic types.  In a language like Javascript
or Lua (duck-typing languages) it's a bit easier.

(I leave it to others to correct me where I'm wrong ~)