views:

611

answers:

2

I see parallels between MEF and Lua. Both allow you to register methods and deploy as you need. Are both MEF and Lua forms of IoC / Dependency Injection?

+3  A: 

MEF has nothing to do with Lua, and is nothing like Lua.

MEF is a framework for extending (basically, an awesome plugin framework).

Lua is a very cool scripting language.

"Both allow you to register methods and deploy as you need." That sentence applies to C, C++, C#, VB, SQL, DI Frameworks, JavaScript, General Motors, Ford, Hospitals...

Timothy Khouri
I don't understand "General Motors, Ford, Hospitals ..."
David Robbins
I think I got carried away. Sometimes I get way too cynical.
Timothy Khouri
+1 for Hospitals...
Yar
+6  A: 

I'll assume you're aware of the huge differences between these technologies and focus on the question:

"Are both MEF and Lua forms of IoC / Dependency Injection?"

Also, I'll assume you're talking about embedded Lua versus Lua as a language.

First, let's separate Dependency Injection from Inversion of Control. Fowler defined Dependency Injection as a specific form of IoC because the idea of IoC had become so common that it was no longer a distinguishing characteristic of a system. His definition includes three main types of Dependency Injection: Constructor injection, Setter injection, and Interface injection. In all three types, the idea is to inject a specific implementation of a class or interface into a class or method that needs it. This is pretty slick because it allows you to decouple the dependency and the class that uses it. As long as they follow a contract, you can edit and swap implementations of the dependency without its consumers caring or being effected.

Using this definition, I'd say MEF passes and embedded Lua fails. MEF is largely a dependency injection framework. It allows you to dynamically load and compose external classes that implement specific contracts. Lua, on the other hand, allows extension through scripting but there is very little in the way of a contract. Sure, you could provide a Lua API for your app and that's a contract of sorts, but it does nothing to ensure a true contract is honored.

IoC is broader.(Fowler,Wikipedia) The common theme is that main program flow gives up control temporarily but receives flow status updates from components that are doing the work. Common ways to accomplish this include: events, closures, and continuations.

Using this definition, MEF easily passes (control is passed to unknown components at runtime) and you can make an argument for embedded Lua as well. The main program cruises along until it needs a function defined in external script. At that point, control is delivered to the script until it's done or is interrupted.

One thing to note is that Lua isn't particularly special in this regard. You can embed Perl, Python, Tcl, and Ruby. In fact, the general definition of IoC isn't particularly useful in a modern programming environment. It's too common. Fowler says that's why he introduced Dependency Injection as a special case. In a world of GUIs, events, threads, daemons, closures, continuations, and monads, everything uses IoC. Today, when people say 'IoC', they often mean some sort of Dependency Injection.

Corbin March