I have been consistently using Singleton value converters in WPF. But recently I got into an argument with a colleague where he was saying like it is bad to use singleton instance of valueconverters as they would be disposed only by the app domain unload. He was suggesting that singleton converters may come handy only in case of a page which is kept loaded until an application unload. Would really like to know views of WPF experts here.
EDIT (with example): I have a converter like
public class ABCConverter : IMultiValueConverter
{
private static ABCConverter _instance;
public static ABCConverter Instance
{
get { return _instance ?? (_instance = new ABCConverter()); }
}
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return true;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
return false;
}
}
I presently use it in xaml as
Converter="{x:Static conv:ABCConverter.Instance}"
Thanks in advance.
Raj
What you are arguing over here is a micro-optimisation. Unless your value converter is quite massive, the memory it consumes will be quite small.
Do not optimize prematurely. The 'standard' pattern for value converters is to create a new instance within each binding, i.e. avoiding singletons. If you do find yourself encountering memory issues, optimize at that point. Use profiling tools to determine where the issue is and target them specifically. I am willing to bet that your value converter will not be teh root cause of a memory issue.