What's the right way to do this?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm fairly new to .NET, and I'm trying to figure out the best way to set up
the communication between my application and its plugins. I am loading the
plugin assemblies when the application starts up. The problem I am trying to
figure out is how to allow the plugins to use some functions in the main
application.

For example, the main application has an event logging function. I would
like for the plugins to be able to call this function whenever they need to.
Is it possible to use delegates for this? Is there some other way that such
a thing should be done?

Thanks!
 
Hello Bagger,

You could look at implementing a service container that your plugins could
use. The service container would essentially just be a hashtable of types
and instances. Your plugins then could do something like:

IEventLogger logger = serviceContainer.GetService(typeof(IEventLogger));
 
Matt,

Ok, I think I understand what you're getting at, but I'm not sure if it's
overkill for what I'm doing or not. I haven't had any experience with
service containers, but I did read that they can be used for pluggable
architectures. I did some googling, but haven't found anything that explains
how to build and use a service container. Could you suggest a link or book
that covers it? Thanks for the help.
 
Hello Bagger,

A simple service container would be this:

[C#]
public class ServiceContainer
{
private Hashtable services = new Hashtable();

public void AddService(Type serviceType, object serviceInstance)
{
services.Add(serviceType, serviceInstance);
}

public object GetService(Type serviceType)
{
return services[serviceType];
}

// Singleton pattern
private ServiceContainer() {}
private static ServiceContainer instance = new ServiceContainer();
public static ServiceContainer Instance { get { return instance; } }
}

Your plugins would then use something like:

IEventLogger logger = (IEventLogger)ServiceContainer.Instance.GetService(typeof(IEventLogger));

Note that I chose to implement the Singleton pattern for this, which would
require that your service container class reside in an assembly referenced
by your plugins. For this reason, if I go with the Singleton pattern, I usually
put it in the same assembly that includes the plugin interfaces that my plugins
are expected to derive from.

Hope this helps... Let me know if you need more.
 
Matt,

This is starting to make sense to me now. Let me make sure that I'm getting
it right though. First, my main app will create a ServiceContainer object,
and add its services to it. Basically, it would have to pass the interface
name, and a reference to itself? Is that correct? Then, the plugins create
ServiceContainer objects themselves and request an object that implements
whatever interface they specify? They then use that object to access the
services they need. Am I close on this? Thanks!


Matt Berther said:
Hello Bagger,

A simple service container would be this:

[C#]
public class ServiceContainer
{
private Hashtable services = new Hashtable();

public void AddService(Type serviceType, object serviceInstance)
{
services.Add(serviceType, serviceInstance);
}

public object GetService(Type serviceType)
{
return services[serviceType];
}

// Singleton pattern
private ServiceContainer() {}
private static ServiceContainer instance = new ServiceContainer();
public static ServiceContainer Instance { get { return instance; } }
}

Your plugins would then use something like:

IEventLogger logger = (IEventLogger)ServiceContainer.Instance.GetService(typeof(IEventLogger));

Note that I chose to implement the Singleton pattern for this, which would
require that your service container class reside in an assembly referenced
by your plugins. For this reason, if I go with the Singleton pattern, I usually
put it in the same assembly that includes the plugin interfaces that my plugins
are expected to derive from.

Hope this helps... Let me know if you need more.

--
Matt Berther
http://www.mattberther.com
Matt,

Ok, I think I understand what you're getting at, but I'm not sure if
it's overkill for what I'm doing or not. I haven't had any experience
with service containers, but I did read that they can be used for
pluggable architectures. I did some googling, but haven't found
anything that explains how to build and use a service container.
Could you suggest a link or book that covers it? Thanks for the help.
 
Hello Bagger,

If you use the Singleton approach like I detailed below, your app would simply
be responsible for adding services to the container.

ServiceContainer.Instance.AddService(typeof(IEventLogger), myEventLoggerInstance);

Plugins would then retreive services using

ServiceContainer.Instance.GetService(typeof(IEventLogger));

--
Matt Berther
http://www.mattberther.com
Matt,

This is starting to make sense to me now. Let me make sure that I'm
getting it right though. First, my main app will create a
ServiceContainer object, and add its services to it. Basically, it
would have to pass the interface name, and a reference to itself? Is
that correct? Then, the plugins create ServiceContainer objects
themselves and request an object that implements whatever interface
they specify? They then use that object to access the services they
need. Am I close on this? Thanks!

Matt Berther said:
Hello Bagger,

A simple service container would be this:

[C#]
public class ServiceContainer
{
private Hashtable services = new Hashtable();
public void AddService(Type serviceType, object serviceInstance)
{
services.Add(serviceType, serviceInstance);
}
public object GetService(Type serviceType)
{
return services[serviceType];
}
// Singleton pattern
private ServiceContainer() {}
private static ServiceContainer instance = new ServiceContainer();
public static ServiceContainer Instance { get { return instance; } }
}
Your plugins would then use something like:

IEventLogger logger =
(IEventLogger)ServiceContainer.Instance.GetService(typeof(IEventLogge
r));

Note that I chose to implement the Singleton pattern for this, which
would require that your service container class reside in an assembly
referenced by your plugins. For this reason, if I go with the
Singleton pattern, I usually put it in the same assembly that
includes the plugin interfaces that my plugins are expected to derive
from.

Hope this helps... Let me know if you need more.

--
Matt Berther
http://www.mattberther.com
Matt,

Ok, I think I understand what you're getting at, but I'm not sure if
it's overkill for what I'm doing or not. I haven't had any
experience
with service containers, but I did read that they can be used for
pluggable architectures. I did some googling, but haven't found
anything that explains how to build and use a service container.
Could you suggest a link or book that covers it? Thanks for the
help.
:

Hello Bagger,

You could look at implementing a service container that your
plugins could use. The service container would essentially just be
a hashtable of types and instances. Your plugins then could do
something like:

IEventLogger logger =
serviceContainer.GetService(typeof(IEventLogger));
--
Matt Berther
http://www.mattberther.com
I'm fairly new to .NET, and I'm trying to figure out the best way
to
set up the communication between my application and its plugins.
I
am loading the plugin assemblies when the application starts up.
The problem I am trying to figure out is how to allow the plugins
to
use some functions in the main application.
For example, the main application has an event logging function.
I would like for the plugins to be able to call this function
whenever they need to. Is it possible to use delegates for this?
Is there some other way that such a thing should be done?

Thanks!
 
Matt,

Thanks for all the help. I'm writing this in VB.NET, but once I got your
example translated, and figured out how to do a couple of other things in VB,
I was able to set up a quick and dirty test, which seems to be working. I
don't have any actual plugins built yet, so that's my next task, to
dynamically load a plugin at runtime and have it log events through the
service container mechanism. I may be back with more questions tonight or
tomorrow, but I appreciate all your help. I might learn how to do this
stuff yet. :)
 
Back
Top