plugin-based applications with plugins that modify other plugins
I am taking a look at how to build modular, plugins-based applications in C#. I am reading about Prism and MEF (that I've already used in some projects of mine).
All examples and articles that I found talk about discrete modules. My question is just about this. Let's say that one of the modules doesnt provide new views, but it "simply" needs to alter an existing view, which is provided by a different module, by adding one or more fields and some further logic. How would you do that?
Would it be a good approach to check at the composition-time which other parts are present in the catalog, and programmatically modify them?
I can think of this as a possible solution if there is a "module 1" and a "module 2" that changes something in "module 1". But if the scenario gets far more complex? for example if we have a basic "module 1" that has to be modified by "module 2" and "module 3", but also it exists a "module 4" that modifies the UI and logic provided by "module 2", and so on... ?
Could you advice me on how this can be realized?
Thanks in advance,
Cheers,
Gianluca
From your example I infer that you have exports that look a little bit like this, for which you do an [ImportMany]
somewhere:
[Export(typeof(IView))]
public class BarView : IView
{
...
}
Suppose one particular view implementation needs to be pluggable itself. Then you could do something like this:
[Export(typeof(IView))]
public class FooView : IView
{
[ImportMany(IFooViewPlugin)]
public IEnumerable<IFooViewPlugin> Plugins { get; set; }
...
}
Of course, it is still up to you to shape IFooViewPlugin
and decide how FooView
invokes it to customize itself. That depends on what kind of customizations you had in mind.
You can have the host application expose the collection of plugins via say a "Plugins" property. Then your plugin can iterate through the plugins collection till you find one of the right type and then modify it.
For security, you may want to add some test code in the plugin being modified (say set a password) to prevent modifications by unwanted plugins.
精彩评论