简体   繁体   English

实施插件/模板系统C#

[英]Implementing a plug-in/templating system c#

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. 如果要扩展我的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: 它要求我向GetType提供Assembly-Qualified名称 :我试图在主应用程序中动态生成模板:

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. 因此,如果我不断更新我的MasterTemplates项目,并根据需要添加新类,则可以实现我的目标。 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. 同时,我很快将看一下DBM关于托管可扩展性框架的建议。

Conclusion: I don't have the time to fully investigate MEF; 结论:我没有时间充分研究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. 不像MEF替代品那么复杂,但是更简单并且适合我当前的需求。

You could use MEF to dynamically load plugins. 您可以使用MEF动态加载插件。 It's in-the-box in VS2010, and works great for dynamically loading assemblies. 它在VS2010中是现成的,非常适合动态加载程序集。

When using activator with a string, use the Activator.CreateInstance(String,String) overload. 当将激活器与字符串一起使用时,请使用Activator.CreateInstance(String,String)重载。

Alternatively you can create an instance of the type and use it like that: 另外,您可以创建该类型的实例并像这样使用它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM