Template / method for C # plugin functionality

I am working on a new new product that will ultimately have different functionalities created based on individual customer requirements. Therefore, I would like to realize the possibility of having plugins that I can deploy, and the main application will detect the plugins and change them accordingly. The kicker is that the product is likely to target earlier versions of .Net that do not support System.Addin or the MEF feature.

However, which programming patterns / methods are good for supporting plugins in C #? I would like to stay away from third-party frameworks, although feel free to call them. Ideally, I would like to be able to look into the plugin if it changes the main menu and / or context menu and / or provides a new service. In Java, this handled well through extension points. Am I mostly stuck to write my own extension point manager?

+4
source share
3 answers

You want to look at the System.Reflection namespace to help you in this task. One approach is to create a common interface for your plugin classes ( search strategy template ), and then use reflection at runtime to view the entire DLL in a given folder (possibly where the exe or a special add-in folder is located), noting the classes that contain this specific interface. In particular, keep a type dictionary (those that have your special interface that designates it as a plugin class). Once you have a list of those types that contain an interface that designates them as a plugin, you can dynamically install them as you wish using the System.Activator class. Do an initial exploration for add-in classes at application startup, and then use an abstract factory to load the plugin based on the type or some other metadata that can be assigned to the class using .net attributes.

If you need specific code examples, I can do it for you, but you should take the time to familiarize yourself with the attributes of System.Reflection, System.Activator and .net if you intend to build a reliable and flexible add-in system.

Enjoy it!

+2
source

I am a Java person, but since your question is related to software development patterns ...

A strategy template will probably help you here on a very abstract level. So, perhaps your main application has a very simple set of “strategies” that it uses to work ... instead of, say, creating a menu bar, you call a simple menu bar strategy object to create it for you. Then specify the methods for the swap strategies (so the deployment of the plugin will essentially consist of calling a bunch of setStrategy () methods on the affected objects, or whatever you like). The plugin will provide alternative strategies. As for loading plugin classes at runtime, I'm not familiar enough with C # to answer this question.

As I said, all this is very abstract. Try to avoid reflection as much as possible - although sometimes it is necessary, there are problems with the performance, security and design of using reflection.

Hope this helps!

+1
source

Almost any "plug-in" architecture will be based on some version of the Builder template - for the most part by definition.

0
source

Source: https://habr.com/ru/post/1313452/


All Articles