哪里存储网站的单一数据类型
我使用的是asp.net mvc。如何或在哪里存储单个数据?例如。 SubscriptionFee
或IsSiteOffline
。哪里存储网站的单一数据类型
我问了一个关于用户设置的问题here。我是否应该为网站设置做这样的事情,或者除了数据库以外还有其他方法吗?我希望我的用户从网站本身更改这些设置。
我将使用EntityFramework code-first,并会喜欢我是否可以执行如下操作:settings.SubscriptionFee
。
通常,您将这些设置放入Web.config文件的<appSettings>
部分。
一个标准的ASP.NET MVC 3应用程序自带了一些设置已经(内<configuration>
元素):
<appSettings>
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="MyCustomSetting" value="abcd1234" />
</appSettings>
引用它们在你的应用程序中使用ConfigurationManager中类:
using System.Configuration;
string value = ConfigurationManager.AppSettings["MyCustomSetting"];
正如之前所说的,虽然,最好在数据后端创建一个配置表(即SQL Server或任何你使用的)并从那里抓取它们。
在我的一个(非MVC)应用程序中,我创建了一个静态SysProperties类,它将使用应用程序的缓存来保持它们缓存至少5分钟。这个例子没有使用实体框架,但它可以很容易地适应:
public static class SysProperties
{
public static string SMTPServer
{
get
{
return GetProperty("SMTPServer").ToString();
}
}
public static decimal TicketFee
{
get
{
return Convert.ToDecimal(GetProperty("TicketFee"));
}
}
private static object GetProperty(string PropertyName)
{
object PropertyValue = null;
if (HttpContext.Current != null)
PropertyValue = HttpContext.Current.Cache[PropertyName];
if (PropertyValue == null)
{
SqlCommand cmSQL = new SqlCommand("SELECT Value FROM tblProperty WHERE Name = @PropertyName");
cmSQL.Parameters.AddWithValue("@PropertyName", PropertyName);
DataTable dt = Functions.RunSqlCommand(cmSQL);
PropertyValue = dt.Rows[0][0];
if (HttpContext.Current != null)
HttpContext.Current.Cache.Insert(PropertyName, PropertyValue, null, DateTime.UtcNow.AddMinutes(5), System.Web.Caching.Cache.NoSlidingExpiration);
return PropertyValue;
}
else
{
return PropertyValue;
}
}
}
注意:您可以使用此与ConfigurationManager中相同的技术来从Web.config文件,而不是从检索这些值数据库。
只是另一个免费的,这是我用来利用SQL Server的SqlCacheDependency的一些代码。您必须启用SQL Server Broker,但这可以让您将值缓存到内存中,直到SQL Server中的值发生更改。那样,你没有任意的5分钟到期。
此函数旨在检索具有两部分标识符(如用户的全名)的事物,因此它需要三个参数: - 在未缓存该值的情况下运行的SQL查询 - The (即用户ID) - 标识数据类型的任意字符串(即“UserFullName”,“UserEmail”)
你可以很容易地适应这个用单部分标识符检索东西:
// Static constructor ensures that SqlDependency.Start is called before
// we try to use any SqlCacheDependencies
static Functions()
{
SqlDependency.Start(ConnectionString);
}
public static string GetUserFullName(string UserName)
{
return GetSqlCachedValue("SELECT FirstName + ' ' + LastName FROM dbo.tblUser WHERE UserName = @Id", UserName, "UserFullName").ToString();
}
public static string GetEventNameFromId(int Id)
{
return GetSqlCachedValue("SELECT EventName FROM dbo.tblEvents WHERE EventID = @Id", Id, "EventName").ToString();
}
private static object GetSqlCachedValue(string Query, object Id, string CacheName)
{
// Get the cache
System.Web.Caching.Cache currentCache = HttpContext.Current.Cache;
object Value = null;
// We use a standard naming convention for storing items in the application's cache
string cacheKey = string.Format("{0}_{1}", CacheName, Id.ToString());
// Attempt to retrieve the value
if (currentCache != null && currentCache[cacheKey] != null)
Value = currentCache[cacheKey];
// If the value was not stored in cache, then query the database to get the value
if (Value == null)
{
// Run the query provided to retrieve the value. We always expect the query to have the @Id parameter specified, that we can use
// to plug-in the Id parameter given to this function.
SqlCommand Command = new SqlCommand(Query);
Command.Parameters.AddWithValue("@Id", Id);
// Generate a cache dependency for this query
System.Web.Caching.SqlCacheDependency dependency = new System.Web.Caching.SqlCacheDependency(Command);
// Run the query
DataTable dt = RunSqlCommand(Command);
if (dt.Rows.Count == 1)
{
// Grab the value returned
Value = dt.Rows[0][0];
// Save the value in the cache, so next time we don't have to query SQL Server
if (currentCache != null)
{
currentCache.Insert(cacheKey, Value, dependency);
}
}
}
// return the final value
return Value;
}
最好是,特别是因为您希望允许用户更改这些设置,请将它们存储在您作为后端的任何数据存储中,如SQL或其他。
要使用设置,您可以将它们置于应用程序缓存中,并创建对数据存储的依赖关系,以便任何更新都会使缓存过期。你甚至可以使用静态类,但你必须自己实现管理。