0

This is how our functional code is structured.
Functional code calls ConfigAdapter which can fetch the configurations from any other store.
Now with the change in the requirements and to decouple the configurations, we created a new class called ABCConfigAdapter (Consider "ABC" to be some business specific term).

ABCConfigAdapter has same methods as ConfigAdapter but now has one more extra parameter to be passed called ABCConfigAttributes. ABCConfigAttributes determines the way in which we fetch the configuration for "ABC" business use-case.

Since ConfigAdapter was used where ever the functional code requires some configuration, now the changes are huge. That is not the actual problem, the problem here is how to pass ABCConfigAttributes as the object for ABCConfigAdapter?

Here are some of the ways of doing it:

  1. Since the data in ABCConfigAttributes is specific to a particular thread, we thought of storing ABCConfigAttributes in ThreadLocal and where-ever we want to call ABCConfigAdapter we fetch ABCConfigAttributes from ThreadLocal and pass it on to ABCConfigAdapter
  2. Make changes to pass ABCConfigAttributes from the top class to the hierarchy everywhere!

Obviously #2 is more costly because it has lot of intrusive changes. I want to understand whether is it a good practice to store Business objects in ThreadLocal (as described in #1) in these type of use-cases?

  • ThreadLocal guarantees for every thread a new version. Even `static` seems better. I would go for some persistancy like XML-Preferences or serialization. – Joop Eggen Jul 29 '15 at 12:58
  • `ThreadLocal` is aimed to manage a special variable which has _a different value_ for each thread. Then, ask yourself: If your program has a different `ABCConfigAdapter` for each thread, and there are no `ABCConfigAdapter`s shared between threads, then ThreadLocal is a safe solution. – Little Santi Jul 29 '15 at 13:00
  • Apologies there was a typo in #1 (instead of mentioning ABCConfigAttributes I mentioned ABCConfigAdapter), corrected it! please check now! – Srihari Kalgi Jul 29 '15 at 13:07

0 Answers0