Why not just service bean instead of managed bean?

Hello

I’m just trying to understand the key concept I might be missing and wondering if someone could enlighten me

As stated in the docs at https://doc.cuba-platform.com/manual-6.9/service_creation.html
“If different services or other Middleware components require calling the same business logic, it should be extracted and encapsulated inside an appropriate managed bean.”

Why is this necesarry? Why not just only have one service with the required business logic? Since a service can also invoke another service.

Thanks

Hi Ronald,

Actually, you can create a service with the required business logic, and then call it from other services.
The key difference here is encapsulation: a service interface will always be available from the client tier, while a managed bean is available only within its tier (middleware, web client, web portal - any of them, but just one).

So, if some logic is designed to be used only by some services on the middleware, it would be a better idea to encapsulate it in a managed bean on the middleware, too.
There is also a small performance penalty in calling services, because such invocations go through an additional interceptor. Besides, unneeded service proxies are created on the clients.

1 Like