Best Practice View State 2:
This is a good example to reduce your view state using transient keyword.You can test this by removing transient keyword for one of attribute in controller try to save and note down your view state size.
public with sharing class DF12TransientController {
// we keep the 'client' member variable as part of the viewstate,
// because we want to track the state of something that we're attempting
// to edit. all other variables are read-only on the VF page, so we
// can make them 'transient' and remove them from viewstate. if you
// want to see the difference made on the page's viewstate, remove the
// 'transient' keyword from the 3 collections below and look at the
// viewstate inspector from the dev footer.
public Contact client { get; private set; }
transient public List<Contact> connections { get; private set; }
transient public List<Account> previousEmployers { get; private set; }
transient public Set<String> hashTags { get; private set; }
public DF12TransientController() {
this.client = retrieveClient();
this.connections = retrieveConnections();
this.previousEmployers = retrievePreviousEmployers();
this.hashTags = retrieveHashTags();
}
// just retrieve a sample contact ... any one will do
private Contact retrieveClient() {
return [
SELECT
Title,
Phone,
Name,
MobilePhone,
MailingStreet,
MailingState,
MailingPostalCode,
MailingCountry,
MailingCity,
LastName,
Id,
HomePhone,
FirstName,
Email,
Birthdate,
AssistantPhone,
AssistantName,
AccountId
FROM Contact
LIMIT 1
];
}
// clearly, these aren't really 'connections' to the client above.
// we just want some sample data to put on the page.
private List<Contact> retrieveConnections() {
return [
SELECT Id, Name
FROM Contact
OFFSET 1
];
}
// dummy data
private List<Account> retrievePreviousEmployers() {
List<Account> a = new List<Account>();
a.add(new Account(Name = 'GE'));
a.add(new Account(Name = 'Siemens'));
a.add(new Account(Name = 'EADS'));
return a;
}
// dummy data
private Set<String> retrieveHashTags() {
Set<String> s = new Set<String>();
s.add('#where');
s.add('#dreamforce');
s.add('#pto');
s.add('#highlights');
s.add('#bestpractice');
s.add('#protip');
s.add('#iwantapony');
s.add('#hr');
return s;
}
public void save() {
try {
insert client;
} catch (Exception x) {
client.addError(x.getMessage());
}
}
@isTest
private static void testGetMemberVariables() {
List<Contact> c = new List<Contact>();
c.add(new Contact(LastName = 'Smith1', FirstName = 'Joe1', Email = 'test@test.test.test.test'));
c.add(new Contact(LastName = 'Smith2', FirstName = 'Joe2', Email = 'test@test.test.test.test'));
insert c;
DF12TransientController con = new DF12TransientController();
System.assert(con.client != null, 'There should be a contact here.');
System.assert(con.connections.size() > 0, 'There should be connections here.');
System.assertEquals(3, con.previousEmployers.size(), 'There should be 3 employers here.');
System.assertEquals(8, con.hashtags.size(), 'There should be 8 hashtags here.');
}
}
<apex:page controller="DF12TransientController" docType="html-5.0" showHeader="false" standardStylesheets="false">
<apex:composition template="DF12Template">
<apex:define name="pageTitle">DF '12: transient Keyword Example</apex:define>
<apex:define name="pageStylesheets">
<apex:stylesheet value="{!$Resource.df_css}"/>
</apex:define>
<apex:define name="header">
<h1 class="pageTitle"><span class="keyword">transient</span> Example</h1>
</apex:define>
<apex:define name="body">
<apex:form >
<h3>Client Information</h3>
<apex:pageBlock >
<apex:pageBlockButtons location="top">
<apex:commandButton action="{!save}" value="Save"/>
</apex:pageBlockButtons>
<apex:pageBlockSection columns="2">
<apex:inputField value="{!client.FirstName}"/>
<apex:inputField value="{!client.LastName}"/>
<apex:inputField value="{!client.Email}"/>
<apex:inputField value="{!client.Phone}"/>
<apex:inputField value="{!client.MobilePhone}"/>
<apex:inputField value="{!client.MailingStreet}"/>
<apex:inputField value="{!client.MailingCity}"/>
<apex:inputField value="{!client.MailingState}"/>
<apex:inputField value="{!client.MailingPostalCode}"/>
<apex:inputField value="{!client.MailingCountry}"/>
<apex:inputField value="{!client.HomePhone}"/>
<apex:inputField value="{!client.Birthdate}"/>
<apex:inputField value="{!client.AssistantName}"/>
<apex:inputField value="{!client.AssistantPhone}"/>
<apex:inputField value="{!client.Title}"/>
<apex:inputField value="{!client.AccountId}"/>
</apex:pageBlockSection>
</apex:pageBlock>
<h3>Connections</h3>
<apex:repeat value="{!connections}" var="c">
<apex:outputText value="{!c.Name}, "/>
</apex:repeat>
<apex:outputText value=" and 500+ more."/>
<h3>Previous Employers</h3>
<apex:dataTable value="{!previousEmployers}" var="e">
<apex:column value="{!e.Name}"/>
</apex:dataTable>
<h3>Frequently Used Hashtags</h3>
<apex:dataTable value="{!hashTags}" var="h">
<apex:column value="{!h}"/>
</apex:dataTable>
</apex:form>
</apex:define>
</apex:composition>
</apex:page>
No comments:
Post a Comment