代码之家  ›  专栏  ›  技术社区  ›  Khaja Minhajuddin

使用nhibernate访问存储在数据库表中的ASP.NET MVC应用程序的网站设置

  •  1
  • Khaja Minhajuddin  · 技术社区  · 15 年前

    我有一个ASP.NET MVC应用程序,它依赖于许多设置(名称-值对),我计划将这些信息存储在一个名为SiteSettings的数据库表中。有没有一个简单的方法,我可以得到这些设置使用NHibernate。以及保存Web应用程序设置时的最佳实践。通过设置,我指的是控制Web应用程序中流程流的设置,这些设置由业务规则控制。这些不是典型的连接字符串类型的设置。关于这个话题,我在网上得不到太多的信息。也许我没有在正确的关键字上搜索,任何帮助都会非常感谢。

    3 回复  |  直到 15 年前
        1
  •  1
  •   James S    15 年前

    我无法回答NHibernate(我没有使用)或最佳实践(我最近自己提出了这个问题)。但是,它对我来说很好,而且很可能对你有用。

    我在数据库中有一个表(biz_config)来存储业务首选项。(我为我称之为“首选项”的内容创建了一个web.config节。)

    我有一个班负责管理商业偏好。构造函数抓取整个表(每个设置一行)并将其复制到字典中,它具有访问(如bizconfig.get(“key”))和更新此字典的方法,同时也更新表。它还有一些特定字典值的快捷属性,特别是在必须强制转换值的地方(我有一些重要的数字)。它工作得很好。

    为了更高效,并且不在每次需要设置时实例化它,也为了从控制器和视图轻松访问它,我创建了一个静态类Globals,负责从会话或应用程序变量中获取内容。对于biz-config对象,它检查应用程序变量,如果为空,则创建一个新的变量。否则它只会返回它。globals是我的helpers名称空间的一部分,它包含在我的web.config中,以供我的视图使用。所以我可以很容易地打电话给:

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

    我希望这有帮助。如果你想要密码,我可以帮你查出来。

    詹姆斯

        2
  •  0
  •   Jon Adams    15 年前

    如果有一组键/值对,则可能需要使用 <map> . 见 official NHibernate documentation Ayende's post about 'NHibernate Mapping - <map/>' .

        3
  •  0
  •   Khaja Minhajuddin    15 年前

    我想出了一个与詹姆斯建议的方案非常相似的方案。我有一个 网站设置服务 类,它管理整个网站的设置,它对一个名为 ISiteServiceRepository服务库 .这可能不是最优雅的解决方案,但它对我来说非常有效。我还将SiteSettingsService类配置为 独生子女 使用结构映射。因此,每次我需要任何设置时,它都会为我节省不必要的实例化。

    //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);
            }
    
        }
    }
    

    希望这能帮助未来面临类似问题的开发人员。任何改善这一点的建议都非常受欢迎。

    推荐文章