views:

1308

answers:

7

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.

My question is: how would I code this on C#? Is it a case of component-driven development? Should I use dynamic libraries?

+5  A: 

Managed Extensibility Framework is what you need here. You could also use a Dependency Injection container, but that's a bit not what you'd expect, though a perfectly viable solution in itself.

Anton Gogolev
Promising indeed, but the idea of under development makes it not good for this project, as it's for work.
Rodrigo
MEF will be (and actually is) used inside Visual Studio 2010 and will ship as part of .NET Framework 4.0
Anton Gogolev
+2  A: 

Each sensor should implement a standard interface so that routines that handle lists of sensors can treat them in a standard manner. Include a ID field in the interface that is unique too each type of sensor so you can handle special cases.

Look at the Reflection API to learn how to scan a directory of .NET Assemblies and look inside them.

Each assembly should have a factory class that it's job is to return a list of sensors that are in that assembly. I recommend that you make it a subroutine not a function and passes it a list that that it appends too. SensorDLL1 appends 4 sensors to the emptylist, SensorDLL2 appends 8 sensor to the list which now has 12 sensors and so on. This approach is the most flexible in the long run.

You will either have to make up a naming convention to find the factory class or use an attribute. Note I don't recommend just scanning the assembly for everything that implements your sensor interface as you could have code inside the factory that controls which sensors are available. This is useful for licensing.

RS Conley
+5  A: 

There are a huge number of ad-hoc plug-in systems for C#. Here is one at Code Project. The general approach is that the host app publishes an assembly with interfaces. It enumerates through a folder and finds assemblies that define 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 app 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 app and deliver it to someone else. They can author a plug-in in their development environment, create a shortcut to it in the app's plug-ins folder and not have to worry about deploying after each compile.

plinth
Do you know how much of this MEF is capable of?
Daniel Straight
A: 

Just remember that MEF is still in testing so, personally, I don't think it is a good idea to include this framework at work yet. If this is a personal project then MEF will work nicely.

Otherwise I fully agree with RS Conley's answer.

FailBoy
+1  A: 

Depending upon the Sensors themselves, this sounds like you would need to define a single interface which all Sensors will implement. Your main "application skeleton" will then work against the ISensor interface and need not concern itself with the concrete implementations of each of the Sensor classes/objects/components.

Whether each Sensor is simply a class within the same project, or a separate assembly is up to you, although if they are separate assemblies, you'd need a way to load these assemblies dynamically.

Some references which may help here are:

Command Pattern Design Pattern: - http://en.wikipedia.org/wiki/Command_pattern

Observer Pattern Design Pattern: - http://en.wikipedia.org/wiki/Observer_pattern

Dynamically loading assemblies: - http://www.divil.co.uk/net/articles/plugins/plugins.asp

Hope this helps.

CraigTP
A: 

We once made a plug-in system in a school project of ours in 2006, Socio. You can find the code here and here.

The basic lesson learned was that it is very simply to dynamically load code in C#. If you just have a plugin DLL and an application which adheres to an interface of yours and links against a common DLL in which that interface exists, it just works™.

In essence, it is what plinth described in his answer.

Adam Lindberg