web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

pack/unpack/create approach for super and subclass hierarchy

(0) ShareShare
ReportReport
Posted on by

I got an abstract super class called MyBase. I'm going to store away a collection of subclasses (MySubClass1, MySubClass2, etc.) in to a List(Types::Class) variable, which will be serialized as part of an outer data structure. So I would need to implement a static MyBase::create(container) method which will be internally called by List::unpack() and, let alone unpacking, must instantiate an appropriate type of a subclass based on super classType member. I hope you get the idea.

Suppose I have an abstract super class:

abstract class MyBase implements SysPackable
{
MyEnum classType;
#define.baseVersion(1)
#localmacro.baseList
  baseVer, classType
#endmacro
}

I also have subclasses, every which one looking akin:

class MySubClass1 extends MyBase
{
int subValue1;
str subValue2;
#define.subVersion(1)
#localMacro.subList
  #baseList, subVer, subValue1, subValue2
#endmacro
}

How would you go about implementing pack/unpack/create serialization pattern for such hierarchy? Maybe there is a better way to accomplish what I need?

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Rachit Profile Picture
    4,015 User Group Leader on at
    RE: pack/unpack/create approach for super and subclass hierarchy

    If you want to have an additional list to be packed/unpacked then you can achieve it easily. You can find some standard AX examples where this is done. A similar example can be found in IntrastatTransfer class. Check out the pack/unpack implementation in this class, an additional query object is packed there. You can similarly pack/unpack your list.

    With reference to instantiating a new class, I am not sure what you are trying to achieve here any why. Normally use construct method to instantiate child classes.

     

    Hope this helps.

  • Verified answer
    Martin Dráb Profile Picture
    236,394 Most Valuable Professional on at
    RE: pack/unpack/create approach for super and subclass hierarchy

    Keeping the type of object inside object state doesn't look useful - you would get the information only after creating and unpacking it, while you need it before creating the object (to know which type it should be).

    Therefore I would keep the type outside the state and my deserialization logic would first read the type, create an instance of the right class and then pass the version and packed state into it.

    If you don't need classType for something else, you can remove it completely. Objects already have type information anyway.

  • psined Profile Picture
    on at
    RE: pack/unpack/create approach for super and subclass hierarchy

    Thanks Martin, you're absolutely right (as always ;)

    To see what's going on I've created a job to debug thru serialization of a List collection into a container. As it turns out that for every element in the collection among other things a name of the class string is also packed, which I assume helps the List class factory construct the appropriate object and call its' create() method directly. All clear now. Learning AX little by little ;)

    Thanks again!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Community Member Profile Picture

Community Member 4

#1
Martin Tocauer Profile Picture

Martin Tocauer 4

#3
Nayyar Siddiqi Profile Picture

Nayyar Siddiqi 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans