Valid alternatives to the strongly-typed Static resource class




Starting from  Visual Studio 2005, it is possible to automatically generate code to get access to resources (in .resx files) by a static strong typed class.

As soon as you try to write a unit test of the class that access the resources via the static class or  you change the way resources are stored and retrieved (i.e. you move them to the db or you switch to a 3rd party library for the localization) you start to feel all the pain due to the static classes:

indeed the code that use that static class violate the OCP and the DIP principles as result lots of application code needs to be changed too.


To improve the design of the code and to make it easier to change and evolve, it's useful to use an instance class instead of the static class and  to access the resources through an interface.


One option
is to write your custom version of the VS code generator for the resources and make it generate an instance class with the interface that it implement, instead of the standard static class. This solution requires to execute the setup and the configuration of the custom generator in all devs PC and is highly coupled with the VS technology: if you plan to write successful sw that last more than 1 version of VS it is not the best option.


What are the valid alternatives to access resources in a convenient way easy to change and evolve then ?



Print | posted @ Thursday, February 24, 2011 10:21 PM

Comments have been closed on this topic.