我有一个共享点项目,我们在其中覆盖搜索框和其他一些东西。我们还有一个响应搜索框的 Web 部件页面。所有这些组件应共享单一配置。我考虑过创建一个单例,但我不确定如何清理/从内存中删除/卸载。
有任何想法吗?关于在共享点中使用单例的任何警告?此外,是否有适当的方法在所有这些组件之间共享对象的实例?
编辑:我在考虑一个非常简单的单例。配置最多只需要包含5-20个字符串和十几个整数。没有复杂的对象:)
最佳答案
只要您的单例类管理的对象是线程安全的,就不应该有问题。
示例:
我有一系列对象调用需要很长时间才能处理(7 秒以上)。所以,我决定尝试使用 cometd 式的长轮询技术来处理它们。因此,我在单个线程中托管服务(作为静态单例)并使用异步 HttpHandler 处理请求......而且效果很好!而且因为我使用的是异步服务,所以它非常高效,因为调用线程会立即释放(处理完成后服务完成回调)。
编辑:
但是您的处理时间长的问题仍然存在,因此您仍然(可能)需要一个异步解决方案来进行初始提取。这个怎么样?为什么不将 asych 解决方案与自定义缓存相结合?
由于各种(可访问性)原因,我将我的 appSetting 保存在数据库中,然后使用自定义缓存进行访问。
自定义缓存示例:
public enum ConfigurationSection
{
AppSettings
}
public static class Utility
{
#region "Common.Configuration.Configurations"
private static Cache cache = System.Web.HttpRuntime.Cache;
public static String GetAppSetting(String key)
{
return GetConfigurationValue(ConfigurationSection.AppSettings, key);
}
public static String GetConfigurationValue(ConfigurationSection section, String key)
{
Configurations config = null;
if (!cache.TryGetItemFromCache<Configurations>(out config))
{
config = new Configurations();
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
}
namespace Common.Configuration
{
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; // Db-Access layer
#endregion
#region EVENTS
#endregion
#region METHODS
private void initialize()
{
_crud = new Crud(Utility.ConnectionName);
}
/// <summary>
/// Lists one-to-many records.
/// </summary>
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
}
public class Configuration
{
#region CONSTRUCTORS
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(Utility.ConnectionName);
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
}
}
关于c# - SharePoint Web 部件和单例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5956597/