views:

92

answers:

1

I'm using Prism and Unity.

I've got this bootstrapper:

protected override IModuleCatalog GetModuleCatalog()
{
    ModuleCatalog catalog = new ModuleCatalog()
        .AddModule(typeof(CustomerModule.CustomerModule))
        .AddModule(typeof(EmployeesModule.EmployeesModule))
        .AddModule(typeof(MenuModule.MenuModule));
    return catalog;
}

And my CustomerModule gets a MenuManager injected and adds menu items to it:

public void Initialize()
{
    menuManager.MenuItems.Add("Customers");
    menuManager.MenuItems.Add("Other Customers");
}

But when my MainMenuPresenter object also gets MenuManager injected, it is not the same object:

public MainMenuPresenter(MainMenuView view, MenuManager menuManager)
{
    View = view;
    View.DataContext = this;

    foreach (string menuItemTitle in menuManager.MenuItems)
    {
        MenuItems.Add(menuItemTitle);
    }
}

How do I tell Prism/Unity that I want the injected MenuManager to be a Singleton so that the same object is injected into each of my modules and objects?

+6  A: 

Using Unity, you do it like this (taken from MSDN on Lifetime Managers in Unity):

// Register a type to have a singleton lifetime without mapping the type
// Uses the container only to implement singleton behavior
myContainer.RegisterType<MySingletonObject>(new ContainerControlledLifetimeManager());
// Following code will return a singleton instance of MySingletonObject
// Container will take over lifetime management of the object
myContainer.Resolve<MySingletonObject>();
galaktor