私のオフィスでは、AppSettingsをWeb.Configのデータベースに配置することになっていると見なされています。このように、私は以下を作成しましたが、コードの2つの側面について疑問を抱いています。データベースとキャッシュからAppSettingsを入手する
だから私の質問です:それは新しいキャッシュオブジェクトを作成するため
UTILITYクラスで「キャッシュキャッシュは=新しいキャッシュを()」を含む行は、おそらく間違っています。
Q:だから、私はその行のために何をしていますか?
...助けていただければ幸いです。
Utility.GetConfigurationValue(ConfigurationSection.AppSettings, "myVariable");
...そしてそれがキャッシュまたは自動魔法のように、データベースから取得する必要があり:
全体的な目的は、次のように電話をかけることができるようにしました。
UTILITYコード:
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
}
ITEM-CLASSのCODE:
パブリッククラスコンフィギュ { #regionコンストラクタ
public Configuration()
{
initialize();
}
#endregion
#region PROPERTIES & FIELDS
private Crud _crud;
public string Section { get; set; }
public string Key { get; set; }
public string Value { get; set; }
#endregion
#region EVENTS
#endregion
#region METHODS
private void initialize()
{
_crud = new Crud("CurrentDbConnection");
Clear();
}
public void Clear()
{
this.Section = "";
this.Key = "";
this.Value = "";
}
public void PopulateFrom(DataRow row)
{
Clear();
this.Section = row["Section"].ToString();
this.Key = row["Key"].ToString();
this.Value = row["Value"].ToString();
}
#endregion
}
私たちはAppSettingsをデータベースに置く必要があると思われるのはなぜですか?アプリケーションの起動後にキャッシュされます。 – frennky
@Frennky:コメントありがとう...私の上司はWEが2つの理由からデータベースからすべてのAppSettingsを取得しなければならないと判断しました。web.config内の何かを変更すると、アプリケーションから人々が起動します。人々はリリース上でweb.configを台無しにし続けました。このように、これは私の最初の試みです。だから...上の質問に対するコメントは素晴らしいだろう! –