Find your content:

Search form

You are here

how to rewrite getter setter in different notation

 
Share

Is it possible to rewrite this:

public Assessment__c thisAssessment;

    public Assessment__c getthisAssessment() {
        if (assessmentUrlId!=null) {
            return thisAssessment=[SELECT Id, name
                                    FROM Assessment__c 
                                    WHERE Id = :assessmentUrlId];
        }
        else {
            return new Assessment__c();
        }       
    }       

Into this style notation? For some reason the setter doesn't seem to work when I write it this way.

public Assessment__c thisAssessment {
        get {
            if (assessmentUrlId!=null) {
                return [SELECT Id,name 
                                        FROM Assessment__c 
                                        WHERE Id = :assessmentUrlId];
            }
            else {
                return new Assessment__c();
            }  
        }
        set;
    }

Attribution to: Phil B

Possible Suggestion/Solution #1

It is because your get and set implementations are not balanced. Your get method never actually returns the value of the 'thisAssessment' property your defining. It either returns a queried instance or a new instance. So you can assign it as long as you like, the 'get' will continue to mask the actual value.

I'm not a big fan of set/get methods with side effects nor ones with extensive implementation in them. IMHO they should primarily be used for data access and that's it. Daniels code above gives a good way to fix it up if you want to continue this route.

Personally I like your original method based version better, it is a clearer statement of intent it being a method, that it has behaviour. Doing SOQL in a get is just waiting for a bulkifcation bug as developers are unaware that a simple .thisAssement is doing so much. And as such implementing this way, feels like its giving the wrong impression, as it is not just a data accessor / mutator as most get/set implementations are typically expected to be.

If you search for 'Properties vs Methods' via Google, you'll find some good reading on the topic. Mostly .Net, since it was the first to add this feature (I think?). But you'll get the general idea.


Attribution to: Andrew Fawcett

Possible Suggestion/Solution #2

I'd suggest that you simply don't provide a setter implementation. Just leave it out unless you need it to do something specifically, like set the assessmentUrlId.


Alternatively, you will need to provide an implementation for the setter.

This example uses the implicit property storage for values. An explicit member isn't created to store the value like you would otherwise do in C#.

private Id assessmentUrlId;

public Assessment__c thisAssessment {
    get {
        if(thisAssessment == null) {
            // Typically a getter shouldn't change the objects state.
            if (assessmentUrlId != null) {
                // Use this with caution!
                // You don't want to be doing the same SOQL query every time
                // something accesses this property.
                thisAssessment = [SELECT Id,name 
                                      FROM Assessment__c WHERE Id = :assessmentUrlId];
            } else {
                thisAssessment = new Assessment__c();
                // Should the name be defaulted to something in the new instance?
                // At what point will the new Assessment be inserted?
            }
        }            
        return thisAssessment;
    }
    set {
        thisAssessment = value;

        // Do you want to insert or update any assigned value to the database?
        // I wouldn't recommend this as good practice in a property. 
        // Better that the caller has already inserted it or it is inserted later.
        //upsert value;

        assessmentUrlId = value.Id;
    }
}

With regards to the getter method. Refer to this note from the Force.com Apex Code Developer's Guide on Apex Properties:

We recommend that your get accessor should not change the state of the object that it is defined on.

Instead of doing the SOQL query to retrieve the existing assessment in the Property you could initialize it in the controllers/objects constructor.


Attribution to: Daniel Ballinger
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/4464

My Block Status

My Block Content