<p>First off, there's a bit of background to this issue available on my blog:</p>

<ul>
<li><a href="http://www.codebork.com/coding/2008/06/25/message-passing-a-plug-framework.html" rel="nofollow noreferrer">http://www.codebork.com/coding/2008/06/25/message-passing-a-plug-framework.html</a></li>
<li><a href="http://www.codebork.com/coding/2008/07/31/message-passing-2.html" rel="nofollow noreferrer">http://www.codebork.com/coding/2008/07/31/message-passing-2.html</a>  </li>
</ul>

<p>I'm aware that the descriptions aren't hugely clear, so I'll try to summarise what I'm attempting as best I can here.  The application is a personal finance program.  Further background on the framework itself is available at the end of this post.</p>

<p>There are a number of different types of plug-in that the framework can handle (e.g., accounts, export, reporting, etc.).  However, I'm focussing on one particular class of plug-in, so-called data plug-ins, as it is this class that is causing me problems.  I have one class of data plug-in for accounts, one for transactions, etc.</p>

<p>I'm midway through a vast re-factoring that has left me with the following architecture for data plug-ins:</p>

<ul>
<li>The data plug-in object (implementing intialisation, installation and plug-in metadata) [implements <code>IDataPlugin&lt;FactoryType&gt;</code>]  </li>
<li>The data object (such as an account) [implements, e.g., <code>IAccount</code>]  </li>
<li>A factory to create instances of the data object [implements, e.g., <code>IAccountFactory</code>]</li>
</ul>

<p>Previously the data object and the plug-in object were combined into one, but this meant that a new transaction plug-in had to be instantiated for each transaction recorded in the account which caused a number of problems.  Unfortunately, that re-factoring has broken my message passing.  The data object implements <code>INotifyPropertyChanged</code>, and so I've hit a new problem, and one that I'm not sure how to work around: the plug-in object is registering events with the message broker, but it's the data objects that actually fire the events.  This means that <strong>the subscribing plug-in currently has to subscribe to each created account, transaction, etc.!</strong>  This is clearly not scalable.  </p>

<p>As far as I can tell at the moment I have two possible solutions:</p>

<ol>
<li>Make the data plug-in object a go-between for the data-objects and message broker, possibly batching change notifications.  I don't like this because it adds another layer of complexity to the messaging system that I feel I should be able to do without.   </li>
<li>Junk the current event-based implementation and use something else that's more easily manageable (in-memory WCF?!).</li>
</ol>

<p>So I guess I'm really asking: </p>

<ol>
<li>How would you solve this problem?</li>
<li>What potential solutions do you think I've overlooked?</li>
<li>Is my approach even vaguely on-track/sensible?! :-)</li>
</ol>

<p>As you will be able to tell from the dates of the blog posts, some variant of this problem has been taxing me for quite a long time now!  As such, any and all responses will be greatly appreciated.  </p>

<p>The background to the framework itself is as follows:  </p>

<blockquote>
  <p>My plug-in framework consists of three main components: a plug-in broker, a preferences manager and a message broker.  The plug-in broker does the bread-and-butter plug-in stuff: discovering and creating plug-ins.  The preferences manager manages user preferences for the framework and individual plug-ins, such as which plug-ins are enabled, where data should be saved, etc.  Communication is via publish/subscribe, with the message broker sitting in the middle, gathering all published message types and managing subscriptions.  The publish/subscribe is currently implemented via the .NET <code>INotifyPropertyChanged</code> interface, which provides one event called <code>PropertyChanged</code>; the message broker builds a list of all plug-ins implementing <code>INotifyPropertyChanged</code> and subscribes other plug-ins this event.  The purpose of the message passing is to allow the account and transaction plug-ins to notify the storage plug-ins that data has changed so that it may be saved.  </p>
</blockquote>

[{'answer_id': 50944, 'author': 'Marcio Aguiar', 'author_id': 4213, 'author_profile': 'https://Stackoverflow.com/users/4213', 'pm_score': 2, 'selected': False, 'text': '<p>Wow! Big question! :)</p>\n\n<p>Correct me if I\'m wrong. Your basic solution now is kind of an Observer pattern, where the data object (Account, etc) notifies about changes in their states. You think that the problem is that the subscribing plugin has to register in every object to be able to handle notifications.</p>\n\n<p>That\'s not a problem per se, you can put the event control in the <a href="http://martinfowler.com/eaaCatalog/domainModel.html" rel="nofollow noreferrer">Domain Model</a>, but I suggest you create a <a href="http://martinfowler.com/eaaCatalog/serviceLayer.html" rel="nofollow noreferrer">Service Layer</a> and do this event notifications in this layer. That way just one object would be responsible for publishing notifications.</p>\n\n<p>Martin Fowler have a series of Event Patterns in his blog. <a href="http://www.martinfowler.com/eaaDev/" rel="nofollow noreferrer">Check it out</a>! Very good reading.</p>\n'}, {'answer_id': 50963, 'author': 'Dave Ward', 'author_id': 60, 'author_profile': 'https://Stackoverflow.com/users/60', 'pm_score': 1, 'selected': False, 'text': '<p>It\'s early yet, but have you considered trying to use <a href="http://www.codeplex.com/MEF" rel="nofollow noreferrer">MEF</a> instead of rolling your own?</p>\n'}, {'answer_id': 51104, 'author': 'dan gibson', 'author_id': 4495, 'author_profile': 'https://Stackoverflow.com/users/4495', 'pm_score': 3, 'selected': True, 'text': "<p>This is my understanding of your question: You have a plugin object that may have to listen for events on x data objects - you don't want to subscribe to the event on each data object though. I'm assuming that several plugins may want to listen to events on the same data object.</p>\n\n<p>You could create a session type object. Each plugin listens for events on the session object. The data object no longer raises the event - it calls the session object to raise the event (one of the parameters would have to be the data object raising the event).</p>\n\n<p>That means that your plugins only have to subscribe to one event, but they get the event from all data objects.</p>\n\n<p>On the other hand, if only one plugin will ever listen to a data object at a time, why not just have the data object call the plugin directly?</p>\n"}]
['https://Stackoverflow.com/questions/50822', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5296/']