NHibernate を使用してデータベース テーブルに保存されている asp.net mvc アプリの Web サイト設定にアクセスする

StackOverflow https://stackoverflow.com/questions/1113837

質問

多くの設定 (名前と値のペア) に依存する ASP.NET MVC アプリがあります。この情報を SiteSettings というデータベース テーブルに保存する予定です。NHibernate を使用してこれらの設定を取得できる簡単な方法はありますか。Web アプリケーションの設定を保存するときのベスト プラクティスは何ですか。ここでの設定とは、Web アプリケーションのプロセス フローを制御し、ビジネス ルールによって管理される設定を意味します。これらは、一般的な接続文字列の種類の設定ではありません。このトピックに関しては、Web 上で多くの情報を得ることができませんでした。おそらく正しいキーワードで検索していない可能性があります。助けていただければ幸いです。

役に立ちましたか?

解決 3

私はジェームズによって提案されたものに非常に似ている解決策が出ています。私はそれがISiteServiceRepository ののと呼ばれるインターフェース上の単純な依存関係を持って、サイト全体の設定を管理しての SiteSettingsService のクラスを持っています。これは、最もエレガントな解決策ではないかもしれません、しかし、それは私のために完璧に働いています。私はまたのStructureMapを使用してのシングルトンのようSiteSettingsServiceクラスを設定しています。だから、それは私がすべての設定を必要とするたびに、不要なinstantiantionが保存されます。

//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のセクションを作成しました。)

私は、ビズの環境設定を管理する担当しているクラスがあります。コンストラクタは、辞書にテーブル全体(設定ごとに1行)コピーこれらグラブ、それが(例えばbizconfig.get(「キー」)のような)にアクセスし、この辞書を更新するためのメソッドがあり、同時にテーブルを更新します。また、値が(私はいくつかの重要な数字を持っている)にキャストする必要があり、特に場合は、特定の辞書の値のためのいくつかのショートカットのプロパティを持っています。これはかなりうまく動作します。

はそれを私は設定を必要とするたびにインスタンス化し、また私のコントローラとビューから簡単にアクセスするために、より効率的ではないために、私は静的クラス、グローバルを作成し、それは外のものを得ることを担当していますセッションまたはアプリケーション変数。ビズconfigオブジェクトの場合は、それがnullの場合、新しいものを作成し、アプリケーション変数をチェックして。それ以外の場合はそれを返します。グローバル私の見解に利用できるように私のweb.configファイルに含まれている私のヘルパーの名前空間の一部です。だから私は、簡単に呼び出すことができます:

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

私はこのことができます願っています。あなたは、コードを希望した場合、私はあなたのためにそれを掘ることができます。

ジェームズ

一連のキーと値のペアがある場合は、おそらく、 <map>. 。を参照してください。 Hibernate の公式ドキュメント または 「NHibernate マッピング - <map/>」に関する Ayende の投稿.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top