MVVM Light SimpleIoC 支持单例吗?

发布于 2024-12-02 17:33:41 字数 238 浏览 1 评论 0原文

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

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

如果我应该以不同的方式来思考这个问题,我也很乐意考虑其他选择。

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.

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.

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

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

发布评论

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

评论(2

披肩女神 2024-12-09 17:33:41

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

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

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

有道理吗?
洛朗

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.

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.

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).

Does it make sense?
Laurent

多情出卖 2024-12-09 17:33:41

我在正常的 silverlight 项目中使用 Sterling,我所做的就是将其添加到 App.xaml..

<Application.ApplicationLifetimeObjects>
        <common:SterlingService />
        <appServices:WebContext>
            <appServices:WebContext.Authentication>
                <!--<appsvc:FormsAuthentication/>-->
                <appsvc:WindowsAuthentication />
            </appServices:WebContext.Authentication>
        </appServices:WebContext>
    </Application.ApplicationLifetimeObjects>

常见引用我从示例中复制的 SterlingService.cs

namespace Common
{
        public sealed class SterlingService : IApplicationService, IApplicationLifetimeAware, IDisposable
        {
            public const long KILOBYTE = 1024;
            public const long MEGABYTE = 1024 * KILOBYTE;
            public const long QUOTA = 100 * MEGABYTE;

            private SterlingEngine _engine;
            private static readonly ISterlingDriver _driver = new IsolatedStorageDriver(); // could use this: new MemoryDriver(); 

            public static SterlingService Current { get; private set; }

}

。 soo..我只是在需要引用服务的地方致电 SterlingService...希望这有帮助。

 [ExportService(ServiceType.Runtime, typeof(IOffLineDataService))]
    public sealed class OfflineDataService : IOffLineDataService
    {
        User user = WebContext.Current.User;

        public OfflineDataService()
        {

        }


        public void PurgeAll(Action<Exception> callback)
        {
            try
            {
                SterlingService.Current.Database.Purge();
                callback(null);
            }
            catch (Exception ex)
            {
                Error.LogError(ex, user);
                callback(new Exception(ErrorMessages.OfflinePurgeAll));
            }
        }
}

I am using Sterling in my normal silverlight project and all i am doing is adding this to App.xaml..

<Application.ApplicationLifetimeObjects>
        <common:SterlingService />
        <appServices:WebContext>
            <appServices:WebContext.Authentication>
                <!--<appsvc:FormsAuthentication/>-->
                <appsvc:WindowsAuthentication />
            </appServices:WebContext.Authentication>
        </appServices:WebContext>
    </Application.ApplicationLifetimeObjects>

common references the SterlingService.cs fine i copied from the examples.. Starts like this

namespace Common
{
        public sealed class SterlingService : IApplicationService, IApplicationLifetimeAware, IDisposable
        {
            public const long KILOBYTE = 1024;
            public const long MEGABYTE = 1024 * KILOBYTE;
            public const long QUOTA = 100 * MEGABYTE;

            private SterlingEngine _engine;
            private static readonly ISterlingDriver _driver = new IsolatedStorageDriver(); // could use this: new MemoryDriver(); 

            public static SterlingService Current { get; private set; }

}

later i just created a wrapper around this service like soo.. and i just call SterlingService where ever i need to reference the service like so... Hope this helps.

 [ExportService(ServiceType.Runtime, typeof(IOffLineDataService))]
    public sealed class OfflineDataService : IOffLineDataService
    {
        User user = WebContext.Current.User;

        public OfflineDataService()
        {

        }


        public void PurgeAll(Action<Exception> callback)
        {
            try
            {
                SterlingService.Current.Database.Purge();
                callback(null);
            }
            catch (Exception ex)
            {
                Error.LogError(ex, user);
                callback(new Exception(ErrorMessages.OfflinePurgeAll));
            }
        }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文