1

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

4

3 に答える 3

1

nhibernate(私は使用していません)またはベストプラクティス(最近自分でこれを思いついた)のコンテキストでは答えることができません。しかし、それは私にとってはうまく機能し、おそらくあなたにとってはうまくいくでしょう。

データベースにビジネス設定を保存するためのテーブル(Biz_Config)があります。(私がITプリファレンスと呼ぶもののためにweb.configセクションを作成しました。)

私はビジネスの好みを管理することを担当するクラスを持っています。コンストラクターは、テーブル全体(設定ごとに1行)を取得してディクショナリにコピーします。コンストラクターには、このディクショナリにアクセスして更新するメソッド(bizconfig.get( "key")など)があり、同時にテーブルも更新されます。 。また、特定の辞書値、特に値をキャストする必要がある場合のショートカットプロパティもいくつかあります(重要な数値がいくつかあります)。それは非常にうまく機能します。

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

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

これがお役に立てば幸いです。コードが必要な場合は、それを掘り下げることができます。

ジェームズ

于 2009-07-12T11:56:25.667 に答える
0

Jamesが提案したものと非常によく似た解決策を思いつきました。サイト全体の設定を管理するSiteSettingsServiceクラスがあります。これは、 ISiteServiceRepositoryというインターフェイスに単純に依存しています。これは最もエレガントなソリューションではないかもしれませんが、私にとっては完璧に機能しています。また、SiteSettingsService クラスを StructureMap を使用してシングルトンとして構成しました。そのため、設定が必要になるたびに不要なインスタンス化を省くことができます。

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

    }
}

これが、同様の問題に直面する将来の開発者に役立つことを願っています。これを改善するための提案は大歓迎です。

于 2009-07-18T09:06:25.720 に答える
0

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

于 2009-07-16T04:21:34.397 に答える