How to support a singleton domain class in grails

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

How to support a singleton domain class in grails

rjain7

All,

Is there a code pattern in grails on how to implement a singleton domain class ?  I basically want to use a grails domain class to read / modify my application configuration in the relational DB.  In particular, I need to address the following:

1. Ensure that the singleton instance is read from database at startup time (ApplicationBootStrap.groovy ?)

2. Ensure that any access to domain object in application returns the global instance of the domain object.

3. Ensure that new() operation on the domain object returns a pre-existing handle.

I looked at groovy documentation, it suggests use of meta class:
http://groovy.codehaus.org/Singleton+Pattern

However, I am not able to get it to work in an elegant way...Any suggestion on how the meta class should be defined and used with the original domain class ? Or do you suggest a different (more elegant) way ?

Thanks,
Rahul


Need a vacation? Get great deals to amazing places on Yahoo! Travel.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How to support a singleton domain class in grails

Gerd Boerrigter
I am rather new to grails, but I would suggest a different way to achieve  
your goal to get a singelton configuration instance.

Services are by default singeltons. So I would create a  
ConfigurationService. This service is used in all places where the  
configuration needs to be accessed. The service can easily be injected  
into other objects.
The ConfigurationService itself would be the only place, where the domain  
class of the application configuration is used to store and receive the  
settings from the DB.

   Gerd


Am 14.06.2007, 02:03 Uhr, schrieb Rahul Jain <[hidden email]>:

> All,
>
> Is there a code pattern in grails on how to implement a singleton domain  
> class ?  I basically want to use a grails domain class to read / modify  
> my application configuration in the relational DB.  In particular, I  
> need to address the following:
>
> 1. Ensure that the singleton instance is read from database at startup  
> time (ApplicationBootStrap.groovy ?)
>
> 2. Ensure that any access to domain object in application returns the  
> global instance of the domain object.
>
> 3. Ensure that new() operation on the domain object returns a  
> pre-existing handle.
>
> I looked at groovy documentation, it suggests use of meta class:
> http://groovy.codehaus.org/Singleton+Pattern
>
> However, I am not able to get it to work in an elegant way...Any  
> suggestion on how the meta class should be defined and used with the  
> original domain class ? Or do you suggest a different (more elegant) way  
> ?
>
> Thanks,
> Rahul
>
>
> ____________________________________________________________________________________Ready  
> for the edge of your seat?
> Check out tonight's top picks on Yahoo! TV.
> http://tv.yahoo.com/



--
Erstellt mit Operas revolutionärem E-Mail-Modul: http://www.opera.com/mail/


---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Re: How to support a singleton domain class in grails

jondo_w
In reply to this post by rjain7
Gerd, agree, and that is in fact exactly what Glen Smith does in GroovyBlogs. You can grab his source code at http://www.bytecode.com.au/downloads/grails/ for an example.

- Darryl

----- Original Message ----
From: Gerd Boerrigter <[hidden email]>
To: [hidden email]
Sent: Thursday, June 14, 2007 10:40:37 AM
Subject: [grails-user]  Re: How to support a singleton domain class in grails

I am rather new to grails, but I would suggest a different way to achieve  
your goal to get a singelton configuration instance.

Services are by default singeltons. So I would create a  
ConfigurationService. This service is used in all places where the  
configuration needs to be accessed. The service can easily be injected  
into other objects.
The ConfigurationService itself would be the only place, where the domain  
class of the application configuration is used to store and receive the  
settings from the DB.

   Gerd


Am 14.06.2007, 02:03 Uhr, schrieb Rahul Jain <[hidden email]>:

> All,
>
> Is there a code pattern in grails on how to implement a singleton domain  
> class ?  I basically want to use a grails domain class to read / modify  
> my application configuration in the relational DB.  In particular, I  
> need to address the following:
>
> 1. Ensure that the singleton instance is read from database at startup  
> time (ApplicationBootStrap.groovy ?)
>
> 2. Ensure that any access to domain object in application returns the  
> global instance of the domain object.
>
> 3. Ensure that new() operation on the domain object returns a  
> pre-existing handle.
>
> I looked at groovy documentation, it suggests use of meta class:
> http://groovy.codehaus.org/Singleton+Pattern
>
> However, I am not able to get it to work in an elegant way...Any  
> suggestion on how the meta class should be defined and used with the  
> original domain class ? Or do you suggest a different (more elegant) way  
> ?
>
> Thanks,
> Rahul
>
>
> ____________________________________________________________________________________Ready  
> for the edge of your seat?
> Check out tonight's top picks on Yahoo! TV.
> http://tv.yahoo.com/



--
Erstellt mit Operas revolutionärem E-Mail-Modul: http://www.opera.com/mail/


---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email






 
____________________________________________________________________________________
Don't get soaked.  Take a quick peak at the forecast
with the Yahoo! Search weather shortcut.
http://tools.search.yahoo.com/shortcuts/#loc_weather

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Loading...