Welcome Guest! To enable all features please Login or Register.



How to load static data into a lookup cache
#1 Posted : Friday, October 19, 2012 6:51:01 PM(UTC)

Xomega Framework provides a flexible infrastructure for loading static data into the lookup cache on demand. It allows you to register multiple lookup cache loaders, each of which can load one or more lookup tables into the cache.

Xomega Framework provides a default cache loader that allows loading static data from an XML file. Additionally, Xomega model allows generating and registering lookup cache loaders based on a service operation, which can read the static data from a database or any other sources. You can also implement your own custom lookup cache loader using the following steps.
  1. Create a class that extends Xomega.Framework.Lookup.LookupCacheLoader base class.
  2. Add a constructor, where you would call the base constructor configured with the proper parameters, such as types of lookup cache it supports, whether the data it loads is case sensitive and which lookup tables it loads.
  3. Override the LoadCache function to construct new LookupTable object(s) populated with Header objects, for which you need to set the ID, text and any additional attributes as needed. Use the provided updateCache callback function to store each lookup table in the cache.

The following example shows how to implement a global case-insensitive cache loader that reads multiple look up tables from the system dictionary and loads them all at once when requested.
public partial class DictionaryReadCacheLoader : LookupCacheLoader 
    public DictionaryReadCacheLoader() : base(LookupCache.Global, false) { }

    protected override void LoadCache(string tableType, CacheUpdater updateCache)
        Dictionary<string, Dictionary<string, Header>> data = new Dictionary<string, Dictionary<string, Header>>();
        foreach (Dictionary_ReadOutput row in ReadData())
            string type = row.Enumeration;
            Dictionary<string, Header> tbl;
            if (!data.TryGetValue(type, out tbl)) data[type] = tbl = new Dictionary<string, Header>();

            string id = "" + row.ItemCode;
            Header h;
            if (!tbl.TryGetValue(id, out h)) tbl[id] = h = new Header(type, id, row.Text);
            foreach (Dictionary_ReadOutput_Properties p in row.Properties)
                h.AddToAttribute(p.Property, p.Value);
        foreach (string type in data.Keys)
            updateCache(new LookupTable(type, data[type].Values, true));

Next up, you need to add code that registers your lookup cache loaders using the LookupCache.AddCacheLoader method and make sure that it gets called only once per application run. The simplest way to ensure that would be to do it in a static initializer of a certain class, for which you can also provide an empty method that can be called to force the class to be loaded as follows.
public class LookupCacheLoaders
    // Static initializer to register lookup cache loaders.
    // This will ensure they will be registered only once.
    static LookupCacheLoaders()
        LookupCache.AddCacheLoader(new DictionaryReadCacheLoader());

    public static void EnsureRegistered() {}

Note, that if you generated your cache loaders from a Xomega model, it will automatically generate such a class, where it will register all the loaders that don't have their skip-registration attribute set to true. For all other cache loaders, you will need to provide a registration class as described above.

Finally, you need to make sure that these registration classes will be initialized in your application. Generally you can just add it to your application startup method as follows.
private void Application_Startup(object sender, StartupEventArgs e)
    // register generated loaders via static initializer
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.