MVVM Light SimpleIoC 是否支持单例? [英] Does the MVVM Light SimpleIoC support singletons?

查看:23
本文介绍了MVVM Light SimpleIoC 是否支持单例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在当前的 Windows Phone 项目中使用 SterlingDB,并且我希望能够使用 MVVM Light v4 中的新 SimpleIoC 容器从我的应用程序的各个位置解析 Sterling 数据库.

I'm using SterlingDB in my current Windows Phone project, and I would like to be able to resolve the Sterling database from various places in my application using the new SimpleIoC container in MVVM Light v4.

但是,我不确定 SimpleIoC 是否支持注册单例.SterlingDB 引擎应该只在应用首次启动时创建,我不想在容器每次注入引用时都启动新实例.

However, I'm not sure if SimpleIoC supports registering singletons. The SterlingDB engine should only be created when the app first launches, and I don't want to be spinning up new instances every time the container injects a reference to it.

如果我应该以不同的方式思考这个问题,我也很乐意接受替代方案.

If there's a different way I should be thinking about this problem, I'd be glad to entertain alternatives as well.

推荐答案

SimpleIoc 根据您传递给它的键返回实例.如果您在没有键的情况下调用 GetInstance(),您将始终获得对象的默认实例.该实例仅在您第一次调用 GetInstance 时创建(延迟创建).如果您使用键调用 GetInstance,我会查找该命名实例是否已存在于注册表中.如果还没有,我创建它然后返回它.如果已经有一个带有该键的实例,我就返回它.

SimpleIoc returns instances based on a key that you pass to it. If you call GetInstance() without a key, you will always get the default instance of your object. The instance is only created when you call GetInstance the first time (lazy creation). If you call GetInstance with a key, I look up if this named instance already exists in the registry. If it doesn't yet, I create it and then I return it. If there is an instance with that key already, I just return it.

在 alpha 版本(BL16 MIX 版本)中,有一个错误导致 Register 在使用密钥时过早创建实例.此错误已在我将在本周发布的 V4 beta1 中修复.

In the alpha version (BL16 MIX edition), there is a bug that caused Register to create the instance too early, when a key was used. This bug is fixes in V4 beta1 which I will publish this week.

因此,如您所见,如果您始终使用相同的密钥,您将从 SimpleIoc 获得相同的实例(或者如果您根本不使用密钥,则只是默认实例).

So as you see you will get the same instance from SimpleIoc if you always use the same key (or simply the default instance if you don't use a key at all).

有意义吗?洛朗

这篇关于MVVM Light SimpleIoC 是否支持单例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