Recommended best practice of returning multiple values from service in one go

Hi
I have a sales order UI from where I want to send Product as a parameter to service where i calculate -

  1. product price
  2. tax 1 amount
  3. tax 2 amount
  4. discount amount

    public BigDecimal getContractDefaultSalesPrice(CustomerProfile customerProfile, Material material){
        BigDecimal amount;// = BigDecimal.ZERO;

        //check SalesContractn if any
        try (Transaction tx = persistence.createTransaction()) {
            Query query = persistence.getEntityManager().createQuery(
                    "select e.price from erp$SalesPriceLine e " +
                            "where e.material.id = :material and e.salesPrice IN (select p from erp$SalesPrice p " +
                            "where p.id = :price) ");
            query.setParameter("price", customerProfile.getSalesPrice());
            query.setParameter("material", material);
            amount = (BigDecimal) query.getFirstResult();
            tx.commit();

        }
        if (amount == null)
            amount = BigDecimal.ZERO;

        return amount;
    }

what will be the most efficient option to get all those variable values after they are computed in service. Do I need to get them one by one or in one go through an array of values? Thanks for any suggestions…

Hi,

I recommend that you define simple Java Class with all required properties in your Service interface that will implement java.io.Serializableand return instance of that class from your business method. That will be more convenient than arrays.

Example:


public interface SalesService {
    TransactionResult performCreditCardTransaction(String ccNumber);

    class TransactionResult implements Serializable {
        private String code;
        private User owner;

        public TransactionResult(String code, User owner) {
            this.code = code;
            this.owner = owner;
        }

        public String getCode() {
            return code;
        }

        public User getOwner() {
            return owner;
        }
    }
}

Hi Yuriy
Thanks. How do I call these values, after calling TransactionResult, getCode, getOwner? Wouldn’t be a service?

Mortoza, I had the same top level question as you and I added the parameters as class values like this, which works in my case and let me keep my familiar variable names.


import java.io.Serializable;

public class TransactionResult implements Serializable {
    public BigDecimal product_price;
    public BigDecimal tax_1_amount;
    public BigDecimal tax_2_amount;
    public BigDecimal discount_amount;
}

you could alternatively define your properties as private with getters and setters. When you create the variable in your calling location, you need to include the service class then the Transaction Result, like:


SalesService.TransactionResult transactionResult = getContractDefaultSalesPrice(CustomerProfile customerProfile, Material material);
tax_1_amount = transactionResult.tax_1_amount;
1 Like