Skip to content

Latest commit

 

History

History
75 lines (61 loc) · 3.62 KB

PX1072.md

File metadata and controls

75 lines (61 loc) · 3.62 KB

PX1072

This document describes the PX1072 diagnostic.

Summary

Code Short Description Type Code Fix
PX1072 BQL queries must be executed within the context of an existing PXGraph instance. Warning (ISV Level 1: Significant) Available

Diagnostic Description

A new PXGraph instance should not be used in the execution of BQL queries. Instead, an existing PXGraph instance should be used. Because the creation of a PXGraph instance is an expensive operation, the instantiation of unnecessary graph instances slows the performance of the application. Also, when you create a new graph, you miss the query results that have already been cached in an existing graph.

The code fix replaces new PXGraph() or PXGraph.CreateInstance<T>() in the BQL query with the existing PXGraph instance of your choice.

The diagnostic also displays the warning when a class contains multiple BQL or Fluent BQL queries and the context contains no available graphs. The diagnostic considers a graph available in the context if it exists in the context which means that a graph is one of the following:

  • A reference to this if the diagnostic analyzes the code inside a graph or a graph extension
  • A method parameter of a graph type
  • A local variable of a graph type

NOTE: Fields or properties of a graph type can also be considered as available graphs, but the diagnostic does not analyze these types of available graphs explicitly and excludes them from the list of available graphs.

The following code shows an example for which the diagnostic will display an error because a method contains multiple BQL queries and no available graphs.

public class CustomerService
{
	public Customer SelectCustomer()
	{
		Customer customer1 = PXSelect<Customer>.Select(new PXGraph());								// Report diagnostic
		Customer customer2 = PXSelect<Customer>.Select(PXGraph.CreateInstance<CustomerMaint>());    // Report diagnostic

		return customer1 ?? customer2;
	}
}

In this case, the diagnostic message recommends to create a new local variable for a shared graph.

Example of Code That Results in the Warning

public class ARInvoiceEntry : PXGraph<ARInvoiceEntry, ARInvoice>
{
	public ARInvoice GetInvoice(PXGraph graph, string refNbr)
	{
		var invoiceEntry = PXGraph.CreateInstance<ARInvoiceEntry>();

		var invoice = PXSelect<ARInvoice, Where<ARInvoice.refNbr, Equal<Required<ARInvoice.refNbr>>>>
			.Select(new PXGraph(), refNbr); //The PX1072 warning is displayed for this line.
	}
}

Example of a Code Fix

public class ARInvoiceEntry : PXGraph<ARInvoiceEntry, ARInvoice>
{
	public ARInvoice GetInvoice(PXGraph graph, string refNbr)
	{
		var invoiceEntry = PXGraph.CreateInstance<ARInvoiceEntry>();

        //First variant of the fix
		var invoice = PXSelect<ARInvoice, Where<ARInvoice.refNbr, Equal<Required<ARInvoice.refNbr>>>>
			.Select(this, refNbr); //`new PXGraph()` is replaced with the `this` instance.
        /*
        // Second variant of the fix
        var invoice = PXSelect<ARInvoice, Where<ARInvoice.refNbr, Equal<Required<ARInvoice.refNbr>>>>
			.Select(graph, refNbr); //`new PXGraph()` is replaced with the `graph` instance. 
        // Third variant of the fix
        var invoice = PXSelect<ARInvoice, Where<ARInvoice.refNbr, Equal<Required<ARInvoice.refNbr>>>>
			.Select(invoiceEntry, refNbr); //`new PXGraph()` is replaced with the `invoiceEntry` instance. 
        */
	}
}