vendredi 9 septembre 2011

Un objet Session / Session Object to share objects

Avant d'implémenter le SimpleBusEvent j'ai eu besoin d'échanger des objets entre différents composants de mon application (Par exemple des attributs d'un record de listgrid lors de sa sélection ou le record en entier dont je peux avoir besoin dans un autre panel) .
 Pour cela j'ai définit une classe utilitaire Session qui me permet de travailler comme avec une Session en Asp.net ou en Php mais coté client. Pour éviter les problèmes usuels de faute de frappe sur la clé j'ai créé des méthodes put et get spécifiques (des accesseurs de Session) pour certains objets couramment utilisés. J'ai tout de même conservé la possibilité d'utiliser la Session de manière générique (<String>, <Object>). Je ne sais pas d'ailleurs si c'est très malin mais pour le développement c'est plus rapide pour faire un essai que d'aller tout de suite ajouter deux accesseurs. Cela fait pas mal de méthodes à écrire mais au final c'est pratique.

Une partie du code
public class Session {private static HashMap<String, Object> map = new HashMap<String,Object>();
   
    public static void put(String cle, Object objet){
        map.put(cle, objet);
    }
   
    public static Object get(String cle){
        return  map.get(cle);
    }  
   
    public static void putNomRubrique(String chaine){
        map.put("NomRubrique", chaine);
    }
   
    public static String getNomRubrique(){
        return (String) map.get("NomRubrique");
    }
  
  
   
    public static RubriqueDTO getRubrique(){
        return (RubriqueDTO) map.get("Rubrique");
    }
   
    public static void putVariable(VariableDTO variable){

  .....
Before I could use the SimpleEventBus I was in the need of using some object in differents place of my application, the component need some way of using common data (for example if I need a record data after its selection in a ListGrid in a panel A and which  I need sometime later in a panel B) . So I wrote a simple Session class acting like Asp.net or Php Session but for client side.
To avoid the problems of typing error on the key string I use typed get and put method for the common object I use. I kept anyway the normal way of using the HashMap <String>, <Object> with two methods, I don't know if it's a good idea but it's practical during development or test something without be obliged to create the two methods. It make a lot of method to write but eventually it's really practise. I know that it is not so good writing manner because if you don't put the value you are interested in in the right place it's difficult to find errors.

Aucun commentaire:

Enregistrer un commentaire

It's your turn / Exprimez vous
What do you think, what would you like to be exposed, on which topic you don't agree....
Qu'en pensez vous, quels sujets aimeriez vous voir traités, sur quels point n'êtes vous pas d'accord....