develop an approach to Acumatica customization development that maximizes the unit-testability of custom code, and that still allows the same degree of extensibility that Acumatica provides by default using PXGraphExtensions and PXCacheExtensions
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Below is a recipe you can follow (and adapt) so that all of your “real” business logic lives in plain C# classes (which are easy to unit-test), yet you still get to wire everything together in Acumatica via PXGraphExtensions/PXCacheExtensions exactly as you do today.
1. Layer your code
Presentation Layer
• PXGraph & PXGraphExtensions → thin orchestration only
• PXCacheExtensions → only metadata / extra DAC fields
Application (Service) Layer
• IWhateverService + WhateverService
• Contains business operations, validations, orchestration of repositories
public interface IOrderRepository
{
Order GetByOrderNbr(string orderNbr);
void Update(Order order);
void Save();
}
b) Service Interface
csharp
public interface IOrderService
{
void ApproveOrder(string orderNbr, string approverID);
// … other business operations
}
3. Implement with Dependency Injection
a) Infrastructure: BQL-backed repository
csharp
public class OrderRepository : IOrderRepository
{
private readonly PXGraph _graph;
public OrderRepository(PXGraph graph) => _graph = graph;
public Order GetByOrderNbr(string orderNbr)
{
var row = PXSelect<SOOrder,
Where<SOOrder.orderNbr, Equal<Required<SOOrder.orderNbr>>>>
.Select(_graph, orderNbr)
.RowCast<SOOrder>()
.FirstOrDefault();
if (row == null) throw new KeyNotFoundException();
return MapToDomain(row);
}
public void Update(Order order)
{
var dac = MapToDAC(order);
_graph.Caches<SOOrder>().Update(dac);
}
public void Save() => _graph.Actions.PressSave();
// mapping methods (private)
}
b) Application Service
csharp
public class OrderService : IOrderService
{
private readonly IOrderRepository _repo;
public OrderService(IOrderRepository repo) => _repo = repo;
public void ApproveOrder(string orderNbr, string approverID)
{
var order = _repo.GetByOrderNbr(orderNbr);
if(order.Status != OrderStatus.Pending)
throw new InvalidOperationException("Cannot approve.");
order.Approve(approverID); // pure-domain call
_repo.Update(order);
_repo.Save();
}
}
4. Wire up in PXGraphExtension
csharp
public class SOOrderEntry_Extension : PXGraphExtension<SOOrderEntry>
{
private IOrderService _orderService;
public override void Initialize()
{
// 1) create the repository against the current graph
var repo = new OrderRepository(Base);
// 2) inject into service
_orderService = new OrderService(repo);
}
public PXAction<SOOrder> approveOrder;
[PXButton]
[PXUIField(DisplayName = "Approve")]
protected void ApproveOrder()
{
_orderService.ApproveOrder(Base.Document.Current.OrderNbr,
PXAccess.GetUserID());
}
}
Because OrderService and OrderRepository implement plain interfaces, you can now unit-test OrderService by mocking IOrderRepository (e.g. with Moq) without ever touching PXGraph or the database.
5. Unit-testing example with Moq
csharp
[TestClass]
public class OrderServiceTests
{
[TestMethod]
public void ApproveOrder_PendingOrder_SetsStatusToApproved()
{
// arrange
var mockRepo = new Mock<IOrderRepository>();
var pending = new Order
{
OrderNbr = "SO0001",
Status = OrderStatus.Pending
};
mockRepo.Setup(r => r.GetByOrderNbr("SO0001"))
.Returns(pending);
var svc = new OrderService(mockRepo.Object);
// act
svc.ApproveOrder("SO0001", "user42");
// assert
Assert.AreEqual(OrderStatus.Approved, pending.Status);
mockRepo.Verify(r => r.Update(pending), Times.Once);
mockRepo.Verify(r => r.Save(), Times.Once);
}
}
6. Extending with PXCacheExtension
If you need extra DAC fields, keep them in your normal PXCacheExtension<SOOrder>. Just map them in your repository’s MapToDomain / MapToDAC.
7. Recap of benefits
• High testability
All business rules live in OrderService & domain classes. No direct BQL/graph magic there.
• Same extensibility
You still use PXGraphExtensions+PXCacheExtensions to hook into the UI & data events.
• Clean separation
Keeps your codebase maintainable and each layer focused on a single responsibility.
Feel free to tweak naming, use your favorite DI container, or introduce more layers (e.g. a QueryService, command/query split, event-sourcing, etc.). But the core idea remains:
• “Skinny” GraphExtension → orchestration only
• “Fat” pure-C# services & repos → testable business logic.