tags:

views:

103

answers:

5

I'd like to build an infrastructure that will monitor a server and check ping, response time, and more.
The catch is that future checks (for example: does a certain file exist) will be added without recompiling the infrastructure.

How do I build it in such a way that will enable me to attach inherited classes with different functionality, and execute them without recompiling?

Thanks!

A: 

Of the top of my head.

I presume you can re-start you application. Have a file that lists all the DLL's to load that implement your required functionality. Each DLL should have the same name entry point. Load each DLL, call the method, unload DLL. loop.

Caveat: I've never done anything like this, so I may be talking hot air.

Richard
+1  A: 

Implement an interface, and the provider pattern, then you can plug anything in that you like. MSBuild is a great example of this, with a simple interface you can add any type of task you like to your build process - follow the same sort of pattern.

slugster
A: 

Sounds like you could use some kind of 'plugin' mechanism. Define a basic interface and you can compile every "check/action" into a separate assembly. Load all your assemblies dynamically from file and call execute the check/action via the defined interface.

The interface could be just as simple as this, for starters:

public interface IMonitorAction
{
    bool Exectute();
}

This infrastructure allows you to add more checks by just creating another assembly file implementing the interface next to the existing ones.

Rob van Groenewoud
A: 

Adding to @slugsters answer, instead of building your own extensibility infrastructure, take a look at extensibility libraries like MEF.

Peter Lillevold
+2  A: 

In addition to creating an interface and defining a single entry point for your new library, you could create an attribute which identifies the classes that you need to load or the methods that you need to call. You then use reflection to look at all the DLLs in a certain path, and instantiate / run whatever contains your attribute.

I've built a similar application that had to perform a number of health checks on a system, and needed to be extensible. The application started, looked through all the DLLs in a specified path and for each class with the 'TestAttribute' decoration it would create an instance and run the 'Execute' method.

The use of an attribute means that you don't need to specify which DLLs to process (doesn't need to be in config / database) because it's safe to process every DLL, and only those decorated with the attribute will do anything.

Kirk Broadhurst
Again, there are libraries out there (I mentioned MEF) that already implements much of this plumbing.
Peter Lillevold
MEF looks interesting but isn't it overkill? This is a really simple problem; my solution here could literally be just 10 lines for an interface and 2 lines for an attribute declaration.
Kirk Broadhurst
You would also have to implement the "look for dlls"...in general requirements like this, I tend to look for libraries rather than roll my own. Could be that MEF does more than absolutely necessary in this case, but still, it will solve the problem at hand with even less code than you describe.
Peter Lillevold
That's true, but the flipside is learning curve and implementation time for MEF (or other framework). I typically try to understand how to do things myself in the first instance, simply because I think it's potentially risky to assume that you'll always have access (to install, deploy etc) to a specific third party framework.
Kirk Broadhurst
Thanks guys. Appreciate it.
Nir