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

发布于 2024-07-26 22:19:15 字数 246 浏览 6 评论 0原文

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

I have an ASP.NET MVC app which depends on a lot of settings (name-value pairs), I am planning to store this information in a database table called SiteSettings. Is there an easy way in which I can get these settings using NHibernate. And what are the best practices when saving settings for a web application. And by settings I mean the settings which control the flow of processes in the web application and which are governed by business rules. These are not the typical connection string kind of settings. I was unable to get much information on the web on this topic. Maybe I am not searching on the right keywords, Any help will be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初熏 2024-08-02 22:19:15

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

我在数据库中有一个表(Biz_Config)来存储业务首选项。 (我为我所谓的 IT 首选项创建了一个 web.config 部分。)

我有一个类负责管理业务首选项。 构造函数抓取整个表(每个设置一行)并将它们复制到字典中,并且它具有访问(例如 bizconfig.get("key"))和更新此字典的方法,同时也更新表。 它还具有一些针对特定字典值的快捷方式属性,特别是在必须转换该值的情况下(我有一些重要的数字)。 它运作得很好。

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

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

我希望这有帮助。 如果您想要代码,我可以为您挖掘。

詹姆士

I can't answer in the context of nhibernate (which I'm not using) or best practices (I came up with this on my own recently). However, it works well for me, and will probably work for you.

I have a table (Biz_Config) in the database to store business preferences. (I've created a web.config section for what I call IT preferences.)

I have a class that is in charge of managing the biz preferences. The constructor grabs the entire table (one row per setting) and copies these into a dictionary, and it has methods to access (such as bizconfig.get("key")) and update this dictionary, also updating the table at the same time. It also has a few shortcut properties for specific dictionary values, especially where the value has to be cast (I have a few important numbers). It works quite well.

In order to be more efficient and not instantiate it every time I need a setting, and also to access it easily from my controllers and views, I created a static class, Globals, that is in charge of getting things out of the session or application variables. For the biz config object, it checks the application variable and, if null, creates a new one. Otherwise it just returns it. Globals is part of my helpers namespace, which is included in my web.config to be available to my views. So I can easily call:

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

I hope this helps. If you'd like code, I can dig that up for you.

James

清风疏影 2024-08-02 22:19:15

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

。 请参阅NHibernate 官方文档Ayende 关于“NHibernate 映射 -

”的帖子

If you have a set of key/value pairs, you probably want to use a <map>. See the official NHibernate documentation or Ayende's post about 'NHibernate Mapping - <map/>'.

忆伤 2024-08-02 22:19:15

我想出了一个与詹姆斯建议的非常相似的解决方案。 我有一个 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);
        }

    }
}

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

I have come up with a solution which is quite similar to the one suggested by James. I have an SiteSettingsService class which manages the settings for the whole site, it has a simple dependency on an interface called ISiteServiceRepository. This might not be the most elegant solution, But it is working perfectly for me. I have also configured the SiteSettingsService class as a Singleton using StructureMap. So, it saves me unnecessary instantiantion every time I need any settings.

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

    }
}

Hope this helps future developers facing similar problems. Any suggestions for improving this are more than welcome.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文