Friday, April 13, 2012

Singleton value converters in WPF

I saw this interesting post about using singleton value converters by Amit:
WPF Binding Converter Best Practices
The discussions there sort of concluded that WPF creates a single instance of converters anyway, so implementing singletons does not add much value.
I guess it is true that for a given "window", the converters are created only once per definition in the Resources.
But, if I have many windows, each with their own resource definitions of these converters, then these converters are atleast created once for each window. If I have 50 converters, and users keep creating and closing windows, then those 50 converters would get created every time a window gets created.
To avoid this, it would make sense to implement converters as singletons as suggested by Amit. With that, my subsequent windows should load faster because they don't need to construct all the converters again.
I am implementing performance improvements in my app and I think this is a small bit that will add up :)

No comments:

Post a Comment