2
votes

I noticed that graph.Caches[typeof(MyDac)] is quite useful in Acumatica to manipulate values in other DACs. What would be the proper way to use it, for example when I update a value in another DAC? Do I have to persist right after it, or will it be saved to DB when I run graph.Action.PressSave(), even if I am not using this DAC in any of the view in the graph?

1

1 Answers

3
votes

The PXGraph.Caches collections contains DAC objects loaded in memory on the server. In a graph it is accessed directly as this.Caches and in an extension it is accessed through Base.Caches. While not technically acurate it can be thought of as a global DAC cache. The DAC objects contained in DataView.Cache is a subset of the DAC objects in PXGraph.Caches.

The usual pattern for persisting records is by using graph DataViews. The PXGraph.Action.PressSave() will persist the records when the current record of the primary DataView of the graph is properly set.

The primary DataView of the graph is declared in the ASPX PrimaryView property of the PXDataSource element:

<px:PXDataSource ID="ds" runat="server" PrimaryView="Document" TypeName="PX.Objects.SO.SOOrderEntry" >

Setting PrimaryView.Current record in the BLC graph is required for the Save action to persist the DataViews records.

It is also possible to persist DAC objects in DataViews without invoking the Save action. To do so you would perform a CRUD operation and then Persist the DAC objects in the DataView using:

DataView.Insert(DAC)
DataView.Update(DAC)
DataView.Delete(DAC)    
DataView.Cache.Persist(PXDBOperation.Insert)
DataView.Cache.Persist(PXDBOperation.Update)    
DataView.Cache.Persist(PXDBOperation.Delete)

While it is possible to do the same operations on PXGraph.Caches there are few situation where this is required in user code because using DataView is the preferred method of persisting DAC objects and access to DataViews is available when you have the BLC.

Where PXGraph.Caches is especially useful is when working with PXGraph instead of a BLC, often found in the context of generic code. You can access DataViews of the BLC like SOOrderEntry.Document with a reference pointing to the SOOrderEntry BLC. However if all you got is a reference to the PXGraph base class of the BLC you don't have access to the DataViews. In that case you can still access the DAC objects in PXGraph.Caches. By knowing the type of the DataView DAC objects for example SOOrderEntry.Document is of SOOrder type and will be in PXGraph.Caches[typeof(SOOrder)]. For user code this pattern is more common in custom attributes that have access to the PXGraph object but not the BLC. In BLC it's more common to work on the DataView.Cache instead of PXGraph.Cache.

Another possible scenario where accessing PXGraph.Caches could be useful is when you have many DataViews of the same type. If you want to query all updated DAC objects of a same type that are in multiple DataViews you can itereate on Caches[typeof(DAC)].Updated. It's also quite common to simply fetch a reference to a Cache object using Caches[typeof(DAC)] that will then be passed to another Acumatica framework method by input parameter. In this case it's often a matter of convenience because the same Cache reference could be obtain through DataView.Cache.