Jun 19, 2012 at 9:56 PM
Edited Jun 21, 2012 at 2:19 PM
|
I have a modular project using IoC with a few various assemblies. Normally, I would just do IoC.Get<IShell>() or <SomeViewModel> whenever I need it. In this case however, I want to get one IModule from another without loading up the other ViewModel's
assembly to ShowWindow with.
IModule.cs:
public interface IModule {
string ModuleName { get; }
}
When I call IoC.Get<IModule>() it returns only the first of 3 modules I have loaded. I would like to get either a list of the 3 so I can compare ModuleName or be able to specify which one to grab. I have a shell view that loads all of the modules based
on a tutorial that I've since lost the link to which properly displays all 3 of them:
[ImportMany(typeof(IModule))]
private IEnumerable<IModule> _modules;
private IModule _selectedModule;
private RelayCommand _showModuleCommand = null;
#region Modules
public IEnumerable<IModule> Modules {
get { return _modules; }
}
public IModule SelectedModule {
get { return _selectedModule; }
set { _selectedModule = value; }
}
public ICommand ShowModuleCommand {
get {
if (_showModuleCommand == null)
_showModuleCommand = new RelayCommand(a => ShowModuleExecuted(), a => ShowModuleCanExecute());
return _showModuleCommand;
}
}
private bool ShowModuleCanExecute() {
if (_selectedModule != null)
return true;
return false;
}
private void ShowModuleExecuted() {
var windowManager = IoC.Get<IWindowManager>();
windowManager.ShowWindow(_selectedModule);
}
#endregion // Modules
#region IPartImportsSatisfiedNotification Members
public void OnImportsSatisfied() {
NotifyOfPropertyChange(() => Modules);
}
#endregion
I'm pretty new to MEF and IoC concepts. Am I even working in the right direction? Let me know if you need anymore information.
EDIT: I would like to clarify as I've not gotten any responses yet a sort of use case. I have a project MyApplication that contains the MefBootstrapper and ShellView. I load up 3 different modules, we'll call them ModA, ModB, and ModC that each reside in
their own projects containing their own views/viewmodels. Finally I have another project MyClasses that contains all of the utility classes and shared views/viewmodels like commonly used dialogs.
The application starts by loading up the shell view and populating it with a list of modules. When you select a module and click a button it takes you to the main viewmodel of that module. I also have a login window from MyClasses that pops up via the OnStartup
method of the bootstrapper.
After logging in, I select ModA which takes me to a sort of File Manager I've created. ModB and ModC are different means through which to interact with the files. When I select a file and click open I am prompted with a popup that asks in what way I wish
to interact with the file. Both options load the file's information into a singleton object in MyClasses that is available across the entire application, but they both lead to their respective modules.
ModA I wanted to avoid adding ModB and ModC as references in ModA because the 3 modules are all available through an IModule interface through the IoC. However, when I call IoC.Get<IModule>() it only returns ModA. I've noticed there is an IoC.Get<T>(string
key) but I can't find any documentation on how to use it and suspect it may be the answer to my problem. Another method that returns all of the IModules exported to the IoC as a list would also suffice. Then I could simply loop through them till I find the
desired ModuleName.
Please advise.
|