I have been developing with Cuba for a while now and the project is growing to a point where the overall structure is becoming a bit messy.
While I have learned a lot about the framework, I still cant decide what should go into the global or core module. My question is what are the big differences between the core and global module, what are considered to be good practices and how to generally go about this when creating services/components.
Right now, I have pretty much placed all my business logic into single @Component’s (no interfaces) in the global module. That includes components that do pure computations, work with the DB, handling 3rd party integrations etc. What I have in the core module are entity listeners and some scheduled tasks. I’d say the code is split roughly 5-95% between the core and global module, respectively, excluding the web module.
If anyone could shed some light on this It would be greatly appreciated.
The first advantage is a clean separation between business logic and UI. The division into services and beans defines exactly which classes are accessible from outside (UI) (boundary) and which are not (beans).
Another advantage is that you can scale the application as the number of users increases. You can find more information here.
First, it is OK to put all logic to the global module if you don’t encounter any problems.
However CUBA advices to put business logic to the core module.
I can name the following practical reasons:
In the CUBA application there are two spring application contexts: one in core module and another is in web module.
Beans declared in the global modules are instantiated twice: first time as part of the core module, second time as part of the web module.
So if you declare some state in the bean, you could sometimes notice some glitches.
Beans declared in core module can use additional persistence APIs: native queries, mybatis, EntityManager, long transactions etc.
Beans declared in global module can only use DataManager.
Implementation of the DataManager in the web module uses DataService - middleware service - under the hood.
Between web and core modules there is a transparent boundary: when middleware service is called, all parameters and return values are serialized and de-serialized under the hood.
Thus if you are using a lot of bulk data loading in your beans, you should expect a little bit higher CPU usage and memory allocation if your beans are located in global module (and called directly from screens - so that web-level bean is called).