Accessing Host from Plugin

Nov 21, 2011 at 8:36 PM

Hi, so another question I'm pondering on plugins.

So say I have an Interface setup something like this:

<System.ComponentModel.Composition.InheritedExport(GetType(IPlugin))> _
Public Interface IPlugin
     Sub SubInMyPlugin()
     Property Host() As IPluginHost
End Interface

Public Interface IPluginHost
     Sub SubInMyHostApp1()
End Interface

In my host app I can set "Host" to my Apps ref ie "Me".

Then in the plugin I can call:

host.SubInMyHostApp1()

So that works OK.

But what if I want to add SubInHostApp2 later and make it available to only certain plugins. Or even add to the interface without breaking existing compiled plugins.

Is there a way to do this sort of thing using MEF?

Ideally from my plugin I want to be able setup my host object and then do something like:

host.ServicesSet1.SubInMyApp1()

host.ServicesSet2.SubInMyApp2()

Any ideas?

Regards

Julian

 

 

 

Coordinator
Nov 21, 2011 at 9:00 PM

Hi Julian,

Again, hope that I have understood your question correctly, from what I understand you are asking if it would be possible for MEF to allow you to update an interface without having to recompile existing implementations of that interface and getting it to work.

Unfortunately this is beyond the scope of MEF. Do be able to selectively add methods and types, you might need to look at something like DLR (Dynamic Language Runtime). Again correct me if I have completely misunderstood your question. Also Nick can jump in, in case there is any further exposition required. J

Hth

-alok

From: juwi_uk [email removed]
Sent: Monday, November 21, 2011 1:37 PM
To: Alok Shriram
Subject: Accessing Host from Plugin [MEF:280320]

From: juwi_uk

Hi, so another question I'm pondering on plugins.

So say I have an Interface setup something like this:

<System.ComponentModel.Composition.InheritedExport(GetType(IPlugin))> _
Public Interface IPlugin
Sub SubInMyPlugin()
Property Host() As IPluginHost
End Interface

Public Interface IPluginHost
Sub SubInMyHostApp1()
End Interface

In my host app I can set "Host" to my Apps ref ie "Me".

Then in the plugin I can call:

host.SubInMyHostApp1()

So that works OK.

But what if I want to add SubInHostApp2 later and make it available to only certain plugins. Or even add to the interface without breaking existing compiled plugins.

Is there a way to do this sort of thing using MEF?

Ideally from my plugin I want to be able setup my host object and then do something like:

host.ServicesSet1.SubInMyApp1()

host.ServicesSet2.SubInMyApp2()

Any ideas?

Regards

Julian

Nov 22, 2011 at 8:18 AM
Edited Nov 22, 2011 at 8:19 AM

Hi,

No that's not what i mean here.

At the moment I have in my code:

Public Interface IPluginHost
     Sub SubInMyHostApp1()
     Sub SubInMyHostApp2()
     Sub SubInMyHostApp3()
     Sub SubInMyHostApp4()
     ...
End Interface

So in a plugin I can execute the host subrotines via:

    host.SubInMyHostApp1()
    host.SubInMyHostApp2()
    host.SubInMyHostApp3()
    host.SubInMyHostApp4()

What I'm trying to say though (badly it seems!! :0) )  is that in my plugin at the moment the plugin has access to all of the above subs in the host app even if it doesnt need that feature for the plugin.

I was wondering if MEF would help somehow in being able to partition interfaces somehow so that plugins could only see what they need.  Also perhaps I can introduce a new set of host "hooks" in alter release,  adding to a new version of the interface and recompiling.

<System.ComponentModel.Composition.InheritedExport(GetType(IPlugin))> _
Public Interface IPlugin
Sub SubInMyPlugin()
Property Host1() As IPluginHost1
Property Host2() As IPluginHost2
End Interface

Public Interface IPluginHost1
    sub SubInMyHostApp1()
    sub SubInMyHostApp2()
     ...
End Interface

Public Interface IPluginHost2 
    sub SubInMyHostApp3()
    sub SubInMyHostApp4()
     ...
End Interface

..set Host and Host2 properties  to address of host app and then in plugin call as:

    host1.SubInMyHostApp1()
    host1.SubInMyHostApp2()
    host2.SubInMyHostApp3()
    host2.SubInMyHostApp4()

