Just wondering if (in Grails/Java) it was bad practice to call: Foo.list()
in the view/gsp of a MVC design, rather than passing it through the model (ie foos: Foo.list()
) and using that?
Seems to me that since it is so simple of a display, that this is fine, but on the other hand I know that it is bad practice to access the domain object directly from the view.
Thanks in advance.
A very practical reason not to do that is you may end up wanting to use the same view from different actions where you end up filtering the data in some way. You might not always want to process Domain.list().
What you are doing is generally considered as a bad practice. The reason is very simple: your view is tightly linked with your model . Let's discover the consequences :
Foo
instances with pagination enabled. You need therefore to have a max
parameter and it will be used like Foo.list(max: params.max)
inside your view . max
is a parameter out of hundreds that you can imagine (order, sort...). So, your view is not only dependent on the Domain Instance but it also depends on the request params and you have to process them. Conclusion : You can do this for prototyping or for views that will not be reused (like admin stuff for instance). Forget it for other situations.
I would argue that during the prototype phase statements like this are "somewhat" acceptable.
But, more realistically, stuff this simple doesn't really happen much in production code. When you start manipulating data, or say, retrieving a lot of different objects and doing something with them you will find that you might want to return something entirely different to the UI layer...
Now, if you have started building something flash out of that one statement made early on, this will cause you to have to rework parts of the UI code... this is not so good, and can potentially cause headaches.
Also, you've answered you own question: yeah, I would consider it bad practice.
I agree this is bad practice. Even if it is during prototyping, passing it from the controller is not like it is going to add a lot of extra time.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.