Wednesday, August 10, 2016

Public members vs. Parm methods on classes extending SysAttribute in AX 7

I thought I did the right thing when I changed attribute classes that I moved into AX 7 from AX 2012, so they had public members instead of Parm methods.

Well, don't do that. As it turns out the attribute framework is looking for the Parm methods when trying to apply the member values. If you don't have them, the values are not being set.

Hopefully this information can save you an hour or so of tedious debugging.


Martin DrĂ¡b said...

Public class fields are dangerous and generally shouldn't be used. They break encapsulation - you shouldn't expose implementation details about storage; the public interface should only expose operations (methods). This will allow you to change the implementation as needed (e.g. you may want to add validations or trigger events, add change the storage to something else, refactor the class and so on).
It's the same in C# - you can technically expose fields publicly, but you shouldn't. You should use properties instead (a property technically is one or two accessor methods). The equivalent of properties in X++ are parm* methods.

That X++ now expose this capability of CIL to declare public fields doesn't mean that you should start using it instead of methods.

Palle Agermark said...

I tend to agree a long way with you.

But these are attributes, and they must public for framework to do what it does and we'are talking very simple properties. So in this case I don't it is unreasonable to use public members rather than methods.