This document describes the PX1077 diagnostic.
Code | Short Description | Type | Code Fix |
---|---|---|---|
PX1077 | Event handlers in graphs and graph extensions should have the protected and virtual modifiers. |
Error | Available |
The accessibility level of an event handler should have the protected
modifier, and the method should have the virtual
or override
modifier.
Private event handlers can lead to unexpected behavior, and it is not guaranteed that such event handlers will be invoked by the Acumatica Framework.
Making the event handler virtual
is a good practice for API design; it allows the derived classes to override the method and change the implementation.
- If the event handler is already overridden, the diagnostic will not show an error.
- Explicit interface implementations are effectively
private
. The diagnostic will show only a warning for such implementations. - If the container type is sealed, the code fix will propose the
public
modifier and novirtual
modifier. - The diagnostic will show an error for the private modifier and only a warning for other access modifiers.
The following example demonstrates the correct implementation of an event handler.
public class MyGraph : PXGraph<MyGraph>
{
}
public class MyGraphExtension : PXGraphExtension<MyGraph>
{
protected virtual void HandleDocTypeFieldUpdated(Events.FieldUpdated<PX.Objects.AR.ARInvoice.docType> e)
{
}
}
In the following example, the access modifier is public
because the method implements an interface.
public class MyGraph : PXGraph<MyGraph>
{
}
public interface IMyGraphExtension
{
void HandleDocTypeFieldUpdated(Events.FieldUpdated<PX.Objects.AR.ARInvoice.docType> e);
}
public class MyGraphExtension : PXGraphExtension<MyGraph>, IMyGraphExtension
{
public void HandleDocTypeFieldUpdated(Events.FieldUpdated<PX.Objects.AR.ARInvoice.docType> e)
{
}
}
In the following example, the access modifier is public
and not virtual
because the container type is sealed
.
public class MyGraph : PXGraph<MyGraph>
{
}
public sealed class MyGraphExtension : PXGraphExtension<MyGraph>
{
public void HandleDocTypeFieldUpdated(Events.FieldUpdated<PX.Objects.AR.ARInvoice.docType> e)
{
}
}
In the following example, the access modifier should be protected
and virtual
.
public class MyGraph : PXGraph<MyGraph>
{
}
public class MyGraphExtension : PXGraphExtension<MyGraph>
{
private void HandleDocTypeFieldUpdated(Events.FieldUpdated<PX.Objects.AR.ARInvoice.docType> e)
{
}
}