|
This is a sample configuration store that I wrote for a SharePoint implementation that I was working on. The configuration manager has a simple interface that takes two keys (siteID, and Key) and returns the string associated with these keys here is the interface definition
public interface IConfigurationManager
{
string GetConfiguration(string siteID, string key);
}
The implementation for the IConfiguration manager expects to find the list URL and the site URL where the list exists(the list can exist in any site) in the application settings of the web.config. The implementation caches the retrieved values which has its own draw backs as changes to the list are not reflected till the cash expires but for this implementation the requirement did not expect lots of changes.
/// <summary>
/// Reads the configuration information from the Configuration list
/// the URL for the list is stored in application settings in key
/// <add key="ConfigListURL" value="/Configuration/Lists/ConfigurationStore" />
//// <add key="configWebSiteURLKey" value="http://localhost/Configuration"/>
///
/// </summary>
public class ConfigurationManager: IConfigurationManager
{
private const string configListNameKey = "ConfigListURL";
private const string configWebSiteURLKey = "configWebSiteURLKey";
public ConfigurationManager()
{
}
public string GetConfiguration(string siteID, string key)
{
string entry = null;
bool dirty = false;
Dictionary<string, Dictionary<string, string>> globalMap = GetMap();
Dictionary<string, string> siteMap = null;
if(globalMap.ContainsKey(siteID))
{
siteMap = globalMap[siteID];
if(siteMap.ContainsKey(key))
{
entry = siteMap[key];
}
if(null == entry)
{
dirty = true;
entry = GetConfigEnrtyFromSharePoint(siteID, key);
if (entry != null)
{
siteMap[key] = entry;
}
}
}
else
{
dirty = true;
entry = GetConfigEnrtyFromSharePoint(siteID, key);
siteMap = new Dictionary<string, string>();
if (entry != null)
{
siteMap[key] = entry;
}
globalMap[siteID] = siteMap;
}
// update map if need be
if (dirty)
{
HttpContext.Current.Cache[globalMapCacheKey] = globalMap;
}
return entry;
}
private string GetValue(SPListItem item)
{
string value = string.Empty;
try
{
value = item["Value"].ToString();
}
catch
{
Debug.WriteLine("NoValue");
}
return value;
}
const string queryformatstr = "<Where> <And><Eq><FieldRef Name='Title'/><Value Type='Text'>{0}</Value></Eq><Eq><FieldRef Name='SiteID'/><Value Type='Text'>{1}</Value></Eq> </And></Where>";
private string GetConfigEnrtyFromSharePoint(string siteID, string key)
{
string result = null;
string SiteUrl = WebConfigurationManager.AppSettings[configWebSiteURLKey];
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite siteCollection = new SPSite(SiteUrl))
{
using (SPWeb rootweb = siteCollection.OpenWeb())
{
string configlistUrl = WebConfigurationManager.AppSettings[configListNameKey];
SPList list = null;
try
{
list = rootweb.GetList(configlistUrl);
SPQuery oQuery = new SPQuery();
oQuery.Query = string.Format(queryformatstr, key, siteID);
SPListItemCollection collListItems = list.GetItems(oQuery);
if ((null != collListItems) && (collListItems.Count >= 1))
{
result = GetValue(collListItems[0]);
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
}
}
}
);
return result;
}
const string globalMapCacheKey = "globalConfigurationMapCacheKey";
private Dictionary<string, Dictionary<string, string>> GetMap()
{
Dictionary<string, Dictionary<string, string>> map = HttpContext.Current.Cache[globalMapCacheKey] as Dictionary<string, Dictionary<string, string>>;
if (null == map)
{
map = new Dictionary<string, Dictionary<string, string>>();
HttpContext.Current.Cache[globalMapCacheKey] = map;
}
return map;
}
}
}
The Provider just loads the implementation based on the class name and assembly name of an entry in the application settings in web.config.
// <summary>
///
/// Class, Assembly
/// <add key="configManagerProvider" value="ConfigurationManager, GlobalConfiguration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=22d2dcb173b01686"/>
/// </summary>
public class ConfigurationManagerProvider
{
private const string configManagerProviderKey = "configManagerProvider";
static public IConfigurationManager GetConfigurationManager()
{
string str = WebConfigurationManager.AppSettings[configManagerProviderKey];
int isep = str.IndexOf(',');
string assemblyName = str.Substring(isep+1);
string typeName = str.Substring(0,isep);
Assembly asm = Assembly.Load(assemblyName);
IConfigurationManager mngr = asm.CreateInstance(typeName) as IConfigurationManager;
return mngr;
}
}
The consumer would write code similar to
IConfigurationManager cm = ConfigurationManagerProvider.GetConfigurationManager();
lblSiteTitle.Text = cm.GetConfiguration("Site1", "Key1");
|