-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Spike: Application service layer with @service decorator (like Java Spring) #2239
Comments
I think this is similar to the problem we encountered in loopbackio/loopback4-example-shopping#26, where the implementation of |
+1 for Miroslav's comment above, we currently implemented it as a utility. I am not very expert on the difference between service and utility, correct me if I am wrong: my point is if it's only for app/api developer's internal use, utility is good, if we give client access to it, then implement it as a service. |
What about the cron-job/queue use case? Is there any way in the current implementation to have a controller be a singleton? |
It is certainly possible to have a singleton controller class, see SINGLETON scope. I don't think that's a good design though. Controllers should be lightweight classes with a new instance created for each request, so that they can receive request-specific values via dependency injection. Instead of a singleton controller, you should have a singleton service that's holding cron-job/queue state, and inject this service into per-request controller instances. FWIW, singletons will get you only so far. Once you start scaling your app horizontally and run multiple worker processes, you will need to find a different way how to share state. Most likely using a key-value store like Redis. |
Let's convert this story into a spike where we will identify how we want to design LB4 to support services and define follow-up stories to implement necessary changes. User stories to consider:
We also need to figure out the relation to service-proxy concept as described in Calling other APIs and web services to make it clear what's similar, what is different and when to use which concept. Other areas to keep in consideration when creating follow-up stories:
|
@haotangio We have a starting point here - https://loopback.io/doc/en/lb4/Binding.html#configure-binding-attributes-for-a-class. Now a class can be decorated with |
I think we now have enough capabilities to support the
Please let us know what's missing to support your scenarios. |
Hello @raymondfeng. Thank you very much for your update. I'm starting to use LB4 for several projects ahead and just checked in the current version of LB4. The only lacking features to be perfect-to-use (in my feeling as framework users/developers) is about has-many-through relation, and built-in authorization with ACL. But it should be in a different issue ticket :) |
@haotangio, thanks for your support for LoopBack 4! For hasMany Through, there's an ongoing PR to add the support: #2359. I'd like to close this issue as resolved. Please feel free to continue the hasMany Through discussion in the above PR or open a new GitHub issues for other areas. Thanks!! |
As far as I understand, currently if we develop application with LB 4, most of the application logic will be handled by
controllers
(belongs to presentation layer) which are injectedrepositories
(belongs to data access layer). This may make controller very "fat" (fat controller), and this is where we mix the application logic together with transport stuff (routing, HTTP response status, ...), which is somehow not so cool for complex use cases.Moreover, some controllers may share the same application logic, or you even don't need a controller to fulfill a use case (e.g: seeding initial data on system startup). This is where a service object which encapsulates application logic would be helpful. It then could be used in many scenarios depending on external triggers (not only REST/SOAP/GraphQL controllers, but also cron-job handlers, application message-queue event-listeners, ...) via DI.
IMHO, Loopback 4 are kind of similar (or inspired?) by not only MVC, but also DDD (Domain-Driven Design). The usage of controller, repository, entity, factory, context, ... are also familiar to Java Spring. How if we have a similar
@service
decorator as well? Just my personal thought :)The text was updated successfully, but these errors were encountered: