Dynamics 365 Business Central: AL and the internalsVisibleTo property

Yesterday I was talking with a partner that developed an extension with a quite complex logic declared in a codeunit with visibility = Internal (so the object can be accessed only by code in the same module, but not from another module). Now they have created a new extension (dependent from the previous) and they would like to reuse the business logic declared on the main extension in this new app. They have changed the access level of the codeunit in the main app but doing this they had immediately a blocking problem: they don’t want to expose all their methods to the external world!

How can achieve this result? The solution is: use the (quite hidden) internalsVisibleTo property.

This property permits you to specify a list of modules that have access to the objects that are marked as Internal using the Access property from the current module.

How can we use that?

As an example, here I’ve created an app A with a codeunit (called Task Management) declared as Internal. In this codeunit I have a public procedure, a local procedure and an internal event:

This means that this codeunit can be used only internally to extension A and the internal event can be subscribed only internally in extension A.

Now I create an extension B that depends from A and on this extension I want to use the Task Management codeunit declared on A. If I try to reference the codeunit, this is obviously the result:

The codeunit is not accessible from B due to its protection level!

To grant extension B to access the Internal objects declared on extension A you need to open the extension’s A app.json file and add the internalsVisibleTo property as follows:

Here I’ve placed the references of the extension (B) that can see the internals objects declared on this app (A).

What happens now if you publish extension A and if you download symbols again from extension B?

Magically, you can now reference the internal codeunit and see its internal methods:

and you can also subscribe to the internal events:

I think that this property can be useful on some scenarios where you have some libraries that you want to share from a main app to certain dependent apps (submodules) but without exposing your business logic to all the possible dependent apps. Quite hidden but remember that you have also this possibility

Comment List
Related
Recommended