[Solved] Specifying a specific instance using IoC, possibly with Get(string key)

Topics: Bootstrappers & IoC
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.


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:

        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>();

        #endregion // Modules

        #region IPartImportsSatisfiedNotification Members

        public void OnImportsSatisfied() {
            NotifyOfPropertyChange(() => Modules);


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.

Jun 22, 2012 at 6:53 AM
Edited Jun 22, 2012 at 6:57 AM

Generally, if I need to get a specific instance of a class from the IoC-Container (in this case MEF), I export it "the default way". This would be the easiest solution for your problem, I think. As MEF allows you to specify multiple Export-attributes, a module export would look like this (assuming IModule has no InheritedExport-Attribute):


public class ModA : IModule

In this case, IoC.Get<ModA>() would (obviously) give you the ModA instance.


Another way would be to add methods to the shell that query all modules by type or by name:


public T GetModule<T>()
    return Modules.OfType<T>().FirstOrDefault();

public IModule GetModuleByName(string name)
    return Modules.FirstOrDefault(x => x.ModuleName.ToUpper() == name.ToUpper();


Of course the shell wouldn't be the best location to implement this, regarding single responsibility and so on, but it would suffice, as you only need to call IoC.Get<IShell>().GetModule<ModA>();

There is also a IoC.GetInstances call in CM's IoC class, but I am not sure if this only retrieves the already instanciated objects of a specific type or is the "enumerable" equivalent to IoC.Get<T>. You should check that (or wait for an answer from someone with greater knowledge).

Hope this helps!


Jun 22, 2012 at 9:46 PM
Edited Jun 23, 2012 at 9:39 PM

Thanks, this should suffice. I'll see if I can't reference the instances of my modules from my singleton session class instead of the shell.

"The default way" is how I have been doing it, but in order to call IoC.Get<ModA>() and not get an error I would need to have the reference to the ViewModel.

EDIT: Just added a variation of the GetModByName function and it works like a charm!