Вопрос

From what I have read most people either place command objects inside the controller (which I am not a fan of) or place them somewhere inside the src/groovy folder.

I, however, like how Grails has specific folders for controllers, domains, services, etc and was thinking it would be great to simply create a new source folder for command objects. Either /grails-app/commands or perhaps /src/commands. Having it laid out this way would help encourage the use of command objects.

I am a little concerned that doing this may break some grails magic that relies on specific project structure conventions. After running a few quick tests, I did verify that data binding and domain constraint importing still seem to function correctly.

Any thoughts or objections to this approach?

Это было полезно?

Решение

Unlike top level Grails artifacts like domain classes, services, taglibs etc., the framework doesn't do anything special with command object classes. An exception to that is if they are defined within a controller source file for a controller which uses the class as a command object, in which case the compiler will automatically rig up the validateable stuff without having to mark the class with the Validateable annotation. If you are defining the command object class in its own source file then you will need the Validateable annotation if you want the thing to be validateable (not all command objects need to be made validateable). Given that, I don't think there is any benefit to having a special place to define these classes source files. The normal thing to do if they aren't defined in a controller source file is to define them under src/groovy/ and unless you have some good reason to deviate from that convention, you probably shouldn't.

Command object classes are almost special kinds of things in Grails, but not really. You can use anything as an argument to a controller action. You could use a class provided by Log4J as a command object if there was some reason to that. Having a special place to define them isn't really justified, IMO.

I hope that helps.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top