Dynamic loading, D all the way (dmd 64bit 2.060/Ubuntu 64bit 12.04/x86_64)

Jacob Carlborg doob at me.com
Fri Aug 24 00:03:43 PDT 2012


On 2012-08-24 00:26, Philip Daniels wrote:

> But wouldn't that require you to link everything together at, err,
> compile time?
>
> What I'm getting at is, would it be possible to port a DI/IoC tool such
> as StructureMap (http://docs.structuremap.net/index.html) or Spring to
> D? This can handle tasks such as creating dynamic plug-in architectures.
> For example, given SomeBigApp.exe (not written by me) which looks in
> standard folders for components implementing a particular interface, I
> can just drop my code in that folder and have it loaded at runtime. I
> could even drop it in there after the program starts running. I know how
> to achieve this in the .Net world, just wondered if it was possible in D.

Sure, you just need to come up with a couple of conventions. Say for 
example that you have your application in a folder structure like this:

app
  |--plugins
      |---fully.qualified.class.name.so/dll/dylib

The host application inspects the "plugins" folder at runtime grabbing 
all the names of the dynamic libraries. The convention is that in the 
dynamic library there will be a class with the same fully qualified name 
as the name of the dynamic library its located in.

The next convention is that the class must implement an interface, which 
the host application provides. The class also needs to have a default 
constructor (there are ways around that).

Then the host application would do something like this:

interface Plugin
{
     void initialize ();
     // ... other needed methods
}

auto plugin = cast(Plugin) Object.factory("fully.qualified.class.name");
plugin.initialize();

The magic behind this is the "Object.factory" method:

http://dlang.org/phobos/object.html#factory

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list