Obviously I could rename "HostX" to "ServiceX" maybe to be a bit nicer but still it doesnt feel right to me, and I was looking for a cleaner solution.  It might  not be MEF related at all rather my skill level on doing this sort of thing with Interfaces but hey that's my question!   :0)

I've don't have a problem recompiling the Interface as I add in new hooks as part of my main application development but what I dont want to do is break existing plugins if I do.

Julian

Coordinator
Nov 22, 2011 at 5:59 PM

Wow talk about completely misunderstanding a question. Sorry for the probably tangential response. J

Again paraphrasing your question currently your plugin to have access to all the methods in an interface and you don`t want that.

You want to be able to specify some group of methods for pluginA and some other group of methods for pluginB.

You should be able to do this by specifying multiple exports.

For instance you can we can partition the IPluginHost methods into IFoo and IBar and have

[c# style pseudo code]

Public interface IFoo

Void subinmyhostApp1()

Void subinmyhostApp2()

Public interface IBar

Void subinmyhostapp3

Void subinmyhostapp4

[Export(typeof(IFoo)]

[Export(typeof(IBar)]

Public interface Ipluginhost: Ifoo,IBar

Now in your plugin you depending on what you want to be available you can either do an import an Ifoo or an IBar, while using the ipluginhost like so.

Public class SomeHostClassA

{

[Import(typeof(IFoo))]

Public IPluginHost pluginHost {get;set;}

}

Public class SomeHostClassB

{

[Import(typeof(IBar))]

Public IPluginHost pluginHost {get;set;}

}

The partitioning of what goes into IFoo and IBar is something specific to your application, and would have to be your decision.

Hope that comes closer to answering your question.

Cheers

-alok

From: juwi_Uk [email removed]
Sent: Tuesday, November 22, 2011 1:18 AM
To: Alok Shriram
Subject: Re: Accessing Host from Plugin [MEF:280320]

From: juwi_Uk

Hi,

No that's not what i mean here.

At the moment I have in my code:

Public Interface IPluginHost
Sub SubInMyHostApp1()
Sub SubInMyHostApp2()
Sub SubInMyHostApp3()
Sub SubInMyHostApp4()
...
End Interface

So in a plugin I can execute the host subrotines via:

host.SubInMyHostApp1()
host.SubInMyHostApp2()
host.SubInMyHostApp3()
host.SubInMyHostApp4()

What I'm trying to say though (badly it seems!! :0) ) is that in my plugin at the moment the plugin has access to all of teh above subs in the host app even if it doesnt need that feature for the plugin.

I was wondering if MEF would help somehow in being able to partition interfaces somehow so that plugins could only see what they need. Also perhaps i can introduce a new set of host "hooks" in a lter release, adding to a new version of the interface and recompiling.

<System.ComponentModel.Composition.InheritedExport(GetType(IPlugin))> _
Public Interface IPlugin
Sub SubInMyPlugin()
Property Host1() As IPluginHost1
Property Host2() As IPluginHost2
End Interface

Public Interface IPluginHost1
sub SubInMyHostApp1()
sub SubInMyHostApp2()
...
End Interface

Public Interface IPluginHost2
sub SubInMyHostApp3()
sub SubInMyHostApp4()
...
End Interface

..set Host and Host2 properties to address of host app and then in plugin call as:

host1.SubInMyHostApp1()
host1.SubInMyHostApp2()
host2.SubInMyHostApp3()
host2.SubInMyHostApp4()

Obviously I could rename "HostX" to "ServiceX" maybe to be a bit nicer but still it doesnt feel right to me, and I was looking for a cleaner solution. It might not be MEF related at all rather my skill level on doing this sort of thing with Interfaces but hey that's my question! :0)

I've don't have a problem recompiling the Interface as I add in new hooks as part of my main application development but what i dont want to do is break existing plugins if I do.

Julian

Nov 25, 2011 at 7:01 AM

Hi Alok,

Thank you for the prompt reply.

Would it be possible for you to expand on your answer a bit more.

Specifically could you just expand on the "Pluginhost" property in the plugin and how you would assign it the reference to the host application.  It also seems this properyty isnt tied to a contract ; is that intended of if not whicj Intereface would hold the property?

Hope that makes sense.

Regards

Julian