Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails How to call service from static method of domain class?

I have a static method on my domain class, and want to get all the business logic out of the domain class definition into the service, but I can't call the service in the domain class static method since the service itself is defined on the instance not the domain class.

What the best solve for this?

E.g.

class Foo {
   def fooService
   Integer count =0
   String name

   static void updateFoo(String name) {
      def foo = FindByName(name)
      fooService.beforeUpdateProcess(foo)   //fooService unavailable here  
      foo.count+=1
      foo.save()
   }

}

like image 428
GGizmos Avatar asked Dec 18 '22 11:12

GGizmos


1 Answers

Since services are beans, you would access them the way you would generically access any bean from the application context. Grails has a Holders helper for this.:

FooService fooService = grails.util.Holders.applicationContext.getBean('fooService') as FooService
like image 168
Anatoly Avatar answered May 16 '23 08:05

Anatoly