пятница, 4 июля 2014 г.

How to transfer objects between server and client using List, Set or Map in Dynamics AX


Hi.
Some time we use such structure as a list of classes. And we can get into situation, when we need to transfer our list using a container (for example between client and server or we need to print Excel report for client ...).
In this post we will try to do this operation using very simple approach.

Starting conditions. We have client site class, and hard processing. We want to transfer hard processing to the server. Let's try do something interesting ...
CMSalesOrderDataContract with some variables

class CMSalesOrderDataContract
{
    SalesId   salesId;
    Qty       maxQty;
    Qty       averageQty;
}

we have parm methods to access these fields

public SalesId parmSalesId(SalesId _salesId = salesId)
{
    salesId = _salesId;
    return salesId;
}

public Qty parmMaxQty(Qty _maxQty = maxQty)
{
    maxQty = _maxQty;
    return maxQty;
}

public Qty parmAverageQty(Qty _averageQty = averageQty)
{
    averageQty = _averageQty;
    return averageQty;
}

and simple init method
public void initFromSalesTable(SalesTable _salesTable)
{
    SalesLine   salesLine;

    this.parmSalesId(_salesTable.SalesId);
    select maxOf(SalesQty) from salesLine
        where salesLine.SalesId == _salesTable.SalesId
        ;
    this.parmMaxQty(salesLine.SalesQty);
    select avg(SalesQty) from salesLine
        where salesLine.SalesId == _salesTable.SalesId
        ;
    this.parmAverageQty(salesLine.SalesQty);
}

and construct method
public static CMSalesOrderDataContract construct()
{
    return new CMSalesOrderDataContract();
}

Than we have 2 classes:
one collects information for us, another prints information in form;

Server class collects and saves information for us.
Declare variables
class CMCollectInformation
{
    List salesOrdersInfo;
}
init list
public void new()
{
    salesOrdersInfo = new List(Types::Class);
}
construct class on Server side.
public server static CMCollectInformation construct()

{
    return new CMCollectInformation();
}

collect information
public void collect()
{
    SalesTable                  salesTable;
    CMSalesOrderDataContract    salesOrderDataContract;
    while select salesTable
    {
        salesOrderDataContract = CMSalesOrderDataContract::construct();
        salesOrderDataContract.initFromSalesTable(salesTable);
        salesOrdersInfo.addEnd(salesOrderDataContract);
    }
}
return information packed in a container
public container getPackedData()
{
    return salesOrdersInfo.pack();
}

And second class allows us to print information on client side
class CMPrintInformation
{

}
initialize class strong on CLIENT side
public client static CMPrintInformation construct()
{
    return new CMPrintInformation();
}

public void printInfo()
{
    CMCollectInformation        collectInformation;
    ListEnumerator              le;
    CMSalesOrderDataContract    salesOrderDataContract;
    List                        soClientInfoList;

    // create and collect information n server side
    collectInformation  = CMCollectInformation::construct();
    collectInformation.collect();
    soClientInfoList = List::create(collectInformation.getPackedData());
    le = soClientInfoList.getEnumerator();

    while (le.moveNext())
    {
        salesOrderDataContract = le.current();
        info(strFmt('SalesId %1, maxQty %2, averageQty %3', salesOrderDataContract.parmSalesId(), salesOrderDataContract.parmMaxQty(), salesOrderDataContract.parmAverageQty()));
    }
}

Finally let's create a job to test our work
static void Job3(Args _args)
{
    CMPrintInformation  printInformation = CMPrintInformation::construct();
    printInformation.printInfo();
}

An error is thrown, because our DataContract class does not contain pack and unpack methods

Let's implment a few changes in our dataContract class 
Class declaration should look like

class CMSalesOrderDataContract implements SysPackable
{
    SalesId   salesId;
    Qty       maxQty;
    Qty       averageQty;
    
    #define.CurrentVersion(1)
    #localmacro.CurrentList
        salesId,
        maxQty,
        averageQty
    #endmacro
    
}

and add pack method
public container pack()
{
    return [#CurrentVersion,#CurrentList];
}

and add unpack method
public boolean unpack(container packedClass)
{
    int version     = runbase::getVersion(packedClass);
    switch (version)
    {
        case #CurrentVersion:
            [version,#CurrentList] = packedClass;
            return true;

        default :
            return false;
    }
    return false;
}

Run the job again and get a new error

We need to implement create method 

public static CMSalesOrderDataContract create(container _packedClass)
{

    CMSalesOrderDataContract    salesOrderDataContract = CMSalesOrderDataContract::construct();

    salesOrderDataContract.unpack(_packedClass);
    return salesOrderDataContract;
}

Try to run our job once again

Excellent result!

Summary:
When we want to transfer List, Set or Map containing objects of a class between server and client we have to implement pack, unpack methods and create method. Pack and unpack methods are standard. Create method must be static and should get container as an input parameter. It should create an instance of the class, unpack variables from passed container and return the instance.



Комментариев нет:

Отправить комментарий