Creando mapa disponible globalmente

I am working on an hospital application where i need to show blood group in every second page in drop down and getting values from them to the db tables since blood group are not frequent changing entity so we are planning to create a map with key value pair and make available this map throughout application in order to avid creation of same map multiple time

my question is what can be the best way to achieve this.some of the quick options coming to my mind are

  1. Create a map at application start up and place it in application context
  2. Create a utility class which read a property file and fill map with these values or simply create map with exisitng blood type.

but i am not sure how effective these options are as site will have to handle a good amount to user hits in near future.

Gracias por adelantado

preguntado el 08 de noviembre de 11 a las 15:11

2 Respuestas

Create a utility class that loads these values on system startup or load values when the class loads. Creating a class this way would give few advantages:

  • You can test this class and its functionality by writing test cases for this class. (Check if things are loading properly etc)
  • This makes you less dependent on the context and how the context works. Makes your application less troublesome to move if you, for some reason, need to change application server.
  • Code becomes more readable (BloodGroupUtils.getAll() en comparación con Application.getContext().get("bloodGroups"); o algo similar.)

On Performance, this may be a bit faster. Not necessarily though ( we would need to check several other system usage/parameters to come to that conclusion.)

respondido 08 nov., 11:19

The map in the application context would be loaded in the same way the static class is--the testability of that wouldn't be affected. Every application server gets access to the app context in the same way; it's part of the spec. - Dave Newton

what i meant was, testing the class would be trivial. Testing static classes and methods are problematic, isn't it? - Ravi Bhatt

Testing static classes is no more difficult than anything else. Testing code that usos static classes is more difficult and is one main reason we use DI/IoC--BloodGroupUtils.getAll() solo usa un una experiencia diferente static class. The code that uses the map itself should be decoupled from the mechanism that recupera the map. - Dave Newton

+1 for raising the issue regarding the code that uses the map; I think separating cómo something is created and/or accessed from the code that uses it is very important. - Dave Newton

@RaviBhatt what you mean by the term class loads as in my case i will be using this utility class in my Actions so when my action get called should i put a call to this utility class inside 'action execute method' or what? - Umesh Awasthi

Retrieving the values from a static class or the application context will have essentially the same performance. The static class might be trivially faster as you don't have to get the map out of the application context, but I can't imagine it'd be worth worrying about.

respondido 08 nov., 11:19

@user702325 Take note of the comments between myself and Ravi; it brought a good point about keeping the code that usos the map decoupled from the mechanism that recupera the map, whether from a static class, from the application context, or from anywhere else. - Dave Newton

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.