Skip to content

Object Model Cache Producer

Simon Mourier edited this page Feb 19, 2020 · 1 revision

The Object Model Cache producer adds caching capabilities to the Business Object Model (BOM). The Object Model Cache producer is in fact a sub-producer of the Business Object Model producer.

To add a sub-producer, you can use the “Add New SubProducer” context menu of the producer node in the Producers folder node:

Object Model Cache Producer - Picture 279

Object Model Cache Producer - Picture 280

Once the sub-producer has been added, you must enable it globally or per entity, as demonstrated here, in the Visual Studio property grid, “Aspects and Producers Properties” tab:

Object Model Cache Producer - Picture 281

For a Customer entity, the Object Model Cache producer replaces the LoadById method by the following three methods:

  • a public LoadById method that uses the cache

  • a public NoCacheLoadById method that doesn't use the cache

  • a private CacheLoadById method that does the job.

// Public Method : LoadById with Cache use
public static Customer LoadById(int id)
{
    return CacheLoadById(true, id);
}
 
// Public Method : LoadById without Cache use
public static Customer NoCacheLoadById(int id)
{
    return CacheLoadById(false, id);
}
 
// Private internal Method : LoadById with or without Cache use
private static Commerce.Customer CacheLoadById(bool useCache, int id)
{
    if ((id == -1))
    {
        return null;
    }
    Commerce.Customer customer = null;
    string cacheKey = null;
    if ((useCache == true))
    {
        cacheKey = CodeModeler.Runtime.Caching.SimpleCacheManager.BuildCacheKey("e1e5e8e28e7b22549de97b66274a8d5a", id);
        customer = Commerce.Caching.CacheManager0.Manager.Get("Commerce.Customer", cacheKey) as Commerce.Customer;
        if ((customer != null))
        {
            return customer;
        }
    }
    customer = new Commerce.Customer();
    CodeModeler.Runtime.CodeModelerPersistence persistence = CodeModelerContext.Get(Commerce.Constants.CommerceStoreName).Persistence;
    persistence.CreateStoredProcedureCommand("Commerce", "Customer", "LoadById");
    persistence.AddParameter("@Id", id, ((int)(-1)));
    System.Data.IDataReader reader = null;
    try
    {
        reader = persistence.ExecuteReader();
        if ((reader.Read() == true))
        {
            customer.ReadRecord(reader, CodeModeler.Runtime.ReloadOptions.Default);
            customer.EntityState = CodeModeler.Runtime.EntityState.Unchanged;
            if ((useCache == true))
            {
                Commerce.Caching.CacheManager0.Manager.Add("Commerce.Customer", cacheKey, customer, null);
            }
            return customer;
        }
    }
    finally
    {
        if ((reader != null))
        {
            reader.Dispose();
        }
        persistence.CompleteCommand();
    }
    return null;
}
Clone this wiki locally