Wednesday, 12 October 2016

Replacing Cache with custom class

Hi All,

Today i want to share with you a good trick how you can override original PXCache object.
Why you may need it?

  • To replace base cache logic, that should not be executed in some cases.
  • Forbid cache to read data for database in case of virtual DAC
  • Inject required logic to the standard workflow
  • Do not clear cache data on graph clearing
  • Access protected method
And many other things.

As you may know, that when you (or system) create a new instance of a graph, it will automatically instantiate all caches for each data views that are declared. Unfortunately, because this is unattended process it is almost impossible to adjust it for our own purposes.

But luckily Acumatica is very flexible system and there are several easy ways to do it lately after base initialization. So the reccomended solution will be:

  1. Create an graph extension for required Graph. Or also you may use constructor of the graph, if you develop new functionality.
  2. Override Initialize method to add your logic
  3. Create a new class for cache that should be inherited form PXCache<DAC> type.
  4. Instantiate a new cache object a new cache of the same DAC type.
  5. Replace original cache in the PXGraph.Caches collection with a new one.
On now it is done. You may use your new cache type and put logic there.

Code example:
public class InventoryItemFilterCache : PXCache<InventoryItemFilter>
{
       public InventoryItemFilterCache(PXGraph graph)
              : base(graph)
       {
       }

       public override void Clear()
       {
       }

       public override int Persist(PXDBOperation operation)
       {
              return 0;
       }
}

public class NonStockItemMaintExt : PXGraphExtension<NonStockItemMaint>
{
       public override void Initialize()
       {
Base.Caches[typeof(InventoryItemFilter)] = new InventoryItemFilterCache(Base);
       }

}

Have a nice Development!

No comments: