我有一個ASP.NET MVC應用程序依賴於很多設置(名稱 - 值對),我打算存儲此信息在名爲SiteSettings的數據庫表中。有一種簡單的方法,我可以使用NHibernate獲得這些設置。什麼是保存Web應用程序設置的最佳做法。通過設置,我的意思是控制Web應用程序中流程流程的設置,這些設置由業務規則來管理。這些不是典型的連接字符串類型的設置。我無法在網上獲得關於此主題的大量信息。也許我沒有搜索正確的關鍵字,任何幫助將不勝感激。使用NHibernate訪問存儲在數據庫表中的asp.net mvc應用程序的網站設置
回答
我想出了一個與James建議的解決方案非常相似的解決方案。我有一個SiteSettingsService類,它管理整個站點的設置,它對稱爲ISiteServiceRepository的接口具有簡單依賴性。這可能不是最優雅的解決方案,但它對我來說非常合適。我還使用StructureMap將SiteSettingsService類配置爲Singleton。所以,每當我需要任何設置時,它都會節省不必要的實例。
//ISiteServiceRepository, an implementation of this uses NHibernate to do just two things
//i)Get all the settings, ii)Persist all the settings
using System.Collections.Generic;
using Cosmicvent.Mcwa.Core.Domain.Model;
namespace Cosmicvent.Mcwa.Core.Domain {
public interface ISiteServiceRepository {
IList<Setting> GetSettings();
void PersistSettings(IDictionary<string, string> settings);
}
}
//The main SiteSettingsService class depends on the ISiteServiceRepository
using System;
using System.Collections.Generic;
using Cosmicvent.Mcwa.Core.Domain;
using Cosmicvent.Mcwa.Core.Domain.Model;
namespace Cosmicvent.Mcwa.Core.Services {
public class SiteSettingsService : ISiteSettingsService {
private readonly ISiteServiceRepository _siteServiceRepository;
private IDictionary<string, string> _settings;
public SiteSettingsService(ISiteServiceRepository siteServiceRepository) {
_siteServiceRepository = siteServiceRepository;
//Fill up the settings
HydrateSettings();
}
public int ActiveDegreeId {
get {
return int.Parse(GetValue("Active_Degree_Id"));
}
}
public string SiteTitle {
get { return GetValue("Site_Title"); }
}
public decimal CounsellingFee {
get { return decimal.Parse(GetValue("Counselling_Fee")); }
}
public decimal TuitionFee {
get { return decimal.Parse(GetValue("Tuition_Fee")); }
}
public decimal RegistrationFee {
get { return decimal.Parse(GetValue("Registration_Fee")); }
}
public void UpdateSetting(string setting, string value) {
if (!string.IsNullOrEmpty(setting) && !string.IsNullOrEmpty(value)) {
SetValue(setting, value);
PersistSettings();
}
}
//Helper methods
private void HydrateSettings() {
_settings = new Dictionary<string, string>();
IList<Setting> siteRepoSettings = _siteServiceRepository.GetSettings();
if (siteRepoSettings == null) {
throw new ArgumentException("Site Settings Repository returned a null dictionary");
}
foreach (Setting setting in siteRepoSettings) {
_settings.Add(setting.Name.ToUpper(), setting.Value);
}
}
private string GetValue(string key) {
key = key.ToUpper();
if (_settings == null) {
throw new NullReferenceException("The Site Settings object is Null");
}
if (!_settings.ContainsKey(key)) {
throw new KeyNotFoundException(string.Format("The site setting {0} was not found", key));
}
return _settings[key];
}
private void SetValue(string key, string value) {
key = key.ToUpper();
if (_settings == null) {
throw new NullReferenceException("The Site Settings object is Null");
}
if (!_settings.ContainsKey(key)) {
throw new KeyNotFoundException(string.Format("The site setting {0} was not found", key));
}
_settings[key] = value;
}
private void PersistSettings() {
_siteServiceRepository.PersistSettings(_settings);
}
}
}
希望這可以幫助未來的開發者面臨類似的問題。任何改善這個建議都是值得歡迎的。
我不能在nhibernate(我沒有使用)或最佳實踐(最近我自己提出這個問題)的背景下回答。但是,它適用於我,並且可能適合您。
我有一個表(Biz_Config)在數據庫中存儲業務偏好。 (我創造了我稱之爲IT喜好的web.config部分。)
我有一類就是負責管理BIZ偏好。構造函數抓取整個表(每個設置一行)並將它們複製到字典中,並且它具有訪問方法(例如bizconfig.get(「key」))並更新此字典,並同時更新表。它還具有特定字典值的一些快捷方式屬性,特別是在需要轉換值的地方(我有一些重要的數字)。它工作得很好。
爲了提高效率,並且在每次需要設置時都不要實例化它,並且還要從我的控制器和視圖中輕鬆訪問它,我創建了一個靜態類Globals,它負責將事情從會話或應用程序變量。對於biz配置對象,它檢查應用程序變量,如果爲null,則創建一個新變量。否則它只是返回它。全局變量是我的助手命名空間的一部分,它包含在我的web.config中以供我的視圖使用。所以,我可以很容易地撥打:
<% Globals.Biz_Config.Get("key") %>
我希望這有助於。如果你想要代碼,我可以爲你挖掘它。
詹姆斯
如果你有一組鍵/值對的,你可能想使用一個<map>
。請參閱official NHibernate documentation或Ayende's post about 'NHibernate Mapping - <map/>'。
- 1. Silverlight應用程序的網站 - 在網站上存儲數據
- 2. 用數據庫訪問設置使用Java的網站
- 3. 數據庫連接使用nhibernate爲asp.net mvc應用程序
- 4. asp.net mvc應用程序的網站安全/訪問管理
- 5. 在Web應用程序的數據庫上存儲設置?
- 6. 我應該如何存儲我的ASP.NET MVC網站的設置?
- 7. 在ASP.NET MVC應用程序中緩存和訪問配置數據
- 8. 如何存儲訪問網站和android應用程序的數據?
- 9. 存儲數據庫中網站的系統設置和配置?
- 10. 在NHibernate中使用MVC ASP.NET中的Oracle存儲過程
- 11. Android應用程序設計 - 訪問網站數據
- 12. 爲Grails應用程序存儲和使用數據庫中的應用程序配置設置?
- 13. 從Chrome打包應用程序訪問網站本地存儲數據
- 14. 從ASP.Net MVC訪問應用程序設置查看
- 15. 如何在應用程序庫中緩存/存儲應用程序設置
- 16. 在窗體應用程序中訪問網站應用程序的數據庫文件
- 17. 在Facebook devleopers中設置應用程序時的網站設置?
- 18. 在windows 7中爲asp.net應用程序設置網站
- 19. 使用asp.net的網站和Web應用程序mvc
- 20. 使用多個數據庫的單個ASP.NET MVC應用程序
- 21. 如何在ASP.Net Form網站中部署ASP.Net MVC應用程序
- 22. 在應用程序中存儲iPhone應用程序設置
- 23. ASP.NET MVC中的數據庫訪問3
- 24. Linq和Entity FW數據庫訪問和ASP.NET MVC網站搜索
- 25. 在應用程序中使用greendao訪問數據庫
- 26. 新的網站或MVC應用程序
- 27. 訪問網絡應用程序中的Web數據表
- 28. 如何從asp.net訪問oracle存儲過程mvc + nhibernate
- 29. 在asp.net應用程序中存儲設置
- 30. 使用ASP.NET MVC在數據庫中存儲和讀取HTML?
感謝您的回答,我想出了類似的東西,我會在原始問題中發佈。 – 2009-07-18 08:56:27