Skip to content

Latest commit

 

History

History
63 lines (47 loc) · 4.02 KB

File metadata and controls

63 lines (47 loc) · 4.02 KB

PX1001

This document describes the PX1001 diagnostic.

Summary

Code Short Description Type Code Fix
PX1001 An instance of the concrete graph type must be created with the PXGraph.CreateInstance() factory method. Error Available

Diagnostic Description

The PX1001 diagnostic reports graphs of concrete types derived from PXGraph<T> that are instantiated directly with a graph constructor (for example, new SOOrderEntry(), new MyGraph()). The PXGraph.CreateInstance<T>() factory method should be used instead to instantiate correctly initialized graph instances from code.

This diagnostic has a complementary rule, PX1003, which reports direct constructor calls for the base PXGraph type (for example, new PXGraph()). Both diagnostics share similar reasoning, but they have different severity levels.

Difference between Graph Constructor Calls and PXGraph.CreateInstance Factory Method

During the execution of the PXGraph.CreateInstance factory method, the Acumatica Framework performs a complex initialization of the graph instance, including the following steps:

  • Collection and initialization of graph extensions
  • Dependency injection of services into a graph
  • Loading and unloading of the graph state from the session

These steps are omitted when the graph instance is created via a direct constructor call.

The sections below describe some of the steps that are performed during the graph initialization.

Graph Extensions

The Acumatica Framework relies on a complex extension mechanism to provide customization capabilities. Proper initialization of graph instances is essential for this mechanism to function correctly. Such initialization is implemented in the PXGraph.CreateInstance() factory method via the dynamic generation of a proxy class that derives from the original graph type and integrates all graph extensions into the original graph's logic. Among extension mechanisms that rely on this dynamic code generation are:

  • The PXOverride attribute mechanism
  • The PXProtectedAccess attribute mechanism
  • The overrides of event handlers

Graph constructors will only create instances of the original graph type without any graph extensions loaded for it. This can lead to incorrect and unexpected behavior at runtime.

Dependency Injection

The Acumatica Framework provides its own Dependency Injection (DI) mechanism to inject different services into the graph instance via the property injection pattern. The PXGraph.CreateInstance() factory method ensures that all DI services are correctly injected into the graph instance, while direct constructor calls bypass this mechanism. This means that graphs created via constructors may lack necessary DI services, leading to runtime errors.

Why "Error" Severity for Concrete Graph Types

The PX1001 diagnostic has the Error severity because for custom graphs (graphs derived from PXGraph<T>) proper initialization is critical. Custom graphs are usually used to execute business logic that relies on correctly initialized graph extensions and DI services.

Code Fix Behavior

The code fix replaces the direct call to the graph constructor with the call to the PXGraph.CreateInstance<T>() factory method.

Example of Incorrect Code

// Error: Constructor call for a concrete graph type
var orderGraph = new SOOrderEntry();

Example of Code Fix

// Correct: Use CreateInstance for concrete graph types
var orderGraph = PXGraph.CreateInstance<SOOrderEntry>();

Related Articles