Implementing C# 10 in Acumatica Projects: Enhancements and Examples
Working with Acumatica ERP often requires constant improvements to the codebase. Leveraging the features of C# 10 can significantly streamline development and improve code readability. In this article, we’ll explore how to use C# 10 features in Acumatica projects with a focus on extending SOOrderEntryExt.
Configuring the Project to Use C# 10
To enable C# 10 features, configure your project file (*.csproj) by adding the following:
<PropertyGroup>
<LangVersion>10</LangVersion>
</PropertyGroup>
After that, reload the project and you can use C# 10 features in your code.
Example 1: Global using for Simplified Code
Global using directives allow you to declare namespaces once and eliminate repetitive using statements in every file. This is especially beneficial in large Acumatica projects where common namespaces are frequently used.
Create a GlobalUsings.cs file and add the following:
global using PX.Data;
global using PX.Objects.SO;
Now in your SOOrderEntryExt extension you don't need to write these using every time. You can remove these using from SOOrderEntryExt because they are in GlobalUsings.
Example 2: Pattern Matching in an Event Handler
One of the key features of C# 10 is enhanced pattern matching, which allows you to write concise and intuitive code for type and value checks.
Below is an event handler for RowSelected that checks the order status and logs relevant information.
protected void _(Events.RowSelected<SOOrder> e)
{
if (e.Row is not SOOrder order) return;
if (order.Status == SOOrderStatus.Completed)
{
PXTrace.WriteInformation($"Order {order.OrderNbr} completed.");
}
else if (order.Status is SOOrderStatus.Open or SOOrderStatus.Hold)
{
PXTrace.WriteInformation($"Order {order.OrderNbr} Open or Hold.");
}
}
Example 3: Lambda Expressions and Local Functions
C# 10 allows for simplified lambda expressions and local functions, which can help you structure your methods better.
Example of status validation using a local function:
public void ValidateOrder(SOOrder order)
{
bool IsValidStatus(string status) => status is SOOrderStatus.Open or SOOrderStatus.Completed;
if (!IsValidStatus(order.Status))
{
PXTrace.WriteWarning($"Order {order.OrderNbr} has an invalid status: {order.Status}");
}
else
{
PXTrace.WriteInformation($"Order {order.OrderNbr} has a valid status: {order.Status}");
}
}
bool IsValidStatus(string status) => status is SOOrderStatus.Open or SOOrderStatus.Completed;
- Purpose: Declares a local function within the ValidateOrder method.
- Local function: This function is only available inside the ValidateOrder method, keeping the code organized and preventing unnecessary global exposure.
- Logic: The local function checks whether the provided status is either Open or Completed. This is done using the is keyword and the or operator introduced in modern C# versions.
Implementing C# 10 in Acumatica projects opens up opportunities for writing more concise, clean, and maintainable code. However, when working with Acumatica, it is important to consider the platform's limitations, such as the inability to use certain language features (e.g., default interface implementations).
Wishing you great success in your development endeavors!