Good Supported way to add save method to grails service

I watched an example grails app for an animal clinic on github .

He has a service for creating pets called PetclinicService, in which there is a way to add pets:

Pet createPet(String name, Date birthDate, long petTypeId, long ownerId) {
    def pet = new Pet(name: name, birthDate: birthDate, type: PetType.load(petTypeId), owner: Owner.load(ownerId))
    pet.save()
    pet
}

which is used from the controller as follows:

def pet = petclinicService.createPet(params.pet?.name, params.pet?.birthDate,
    (params.pet?.type?.id ?: 0) as Long, (params.pet?.owner?.id ?: 0) as Long)

I'm curious to know if this is the best way to keep something in the grail? With this approach, if I add another field to the domain Pet, say String color, then I have to touch the three classes ( Pet, PetController, and PetclinicService) to make the change complete.

Is there a way to send the entire object paramsto the service and will it automatically appear in the domain?

+5
source share
3

, params, . -, -. , , . , "" , , . , .

, , , , , , .

params , , , , .

+7

, , .

params . , , API . , , .

+2

You can send everything paramsto the service, just declare how Map:

Class PetclinicService {
  Pet createPet(Map params) {
    def pet = new Pet(params)
    pet.save()
    pet
  }
}
+1
source

All Articles