none
Is there any optimized way to implement the value converters without using a static or singleton reference ???? RRS feed

  • Question

  •   

      i have created some of the converters  class in my application and which is going to use in so many places. So Im using  the singleton instance to implement the convert operation and im seeking the other better way to implement the converters without using static and singleton instance.


    Sudhesh. G <br/> <a href="http://gurucoders.blogspot.com" target="_blank">http://gurucoders.blogspot.com</a><br/>

    Monday, January 6, 2014 10:07 AM

Answers

  • If you add your converter as a resource to your window, then only a single instance of it will be created and used for all conversions where you use it inside this window. Why would you want to make a class that is used from many places as a singleton anyway? For example, the System.String (string) and System.Int32 (int) classes are often used heavily in an application but these classes are not singletons or static...

    You should only use the singleton desing pattern when you want to ensure that a class can only have one instance and when you want to provide a global point of access to it. Converters don't hold their state for the lifetime of the application in general though.

    Monday, January 6, 2014 11:41 AM

All replies

  • Hi,

    Whatever you are doing in the converters can be done in ViewModel (MVVM). So there won't be a need for Converters. If you post some code of the converters we can suggest you how it can be ported to ViewModel. 


    srithar

    Monday, January 6, 2014 10:53 AM
  • If you add your converter as a resource to your window, then only a single instance of it will be created and used for all conversions where you use it inside this window. Why would you want to make a class that is used from many places as a singleton anyway? For example, the System.String (string) and System.Int32 (int) classes are often used heavily in an application but these classes are not singletons or static...

    You should only use the singleton desing pattern when you want to ensure that a class can only have one instance and when you want to provide a global point of access to it. Converters don't hold their state for the lifetime of the application in general though.

    Monday, January 6, 2014 11:41 AM
  • Hi,

    you can call your converter from code behind. Thus, you only have one implementation.

    Even in MVVM it make sense to implement converters and as well, the converter itself can be used in MVVM functions and methods.

    You dont need a static on, just create an instance and call e.g. "Convert".
    May look like "var s = new BooleanToVisibility().Convert(...);".

    Greetings,

    Chris


    • Edited by cpehonk Monday, January 6, 2014 11:59 AM
    Monday, January 6, 2014 11:56 AM