C# 从数据库中获取应用程序设置&;隐藏物
在我的办公室,人们认为我们应该将Web.Config的AppSettings放入数据库。因此,我创建了以下内容,但对代码的几个方面有一些疑问 所以我的问题是:C# 从数据库中获取应用程序设置&;隐藏物,c#,asp.net,caching,refactoring,web-config,C#,Asp.net,Caching,Refactoring,Web Config,在我的办公室,人们认为我们应该将Web.Config的AppSettings放入数据库。因此,我创建了以下内容,但对代码的几个方面有一些疑问 所以我的问题是: 实用程序类中包含“Cache Cache=new Cache()”的行可能是错误的,因为它创建了一个新的缓存对象 问:那么,我应该为这条线做些什么 …非常感谢您的帮助 总体目标是能够打这样的电话: Utility.GetConfigurationValue(ConfigurationSection.AppSettings, "myVari
实用程序类中包含“Cache Cache=new Cache()”的行可能是错误的,因为它创建了一个新的缓存对象 问:那么,我应该为这条线做些什么 …非常感谢您的帮助 总体目标是能够打这样的电话:
Utility.GetConfigurationValue(ConfigurationSection.AppSettings, "myVariable");
public static class Utility
{
#region "Configurations"
public static String GetConfigurationValue(ConfigurationSection section, String key)
{
Configurations config = new Configurations();
Cache cache = new Cache(); // <--- This is probably wrong!!!!
if (!cache.TryGetItemFromCache<Configurations>(out config))
{
config.List(SNCLavalin.US.Common.Enumerations.ConfigurationSection.AppSettings);
cache.AddToCache<Configurations>(config, DateTime.Now.AddMinutes(15));
}
var result = (from record in config
where record.Key == key
select record).FirstOrDefault();
return (result == null) ? null : result.Value;
}
#endregion
}
public static class Extensions
{
#region "System.Web.Caching"
public static void Remove<T>(this Cache cache) where T : class
{
cache.Remove(typeof(T).Name);
}
public static void AddToCache<T>(this Cache cache, object item, DateTime absoluteExpiration) where T : class
{
T outItem = null;
if (cache.TryGetItemFromCache<T>(out outItem))
return;
cache.Insert(typeof(T).Name,
item,
null,
absoluteExpiration,
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.Normal,
null);
}
public static bool TryGetItemFromCache<T>(this Cache cache, out T item) where T : class
{
item = cache.Get(typeof(T).Name) as T;
return item != null;
}
#endregion
}
public class Configurations : List<Configuration>
{
#region CONSTRUCTORS
public Configurations() : base()
{
initialize();
}
public Configurations(int capacity) : base(capacity)
{
initialize();
}
public Configurations(IEnumerable<Configuration> collection) : base(collection)
{
initialize();
}
#endregion
#region PROPERTIES & FIELDS
private Crud _crud;
#endregion
#region EVENTS
#endregion
#region METHODS
private void initialize()
{
_crud = new Crud("CurrentDbConnection");
}
public Configurations List(ConfigurationSection section)
{
using (DbCommand dbCommand = _crud.Db.GetStoredProcCommand("spa_LIST_SecConfiguration"))
{
_crud.Db.AddInParameter(dbCommand, "@Section", DbType.String, section.ToString());
_crud.List(dbCommand, PopulateFrom);
}
return this;
}
public void PopulateFrom(DataTable table)
{
this.Clear();
foreach (DataRow row in table.Rows)
{
Configuration instance = new Configuration();
instance.PopulateFrom(row);
this.Add(instance);
}
}
#endregion
}
…并让它自动从缓存或数据库中检索。实用程序代码:
Utility.GetConfigurationValue(ConfigurationSection.AppSettings, "myVariable");
public static class Utility
{
#region "Configurations"
public static String GetConfigurationValue(ConfigurationSection section, String key)
{
Configurations config = new Configurations();
Cache cache = new Cache(); // <--- This is probably wrong!!!!
if (!cache.TryGetItemFromCache<Configurations>(out config))
{
config.List(SNCLavalin.US.Common.Enumerations.ConfigurationSection.AppSettings);
cache.AddToCache<Configurations>(config, DateTime.Now.AddMinutes(15));
}
var result = (from record in config
where record.Key == key
select record).FirstOrDefault();
return (result == null) ? null : result.Value;
}
#endregion
}
public static class Extensions
{
#region "System.Web.Caching"
public static void Remove<T>(this Cache cache) where T : class
{
cache.Remove(typeof(T).Name);
}
public static void AddToCache<T>(this Cache cache, object item, DateTime absoluteExpiration) where T : class
{
T outItem = null;
if (cache.TryGetItemFromCache<T>(out outItem))
return;
cache.Insert(typeof(T).Name,
item,
null,
absoluteExpiration,
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.Normal,
null);
}
public static bool TryGetItemFromCache<T>(this Cache cache, out T item) where T : class
{
item = cache.Get(typeof(T).Name) as T;
return item != null;
}
#endregion
}
public class Configurations : List<Configuration>
{
#region CONSTRUCTORS
public Configurations() : base()
{
initialize();
}
public Configurations(int capacity) : base(capacity)
{
initialize();
}
public Configurations(IEnumerable<Configuration> collection) : base(collection)
{
initialize();
}
#endregion
#region PROPERTIES & FIELDS
private Crud _crud;
#endregion
#region EVENTS
#endregion
#region METHODS
private void initialize()
{
_crud = new Crud("CurrentDbConnection");
}
public Configurations List(ConfigurationSection section)
{
using (DbCommand dbCommand = _crud.Db.GetStoredProcCommand("spa_LIST_SecConfiguration"))
{
_crud.Db.AddInParameter(dbCommand, "@Section", DbType.String, section.ToString());
_crud.List(dbCommand, PopulateFrom);
}
return this;
}
public void PopulateFrom(DataTable table)
{
this.Clear();
foreach (DataRow row in table.Rows)
{
Configuration instance = new Configuration();
instance.PopulateFrom(row);
this.Add(instance);
}
}
#endregion
}
}您已经正确识别了问题-当前代码在每次调用
GetConfigurationValue
时都会创建一个新的缓存
实例,这会破坏缓存的目的。您需要使缓存实例保持静态,而不是每次都创建一个新实例
public static class Utility
{
private static Cache cache = new Cache(); // Static class variable
#region "Configurations"
public static String GetConfigurationValue(ConfigurationSection section, String key)
{
Configurations config = new Configurations();
// Cache cache = new Cache(); --- removed
...
}
}
答案附录:
我确实(事实上)需要将缓存变量指向其他对象。直到我在实用程序类中执行以下操作,它才起作用
private static Cache cache = System.Web.HttpRuntime.Cache;
为什么认为我们应该将应用程序设置放在数据库中?它会在应用程序启动后被缓存。@Frennky:谢谢你的评论……我的老板认为我们必须从数据库中获取所有应用程序设置,原因有二:更改web.config中的任何内容会引导人们离开应用程序;人们在发布时不断地破坏web.config。因此,这是我在这个问题上的第一次尝试。所以…对上述问题的任何评论都会很好!非常感谢你…我今天会尝试的!起初,我想我需要指向一个已经存在的物体。但我认为HttpContext.Current.Cache将是传递和使用的错误对象…所以我在这里提示大家。再次感谢!