I'm currently reviewing code and found CDI converters like:
@Named
@RequestScoped
public class BankConverter implements Converter, Serializable
{
@EJB
private BankService bankService;
@Override
public Object getAsObject( FacesContext ctx, UIComponent comp, String identifier )
{
if ( identifier == null || identifier.trim().isEmpty() )
{
return null;
}
Bank bank = null;
try
{
bank = this.bankService.findByPrimaryKey( Long.valueOf( identifier ) );
}
catch( Exception e )
{
// omitted
}
return bank;
}
@Override
public String getAsString( FacesContext ctx, UIComponent comp, Object obj )
{
if ( obj == null || ( ( Bank ) obj ).getId() == null )
{
return null;
}
return ( ( Bank ) obj ).getId().toString();
}
}
The converters are basically always used like this (note the converter="#{bankConverter}"
):
<p:autoComplete id="bank"
value="#{employeeDepotManager.selectedBank}"
var="bnk"
converter="#{bankConverter}"
completeMethod="#{autoCompleter.completeBankSearch}"
itemLabel="#{bnk.name}"
itemValue="#{bnk}"
forceSelection="false"
minQueryLength="3"
global="true"
validator="#{employeeDepotManager.validateBank}"
scrollHeight="200">
<p:ajax event="itemSelect" update="bank-code bank-name" />
<p:column>#{bnk.code}</p:column>
<p:column>#{bnk.name}</p:column>
</p:autoComplete>
I am currently discussing with a colleague about which scope would be the best for the converters...
95% of the manager beans referenced from the JSF pages are @ViewScoped
and so I thought it would be best for the converters to be @ViewScoped
as well (instead of @RequestScoped, which as far as I understand would recreate a converter instance per AJAX request).
Then my colleague added, that the converter should probably be @Dependent
as this would automatically put the converters into the scope the the surrounding beans are in. My feeling said, this wouldn't work. However, I couldn't really disagree as my knowledge pretty much ends here.
So, what would probably be the best scope for converters when almost all beans referenced from JSF are @ViewScoped
?
PS: Note that we are using Seam 3 to mix @Named
and @ViewScoped