Quantcast

how to add a method to a domain class?

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

how to add a method to a domain class?

chichibek bros
Hi group, i need your help and advices, i want to add a simple method to a domain class something like

def fullName = {

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

Re: how to add a method to a domain class?

chichibek bros


2012/7/30 chichibek bros <[hidden email]>
Hi group, i need your help and advices, i want to add a simple method to a domain class something like

def fullName = {
    return firstName + " " + lastName
}

is this a correct implementation and is this a best practice?

Thanks from now!

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

Re: how to add a method to a domain class?

Sebastien Blanc
Hi,
Your implementation is correct but you can get more GString, return statement is optional and use a normal method : 

String fullName()  {
  "$firstName $lastName"
}

but closure will also work
Seb


On Mon, Jul 30, 2012 at 5:25 PM, chichibek bros <[hidden email]> wrote:


2012/7/30 chichibek bros <[hidden email]>
Hi group, i need your help and advices, i want to add a simple method to a domain class something like

def fullName = {
    return firstName + " " + lastName
}

is this a correct implementation and is this a best practice?

Thanks from now!


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

Re: how to add a method to a domain class?

burtbeckwith
In reply to this post by chichibek bros
It's perfectly valid to add methods to domain classes. But that's not a method, it's a Closure.

This is how I'd do it:

   String fullName() { "$firstName $lastName" }

Burt

chichibek bros wrote
2012/7/30 chichibek bros <[hidden email]>

> Hi group, i need your help and advices, i want to add a simple method to a
> domain class something like
>
> def fullName = {
>     return firstName + " " + lastName
> }
>

is this a correct implementation and is this a best practice?

Thanks from now!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: how to add a method to a domain class?

Jean-Henri Duteau
In reply to this post by chichibek bros
As others have commented, you are not using Groovy idioms and you've defined a closure instead of a method.  You should do:

def fullName { "$firstName $lastName"}

A good Groovy book can help you develop good Groovy idiom.

Jean Duteau

Sent from my iPhone

On 2012-07-30, at 4:25 PM, chichibek bros <[hidden email]> wrote:



2012/7/30 chichibek bros <[hidden email]>
Hi group, i need your help and advices, i want to add a simple method to a domain class something like

def fullName = {
    return firstName + " " + lastName
}

is this a correct implementation and is this a best practice?

Thanks from now!

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

Re: how to add a method to a domain class?

Greg Pagendam-Turner
You may want to add it to transients also so that GORM doesn't try to save your derived field.

    static transients = ['fullName']


Regards,

Greg


On 31/07/12 8:41 AM, Jean Duteau wrote:
As others have commented, you are not using Groovy idioms and you've defined a closure instead of a method.  You should do:

def fullName { "$firstName $lastName"}

A good Groovy book can help you develop good Groovy idiom.

Jean Duteau

Sent from my iPhone

On 2012-07-30, at 4:25 PM, chichibek bros <[hidden email]> wrote:



2012/7/30 chichibek bros <[hidden email]>
Hi group, i need your help and advices, i want to add a simple method to a domain class something like

def fullName = {
    return firstName + " " + lastName
}

is this a correct implementation and is this a best practice?

Thanks from now!



--
Greg Pagendam-Turner
Founder
Liftyourgame.com - It's daily motivation!
Email: [hidden email]
Mobile: 61 (0) 411 020 467
IM: netroworx (Skype)
http://www.linkedin.com/in/turner
Liftyourgame.com
22/8 Earnshaw Street
Calamvale, Queensland 4116 Australia
See who we know in common
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: how to add a method to a domain class?

chichibek bros
Thanks for your answer, kind regards

2012/7/30 Greg Pagendam-Turner <[hidden email]>
You may want to add it to transients also so that GORM doesn't try to save your derived field.

    static transients = ['fullName']


Regards,

Greg



On 31/07/12 8:41 AM, Jean Duteau wrote:
As others have commented, you are not using Groovy idioms and you've defined a closure instead of a method.  You should do:

def fullName { "$firstName $lastName"}

A good Groovy book can help you develop good Groovy idiom.

Jean Duteau

Sent from my iPhone

On <a href="tel:2012-07-30" value="+50520120730" target="_blank">2012-07-30, at 4:25 PM, chichibek bros <[hidden email]> wrote:



2012/7/30 chichibek bros <[hidden email]>
Hi group, i need your help and advices, i want to add a simple method to a domain class something like

def fullName = {
    return firstName + " " + lastName
}

is this a correct implementation and is this a best practice?

Thanks from now!



--
Greg Pagendam-Turner
Founder
Liftyourgame.com - It's daily motivation!
Email: [hidden email]
Mobile: 61 (0) 411 020 467
IM: netroworx (Skype)
http://www.linkedin.com/in/turner
Liftyourgame.com
22/8 Earnshaw Street
Calamvale, Queensland 4116 Australia
See who we know in common

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

Re: how to add a method to a domain class?

sethfuller
If you make the method getFullName() { "$firstName $lastName"}
You can refer to it as myDomainInstance.fullName. You also don't need to add this to transients. As I understand it only if add both a getter and a setter would you need to add it to transients. I have several methods in my domain classes defined as getSomething() and don't add them to transients with no problems.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: how to add a method to a domain class?

sethfuller
I forgot the return type, so the method would be:
String getFullName() {"$firstName $lastName"}
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: how to add a method to a domain class?

chichibek bros
In reply to this post by sethfuller
some days ago a symfony framework developer friend told me if in grails is there a place were you can create all sql queries and call it like getAllSomething() but not in controllers after some of your answer i was wondering if the domain class is that place?

2012/7/31 sethfuller <[hidden email]>
If you make the method getFullName() { "$firstName $lastName"}
You can refer to it as myDomainInstance.fullName. You also don't need to add
this to transients. As I understand it only if add both a getter and a
setter would you need to add it to transients. I have several methods in my
domain classes defined as getSomething() and don't add them to transients
with no problems.



--
View this message in context: http://grails.1312388.n4.nabble.com/how-to-add-a-method-to-a-domain-class-tp4632425p4632434.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
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: how to add a method to a domain class?

burtbeckwith
In reply to this post by chichibek bros

That's true in 2.0+.

Burt

sethfuller <[hidden email]> wrote:
If you make the method getFullName() { "$firstName $lastName"}
You can refer to it as myDomainInstance.fullName. You also don't need to add
this to transients. As I understand it only if add both a getter and a
setter would you need to add it to transients. I have several methods in my
domain classes defined as getSomething() and don't add them to transients
with no problems.



--
View this message in context: http://grails.1312388.n4.nabble.com/how-to-add-a-method-to-a-domain-class-tp4632425p4632434.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
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: how to add a method to a domain class?

sethfuller
I didn't realize that was only 2.0+. I guess that's why you didn't suggest it in your original answer.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: how to add a method to a domain class?

sethfuller
In reply to this post by chichibek bros
It may depend on your version of Grails. I know in 2.0+ it is good to have getAllSomething() type methods in domain classes.

chichibek bros wrote
some days ago a symfony framework developer friend told me if in grails is
there a place were you can create all sql queries and call it like
getAllSomething() but not in controllers after some of your answer i was
wondering if the domain class is that place?

2012/7/31 sethfuller <[hidden email]>

> If you make the method getFullName() { "$firstName $lastName"}
> You can refer to it as myDomainInstance.fullName. You also don't need to
> add
> this to transients. As I understand it only if add both a getter and a
> setter would you need to add it to transients. I have several methods in my
> domain classes defined as getSomething() and don't add them to transients
> with no problems.
>
>
>
> --
> View this message in context:
> http://grails.1312388.n4.nabble.com/how-to-add-a-method-to-a-domain-class-tp4632425p4632434.html
> Sent from the Grails - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>
Loading...