I’m using MVVM and PRISM. In the project, I’ve got a common interface called IFoo and others modules should implement this interface and registered it.
// Common module
public interface IFoo { }
// Module1 module
public class Foo1 : IFoo { }
Then when I initialize the module1, I register my type and navigate.
_container.RegisterType<IFoo, Foo1>(new ContainerControlledLifetimeManager());
_container.RegisterType<Object, View1>("View1");
var module = new Uri("View1", UriKind.Relative);
_regionManager.RequestNavigate("MainRegion", module);
View1 constructor contains viewModel, this view model has in its constructor:
public ViewModel1(IFoo foo, IEventAggregator eventAggregator, IRegionManager regionManager)
{
...
}
Until this, is okay. But later, I need to get the Foo1 from external modules. So, I set another registry to mapping name for Foo1:
_container.RegisterType<IFoo, Foo1>(new ContainerControlledLifetimeManager());
_container.RegisterType<IFoo, Foo1>("foo1", new ContainerControlledLifetimeManager());
And right, it’s working for me, but I don’t like the idea to have two instances separated. I need to have just one, and accesing to the same instance.
Is there a way to fix this scenario?
Thanks in advance.
Anyway, I attach a Zip where contains a demo which represents my problem.
http://www.mediafire.com/?feod8x0b952457e
You can register all your types in the bootstrapper when you load the modules.
Then in
ConfigureContaineryou map all the types and/or instances you want to access later on. The configured container is passed into your constructor for Module1Module.