2009-07-11 45 views
1

我有一個ASP.NET MVC應用程序依賴於很多設置(名稱 - 值對),我打算存儲此信息在名爲SiteSettings的數據庫表中。有一種簡單的方法,我可以使用NHibernate獲得這些設置。什麼是保存Web應用程序設置的最佳做法。通過設置,我的意思是控制Web應用程序中流程流程的設置,這些設置由業務規則來管理。這些不是典型的連接字符串類型的設置。我無法在網上獲得關於此主題的大量信息。也許我沒有搜索正確的關鍵字,任何幫助將不勝感激。使用NHibernate訪問存儲在數據庫表中的asp.net mvc應用程序的網站設置

回答

0

我想出了一個與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); 
     } 

    } 
} 

希望這可以幫助未來的開發者面臨類似的問題。任何改善這個建議都是值得歡迎的。

1

我不能在nhibernate(我沒有使用)或最佳實踐(最近我自己提出這個問題)的背景下回答。但是,它適用於我,並且可能適合您。

我有一個表(Biz_Config)在數據庫中存儲業務偏好。 (我創造了我稱之爲IT喜好的web.config部分。)

我有一類就是負責管理BIZ偏好。構造函數抓取整個表(每個設置一行)並將它們複製到字典中,並且它具有訪問方法(例如bizconfig.get(「key」))並更新此字典,並同時更新表。它還具有特定字典值的一些快捷方式屬性,特別是在需要轉換值的地方(我有一些重要的數字)。它工作得很好。

爲了提高效率,並且在每次需要設置時都不要實例化它,並且還要從我的控制器和視圖中輕鬆訪問它,我創建了一個靜態類Globals,它負責將事情從會話或應用程序變量。對於biz配置對象,它檢查應用程序變量,如果爲null,則創建一個新變量。否則它只是返回它。全局變量是我的助手命名空間的一部分,它包含在我的web.config中以供我的視圖使用。所以,我可以很容易地撥打:

<% Globals.Biz_Config.Get("key") %> 

我希望這有助於。如果你想要代碼,我可以爲你挖掘它。

詹姆斯

+0

感謝您的回答,我想出了類似的東西,我會在原始問題中發佈。 – 2009-07-18 08:56:27

相關問題