What is the safest most maintainable practice for working with queries in Grails? [closed]

StackOverflow https://stackoverflow.com/questions/12531433

  •  03-07-2021
  •  | 
  •  

문제

I am new to Grails. I have noticed that most tutorials show queries being created and stored in the Controller. It seems like this could really bloat the controller over time, especially as queries grow in greater number and complexity.

In my research, I read that controllers should stay as lean as possible (having to do with requesting and submitting data to/from the model/view, and that heavy querying is discouraged.

How have you gone about creating, storing, and calling queries for your Grails application?

도움이 되었습니까?

해결책

There's a good article about Grails best practices here. According to the post:

Don’t allow the controller to take over another role. The role of a controller is to accept incoming requests, check permissions etc, ask a domain or a service for a result, give the result back to the requester in the desired format such as HTML, JSON, or XML. Keep the controller as thin as possible. Don’t perform business logic, queries, or updates within controllers.

So if you have a more complex logic, I suggest you to build services:

A service is the right candidate for complex business logic or coarse grained code. If required, the service API can easily be exposed as a RESTful/SOAP web service.

다른 팁

This is what Services are for. Put all of your domain manipulation logic in services, and limit your controllers to the role of "middle-man" - bind and validate the request parameters, invoke the service, and render the results.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top