Tuesday 29 March 2016

Collection Classes in Ax 2012

Collection Classes

We cannot store objects in arrays (x++ class) or containers. The Microsoft Dynamics AX collection classes have been designed for storing objects. 

Below are collection classes: Set , Map , List , Array (Collection class)

Set is used for the storage and retrieval of data from a collection in which the members are unique. The values of the members serve as the key according to which the data is automatically ordered. Thus, it differs from a List collection class where the members are placed into a specific position, and not ordered Automatically by their value.

static void Set(Args _args)
{
    Set setOne;
    Set setTwo;
    SetEnumerator enumerator;
    Int value;
    setOne = new Set(types::Integer);
    setOne.add(4);
    setOne.add(6);
    setOne.add(3);
   
    enumerator = setOne.getEnumerator();
    while (enumerator.moveNext())
    {
        value = enumerator.current();
        info(strFmt("%1",value));      
    }
}

Output :- 3
                 4
                 6

List object contains members that are accessed sequentially. Lists are structures that can contain members of any X++ type. All the members in the same list must be of the same type.

static void List(Args _args)
{
    List integerList = new List(Types::Integer);
    ListEnumerator enumerator;
    // Add some elements to the list
    integerList.addEnd(1);
    integerList.addEnd(4);
    integerList.addEnd(3);
    // Set the enumerator
    enumerator = integerList.getEnumerator();
    // Go to beginning of enumerator
    enumerator.reset();
    //Go to the first element in the List
    while(enumerator.moveNext())
    {
        info(strfmt("%1", enumerator.current()));
    }
}

Output :- 1
                 4
                 3

Map object associates one value (the key) with another value. Both the key and value can be of any valid X++ type, including objects. The types of the key and value are specified in the declaration of the map. The way in which maps are implemented means that access to the values is very fast.

static void Map(Args _args)
{
    Map mapTest;
    MapEnumerator enumerator;
  
    mapTest = new Map(Types::String, Types::Integer);
   
    mapTest.insert("One"1);
    mapTest.insert("Two"2);
   
    enumerator = mapTest.getEnumerator();
    while (enumerator.moveNext())
    {
        info(strfmt("Key - %1 , Value  - %2.",enumerator.currentKey(),enumerator.currentValue()));
    }
}

Output:- Key - One , Value  - 1.
                Key - Two , Value  - 2.



Array inserts, sometimes referred to as bulk inserts, are implemented in the kernel. They buffer a group of rows and insert them in a single trip to the SQL backend. This vastly reduces the number of trips, and speeds up inserts. You can use RecordSortedList or RecordInsertList to hold your rows until they are inserted. Both classes have an insertDatabase method that is used to insert the records into the database as efficiently as possible. However, the insertDatabase method does

RecordSortedList
                       
                         Use RecordSortedList when you want a subset of data from a particular table, and you want it sorted in an order that does not currently exist as an index.
  • A RecordSortedList object holds records from a single table. The list has a unique key that is defined by the fields listed by using the sortOrder method. 
  • Records are automatically sorted as they are inserted, they do not have to be inserted in sort sequence. 
  • There is no limit to the size of a RecordSortedList object, but they are completely memory-based, so there are potential memory consumption problems. 
  • RecordSortedList objects must be server-located before the insertDatabase method can be called. Otherwise, an exception is thrown. 
  • Record level security (RLS) cannot be applied by the RecordSortedList class. RLS is applied by the RecordInsertList class).

Compared to temporary tables, RecordSortedList objects:
  • are faster
  • are not disk-based
  • only have one index
  • cannot be used in forms
  • require a call between the client and server per (grouped) read
 Ex:
Student student;
RecordSortedList recordSortedList = new RecordSortedList(tablenum(Student));
recordSortedList .sortOrder(fieldname2id(tablenum(Student),’StudentId’));
student.clear();
student.StudentID=”123″;
student.FirstName=”DOM”;
student.LastName=”FED”;
recordSortedList.ins(student);

student.clear();
student.StudentID=”456″;
student.FirstName=”TOM”;
student.LastName=”GED”;
recordSortedList.ins(student);

 student.clear();
student.StudentID=”789″;
student.FirstName=”ROM”;
student.LastName=”TED”;
recordSortedList.ins(student);

recordSortedList.insertDatabase();

RecordInsertList
               The RecordInsertList class provides array insert capabilities in the kernel. This allows you to insert more than one record into the database at a time, which reduces communication between the application and the database.
               The array insert operation automatically falls back to classic record-by-record inserts when non-SQL based tables are used (for example, temporary tables), or when the insert method on the table is overridden (unless it is explicitly discarded).
Ex:
FiscalCalendar myTable;
 RecordInsertList insertList = new RecordInsertList(myTable.TableId, True);
  int i;

 for ( i = 1; i <=  100; i++ )
 {
        myTable.CalendarId = "F"+int2str(i);
        insertList.add(myTable);
 }
 insertList.insertDatabase();






No comments:

Post a Comment