为什么我注入的依赖对象在每个模块中不是同一个实例?

发布于 2024-07-27 15:01:32 字数 1047 浏览 2 评论 0原文

我正在使用 Prism 和 Unity。

我有这个 bootstrapper

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

我的 CustomerModule 注入了 MenuManager 并向其添加菜单项:

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

但是当我的 MainMenuPresenter 对象也获取 MenuManager 时注入,它不是同一个对象:

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

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

我如何告诉 Prism/Unity 我希望注入的 MenuManager 成为 Singleton,以便将相同的对象注入到我的每个模块和对象中?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

渔村楼浪 2024-08-03 15:01:32

使用 Unity,您可以这样做(取自 MSDN on Lifetime Managers in Unity< /a>):

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

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>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文