0

I am relatively new to managed packages, we have a 1st gen package that I am actively maintaining and I have noticed in one specific area there is a lot of global properties and methods. Which means if I want to change their signatures I basically have to create a new method V2 and change all references in the code to point to that, leaving all the existing signatures in place. I understand the danger of deprecating, so I haven't been doing that, simply commenting out the internals of those methods and returning null. But there has got to be a better way to design these.

I thought about changing the methods to all accept a Map<string, sObject> signature and then passing in the parameter name as the key and the parameter value as the object. This would, I believe, remove the issues, but am not sure that is actually the best design.

Anyone know a place where I can find best practices around design patterns specifically for dealing with managed package restrictions?

Thanks!

1 Answer 1

1

There is already something you can use, the Callable interface. It accepts two parameters, a method name and a Map<String, Object> parameter map. You can use this to provide a way to call any method you want to support without worrying about versioning your methods. This method can in turn call public, protected, and private methods in the way you design it. You can add a factory method (e.g. MyClass getInstance() { return new MyClass(); }) with a less-than-global constructor to prevent outside packages from instantiating your class in undesirable ways. Of course, this means that you'll have to be potentially concerned about notifying clients of changes, as the compiler will no longer be able to warn them about data type mismatches, spelling errors, etc.

4
  • Thank you I will check it out! Commented Oct 10, 2023 at 17:39
  • I have a hard time accepting this "poor man's" alternative to supporting reflection (Callable). There is a lack of typing and type safety, there is no discovery. It is very unsatisfactory and I have refused to allow its use in our managed package APIs.
    – Phil W
    Commented Oct 10, 2023 at 19:16
  • Instead we define global abstract/virtual classes and a small number of global interfaces (the latter are more troublesome than classes because they become entirely frozen once published). To avoid construction issues we either support a Builder pattern or take a fluent interface approach.
    – Phil W
    Commented Oct 10, 2023 at 19:19
  • 2
    @PhilW I agree it's not great. Reflection would be awesome. That said, if you're already in a corner, this is an out without reinventing the wheel.
    – sfdcfox
    Commented Oct 10, 2023 at 19:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .