None of them are optimal ...
First, forget about performance first, think about design.
You should do this through an interface or the like:
public interface IConsProvider { string UserName { get; set; } }
Now your implementations (NOTE: you should not compile for both desktop and web sites in one assembly. System.Web, for example, is not available in the client profile that you really should use for desktop applications).
public class WebConsProvider : IConsProvider { public string UserName {
And then your environment is static:
public static class Cons {
Now you have an extensible provider whose implementation you do not need to worry about.
I also have a problem with wrapping HttpContext.Current - however, in most scripts that work fine - if you have any asynchrony, however, you have to be careful.
Also, as I mentioned in my comments, now you no longer need to wrap properties as static in Cons . In fact, you get terrible multi-problem and extensibility by changing the code as follows:
public void TraceUserName() { Trace.WriteLine(Cons.UserName ?? "[none]"); }
For this:
public void TraceUserName(IConsProvider provider) { Trace.WriteLine(provider.UserName ?? "[none]"); }
Believe me, there will be times in your code when you want "just for this call, I would like to redefine the username," but I canβt, because this is a static property. "
Finally, you now have another extensibility mechanism that you don't use with statics: extension methods.
Suppose you add a generic storage engine to the interface for strings:
string this[string key] { get; set; }
So, for the row indexer, which allows us to implement vocabulary functionality for unexpected values. Suppose both of them were implemented with Dictionary<string, string> in DefaultConsProvider and wrapping Session in WebConsProvider ).
Now, if I am writing an additional module for your project that requires an additional string value, I can do this:
public static MySettingsExtensions { public static string GetMySetting(this IConsProvider provider) {
(Sorry, I had to update the last bit, because for some reason I parameterize the key - it was pointless!)
That is - now we can begin to expand the range of strongly typed settings offered by the provider, using extension methods - without having to change any source code.