What is the best practice for sharing records among modules in Ballerina?

54 Views Asked by At

I have a Ballerina package consisting of multiple modules, each requiring the same set of record types. Unfortunately, it seems impossible to define these common record types in the main module and have the sub-modules inherit them. The only solution I can think of is creating a separate module exclusively for the shared record types and importing it into all other modules. However, this approach results in the "types" module containing only a list of records without any functionality. Could you confirm if this is the appropriate method or suggest a better design pattern for this scenario?

2

There are 2 best solutions below

0
Chiran Sachintha On BEST ANSWER

In Ballerina, there's currently no other way to share record types across multiple modules other than defining a library module for those record types and importing it in the modules that need to use them. There is no concept of inheritance in Ballerina, so record types cannot be inherited from one module to another.

The best approach is to create a library module that defines the shared record types, and then import it into the other modules that need to use those types.

0
Nipuna Fernando On

Having a separate module for the common records is an appropriate approach for your use case. We have followed a similar approach in the Ballerina YAML module as well to encapsulate the common resources in a single module. If you need to expose those records to users, you can do it through the Ballerina.toml if needed.