Question

I am currently building an MEF project so I can learn how to use it in practical applications. I have successfully imported a DataHandler module to provide database connections and other centralized database operations.

Now I am thinking about a way to provide methods to other imported modules. For instance, if a Security Module requires a connection to a database, it would be able to request a database connection. Now, I have a way for a module to detect if a DataHandler is available as the Host application makes that public. So I am wondering if there were a way to use EventAggregator to publish a type of event where the subscriber (Security Module) simply fires an event and gets a DbConnection object in return.

If EventAggregator is the answer, I could use some hints as to how. Keep in mind that I am still researching...not just asking for teh codez. If there is a better solution out there, I am interested in options.

I am currently reviewing this post by Denis Vuyka

Edit

EventAggregator looks like it might not be the right tool to use. Is there some sort of loosely coupled messaging that I can implement in my Host app? Is a Service locator or Command pattern something that can be implemented?

Was it helpful?

Solution

I pass my configuration to all modules by Exporting a main app interface. You could have single instance of your db interface hosted in the main app.

The host application exports its self and all the plugins import it and have access to its interface. See my example below:

Interface:

public interface IMainApp
{
    ConfigClass Config { get; set; } 
}

Host App:

[Export(typeof(IMainApp))]
public class Host : IMainApp
{
    public Host()
    { /* Compose parts etc here..? */ }

    public ConfigClass Config { get; set; }  

    [Import(typeof(IModule))]
    public List<IModule> LoadedModules { get; set; }
}

Plug-in assembly:

[Export(typeof(IModule))]
public class Module : IModule
{        
    public Module() { }

    [Import(typeof(IMainApp))]
    public IMainApp Host { get; set; } 

    public void DoSomething()
    {
        Host.Config... // use config here
    }
}

OTHER TIPS

It sounds like you are looking for a Service Locator, not an EventAggregator.

But why request services through a service locator if you already injecting those services with MEF? If you need to acquire services later at run-time, you can inject a Lazy<T> or a factory instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top