I’m using asp.net mvc. How or where do I store single pieces of data? For eg. SubscriptionFee, or IsSiteOffline.
I asked a question about user-settings here. Should I do something like this for sitesettings or is there another way apart from the database? I’d like my user to change these settings from the site itself.
I will be using EntityFramework code-first and would love if I could do something like: settings.SubscriptionFee.
Typically, you’ll put those settings into the
<appSettings>section of your Web.config file.A standard ASP .NET MVC 3 application comes with a few settings already (inside of the
<configuration>element):To reference them in your application, you use the ConfigurationManager class:
using System.Configuration;string value = ConfigurationManager.AppSettings["MyCustomSetting"];As was said before, though, it may be better to create a configuration table in your data back-end (ie, SQL Server or whatever you use) and grab them from there.
In one of my (non-MVC) applications, I created a static SysProperties class that would use the application’s cache to keep them cached for at least 5 minutes. This example doesn’t use the Entity Framework, but it could very easily be adapted:
Note: You could use this same technique with the ConfigurationManager to retrieve these values from the Web.config file instead of from a database.
Just another free-be, this is some code that I’ve used to take advantage of SQL Server’s SqlCacheDependency. You’d have to enable the SQL Server Broker, but this allows you to keep values cached in memory until the value has changed in SQL Server. That way, you don’t have an arbitrary 5-minute expiration.
This function was intended retrieve things with a two-part identifier (like the full name of a user), so it takes three parameters:
– The SQL query to run in case the value isn’t cached
– The unique ID of the item you’re wanting to retrieve (ie, the User ID)
– An arbitrary string that identifies the type of data (ie, “UserFullName”, “UserEmail”)
You could very easily adapt this to retrieve things with one-part identifiers: