c# - SharePoint Web Part & Singletons -
i have sharepoint project override search box , other things. have web part page responds search box. these components should share single configuration. thought creating singleton, not sure how ever cleaned / removed memory / uninstalled.
any ideas? warnings using singletons in sharepoint? also, there proper way share instance of object between these componenets?
edit: thinking simple singleton. configuration needs contain 5-20 strings , dozen integers @ most. no complex objects :)
as long objects being managed singleton class thread-safe there shouldn't issue.
example:
have series of object-calls taking long-time process (7+ seconds). so, decided try using comet-styled long-polling technique process them. such, host service (as static singleton) in single thread , process requests using asynchronous httphandler's...and works great! , because i'm using service asynchronously it's highly efficient because calling thread gets released (upon completion of processing service completes callback).
edit:
problem of long processing time still exists, still (probably) need asynchronous solution initial fetch. hows this? why not combine asych-solution custom-caching?
for various (accessibility) reasons, save appsetting's in database , use custom-cache access thereafter.
example of custom cache:
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 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 } }
Comments
Post a Comment