Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System with plugins in C#

Tags:

c#

plugins

I have to develop a system to monitor sensor information, but many sensors might be added in the future.

That said, the idea would be to develop a system that would consist of the application skeleton. The sensors (as each of them has its communication and data presentation characteristics) would be added as plugins to the system.

How would I code this on C#? Is it a case of component-driven development? Should I use dynamic libraries?

like image 457
Rodrigo Avatar asked Feb 05 '09 13:02

Rodrigo


People also ask

How do plugin systems work?

plug-in, also called add-on or extension, computer software that adds new functions to a host program without altering the host program itself. Widely used in digital audio, video, and Web browsing, plug-ins enable programmers to update a host program while keeping the user within the program's environment.


1 Answers

There are a huge number of ad-hoc plug-in systems for C#. One is described in Plugin Architecture using C# (at The Code Project). The general approach is that the host application publishes an assembly with interfaces. It enumerates through a folder and finds assemblies that define a class that implement its interfaces and loads them and instantiates the classes.

In practice you want to do more. It's best if the host application defines two interfaces, an IHost and an IPlugIn. The IHost interface provides services that a plug-in can subscribe to. The IPlugIn gets constructed taking an IHost.

To load a plug-in, you should do more than simply get a plug-in. You should enumerate all plug-ins that are loadable. Construct them each. Ask them if they can run. Ask them to export APIs into the host. Ask them to import APIs from the host. Plug-ins should be able to ask about the existence of other plug-ins.

This way, plug-ins can extend the application by offering more APIs.

PlugIns should include events. This way plug-ins can monitor the process of plug-ins loading and unloading.

At the end of the world, you should warn plug-ins that they're going to go away. Then take them out.

This will leave you with an application that can be written in a tiny framework and implemented entirely in plug-ins if you want it to.

As an added bonus, you should also make it so that in the plug-ins folder, you resolve shortcuts to plug-ins. This lets you write your application and deliver it to someone else. They can author a plug-in in their development environment, create a shortcut to it in the application's plug-ins folder and not have to worry about deploying after each compile.

like image 143
plinth Avatar answered Nov 15 '22 11:11

plinth