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.
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
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));
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With