Question

I have a fairly simple console app that monitors an exchange mailbox, picks particular emails out, and updates a couple of databases based on the contents.

I would like to implement a couple of similar systems. While it would be very simple to duplicate this system, I am looking at a more sophisticated solution - mainly an intellectual exercise, a learning exercise.

I would like to build a core application that pulls template information periodically from a DB; this information would tell the app that is has to monitor a given mailbox for emails with given characteristics at a particular interval.

I envision creating a master template (assembly) with some virtual functions (pre-processing, process items, archive items, send notifications etc). In turn, I'd create any number of templates that implement the interfaces in the master template, but the functionality could differ wildly in each case, one may update a database, while the next might store something in a file system.

My first question is whether this is a sensible implementation?

My second question is how to dynamically reference each template, and how would I call the methods of these templates at the appropriate time?

If I were to extend my Templates project, adding a new class for each new template required, I'd overcome the problem of dynamically referencing the templates. But if I wanted to keep them in separate assemblies.. Is there a way to just drop them into the project? Don't forget, the templates will be listed in a DB, so the app will be aware of them, but how to make use of them...

UPDATE: I've figured how I can dynamically reference each template class; it requires me to supply the Assembly-Qualified Name to GetType: I've tried to dynamically generate the template in the main app:

string aqn= "MasterTemplates.TestTemplate, TestTemplate, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";
MasterTemplate mt = (MasterTemplate)Activator.CreateInstance(Type.GetType(aqn));

So if I keep updating my MasterTemplates project, adding in new classes as necessary, I can achieve what I am aiming for. However, how can I handle different template assemblies?

In the meantime, I'm shortly going to look at DBM's suggestion of the Managed Extensibility Framework.

Conclusion: I don't have the time to fully investigate MEF; though it's overkill for my current needs, it looks extremely promising. And I haven't figured how to easily develop and use different assemblies for different templates - instead I am keeping all templates in one assembly, which I will have to recompile and up-date each time I require a new template. Not quite as sophisticated as the MEF alternative, but simpler and suited to my current needs.

Was it helpful?

Solution

When using activator with a string, use the Activator.CreateInstance(String,String) overload.

Alternatively you can create an instance of the type and use it like that:

Activator.CreateInstance(Type.GetType(templateName));

OTHER TIPS

You could use MEF to dynamically load plugins. It's in-the-box in VS2010, and works great for dynamically loading assemblies.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top