Simple question really,
I am just looking for community support what works best architecturally.
Say, following case: TempData["MySetting"]
? Where should I store "MySetting"
? Should it be
TempData[Config.MySetting];
TempData[ConfigurationManager.AppSettings["MySetting"];
TempData["MySetting"];
?in the DB in some constants table, so that
var mySetting = (string)_db.GetConstant("MySetting");
TempData[mySetting];
I would not deal with option 4, just brought it for completeness of the picture. So what do you suggest and why?
Edit Question is rather general about constants in asp mvc rather than TempData case.
Thanks.
In your particular case you should make a wrapper around TempData
which will expose your data as property. All the access to TempData
should be hidden inside that wrapper. In this case all the usages of "MySetting"
will be limited to at most two. If there's only one usage, you don't need to do anything. If there're two - you define your "MySetting"
as private const
field.
In short:
public static class TempDataWrapper
{
private const string _mySettingName = "MySetting";
public int MySetting
{
get { return (int) TempData[_mySettingName];}
set { TempData[_mySettingName] = value;}
}
}
For the sake of brevity we are not dealing here with coupling (which is tight) and error-handling (which is none).
I would go with:
public static class Config
{
public static readonly string MyValue = "SomeValue";
}
More about Const vs Readonly
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With