silverlight using singleton pattern

silverlight using singleton pattern

Old forum URL: forums.lhotka.net/forums/t/9653.aspx


lacota posted on Tuesday, October 19, 2010

I need to use a singleton namevalue list in a view model. I am using the sample ProductList as a template. I am not clear how I would implement this. The callback is async but I need to use the results inline with the method calling it. Can anyone point me in the right direction here? Thanks

lacota replied on Tuesday, October 19, 2010

I tried doing the following;

MySingleton mySingleton = null;

MySingleton.Get((o, e1) = >) {

if (e1.Error == null && e1.Object != null) {

mySingleton = e1.Object;

}

}

I got a serialization error when the singleton was a namevaluelist but it worked fine when it was a readonly list. I don't know if this is a bug in the silverlight implementation of the csla.namevaluelist or an error in my implementation.

RockfordLhotka replied on Tuesday, October 19, 2010

I usually maintain the cached value within the business class, so the singleton access occurs through the factory method. So in a read-only class of some sort you'd do this (typed off the top of my head, so probably not exactly right):

private static MyList _cache;

public static void GetList(DataPortalResult<MyList> callback)
{
  if (_cache != null)
    callback(this, new DataPortalResult<MyList>(_cache, null));
  else
    DataPortal.BeginFetch<MyList>((o, e) =>
    {
      _cache = e.Object;
      callback(o, e);
    });
}

public static void ClearCache()
{
  _cache = null;
}

Basically the same pattern as you use with a sync factory method, but with an async factory method.

lacota replied on Wednesday, October 20, 2010

That's what I have. And it works when the singleton is a readonlylist but I get a serialization error when the singleton is a namevaluelist. It can't seem to be able to find the type of the value.

Copyright (c) Marimer LLC