Techniques for sharing a value among classes in a program
Posted
by Kenneth Cochran
on Stack Overflow
See other posts from Stack Overflow
or by Kenneth Cochran
Published on 2010-03-17T16:25:42Z
Indexed on
2010/03/17
16:31 UTC
Read the original article
Hit count: 369
I'm using
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\MyProgram"
As the path to store several files used by my program. I'd like to avoid pasting the same snippet of code all over the my applcation.
I need to ensure that:
- The path cannot be accidentally changed once its been set
- The classes that need it have access to it.
I've considered:
- Making it a singleton
- Using constructor dependency injection
- Using property dependency injection
- Using AOP to create the path where its needed.
Each has pros and cons.
The singleton is everyone's favorite whipping boy. I'm not opposed to using one but there are valid reasons to avoid it if possible.
I'm already heavily using constructor injection through Castle Windsor. But this is a path string and Windsor doesn't handle system type dependencies very gracefully. I could always wrap it in a class but that seems like overkill for something as simple as a passing around a string value. In any case this route would add yet another constructor argument to each class where it is used.
The problem I see with property injection in this case is that there is a large amount of indirection from the where the value is set to where it is needed. I would need a very long line of middlemen to reach all the places where its used.
AOP looks promising and I'm planning on using AOP for logging anyway so this at least sounds like a simple solution.
Is there any other options I haven't considered? Am I off base with my evaluation of the options I have considered?
© Stack Overflow or respective owner