Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 使用NHibernate访问存储在数据库表中的asp.net mvc应用程序的网站设置_Asp.net Mvc_Nhibernate_Web Applications_Utilities - Fatal编程技术网

Asp.net mvc 使用NHibernate访问存储在数据库表中的asp.net mvc应用程序的网站设置

Asp.net mvc 使用NHibernate访问存储在数据库表中的asp.net mvc应用程序的网站设置,asp.net-mvc,nhibernate,web-applications,utilities,Asp.net Mvc,Nhibernate,Web Applications,Utilities,我有一个ASP.NET MVC应用程序,它依赖于许多设置(名称-值对),我计划将此信息存储在名为SiteSettings的数据库表中。有没有一种简单的方法可以让我使用NHibernate获得这些设置。保存web应用程序设置时的最佳做法是什么。我所说的设置是指控制web应用程序中的流程的设置,这些设置受业务规则的约束。这些不是典型的连接字符串类型的设置。我在网上找不到关于这个话题的很多信息。也许我没有搜索正确的关键词,任何帮助都将不胜感激。我无法在nhibernate(我没有使用)或最佳实践(我最

我有一个ASP.NET MVC应用程序,它依赖于许多设置(名称-值对),我计划将此信息存储在名为SiteSettings的数据库表中。有没有一种简单的方法可以让我使用NHibernate获得这些设置。保存web应用程序设置时的最佳做法是什么。我所说的设置是指控制web应用程序中的流程的设置,这些设置受业务规则的约束。这些不是典型的连接字符串类型的设置。我在网上找不到关于这个话题的很多信息。也许我没有搜索正确的关键词,任何帮助都将不胜感激。

我无法在nhibernate(我没有使用)或最佳实践(我最近自己提出)的上下文中回答。然而,它对我很有效,并且可能对你也有效

我在数据库中有一个表(Biz_Config)来存储业务首选项。(我已经为我称之为首选项的内容创建了一个web.config部分。)

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

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

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

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


James

我无法在nhibernate(我没有使用)或最佳实践(我最近自己提出)的背景下回答这个问题。然而,它对我很有效,并且可能对你也有效

我在数据库中有一个表(Biz_Config)来存储业务首选项。(我已经为我称之为首选项的内容创建了一个web.config部分。)

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

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

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

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


James

如果您有一组键/值对,可能需要使用
。请参阅或。

如果您有一组键/值对,可能需要使用
。参见or。

我提出了一个与James建议的解决方案非常相似的解决方案。我有一个SiteSettingsService类,它管理整个站点的设置,它对名为IsiteServicePository的接口有一个简单的依赖关系。这可能不是最优雅的解决方案,但它对我来说非常有效。我还使用StructureMap将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);
        }

    }
}
//IsiteServicePository,它的一个实现使用NHibernate只做两件事
//i) 获取所有设置,ii)保留所有设置
使用System.Collections.Generic;
使用Cosmicvent.Mcwa.Core.Domain.Model;
命名空间Cosmicvent.Mcwa.Core.Domain{
公共接口IsiteServicePository{
IList GetSettings();
无效持久化设置(IDictionary设置);
}
}
//主站点设置服务类取决于ISITEServicePository
使用制度;
使用System.Collections.Generic;
使用Cosmicvent.Mcwa.Core.Domain;
使用Cosmicvent.Mcwa.Core.Domain.Model;
命名空间Cosmicvent.Mcwa.Core.Services{
公共类站点设置服务:ISiteSettingsService{
私有只读ISiteService存储库_siteServiceRepository;
私人词典设置;
公共站点设置服务(ISiteService存储站点服务存储库){
_siteServiceRepository=siteServiceRepository;
//填写设置
水合设置();
}
公共int ActiveDegreeId{
得到{
返回int.Parse(GetValue(“Active_Degree_Id”);
}
}
公共字符串站点标题{
获取{return GetValue(“Site_Title”);}
}
公众谘询费{
获取{return decimal.Parse(GetValue(“咨询费”);}
}
公共十进位学费{
获取{return decimal.Parse(GetValue(“学费”);}
}
公开十进制登记费{
获取{return decimal.Parse(GetValue(“注册费”);}
}
P