This document describes the PX1001 diagnostic.
| 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 |
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.
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.
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
PXOverrideattribute mechanism - The
PXProtectedAccessattribute 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.
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.
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.
The code fix replaces the direct call to the graph constructor with the call to the PXGraph.CreateInstance<T>() factory method.
// Error: Constructor call for a concrete graph type
var orderGraph = new SOOrderEntry();// Correct: Use CreateInstance for concrete graph types
var orderGraph = PXGraph.CreateInstance<SOOrderEntry>();