0

このデスクトップ アプリケーションのビジネス要件は、一部のビューに共通のフィルター オプションが存在することです。つまり、共通のフィルター オプションを含むビューの場合、1 つのビューでいくつかのオプションをチェックすると、それを含む他のすべてのビューでも同じオプションをチェックする必要があります (多くのビューが既に表示されているため、それらを更新する必要があります)。

CommonSettings { CommonSetting1, CommonSetting2, CommonSetting3, CommonSetting4, CommonSetting5  }
ScreenASettings {ScreenASetting1, ScreenASetting2, CommonSetting2, CommonSetting4 }
ScreenBSettings {ScreenASetting1, CommonSetting1, CommonSetting2, CommonSetting3 }
// and so on

この設定は、後で読み取るためにファイルに保存する必要があります。アイデアは、画面が開いたときにこの設定が適用されるということです。ScreenA と ScreenB の両方が開いていて、ScreenA の CommonSetting2 を変更すると、ScreenB にもこの設定が含まれているため、この設定に新しい値が適用されるはずです。

私の現在の設計は次のとおりです。 - CommonSettings のインスタンスは 1 つしか存在しません - CommonSettings インスタンスへの参照を含む CommonSettingsViewModel があります。CommonSettingsViewModel はすべてを処理します: 画面にオプションを表示する (共通設定を CommonSettingsView に公開し、共通設定を保存し、共通設定をロードするなど) - カスタムおよび共通設定を含む各 CustomViewModel は、(DI を介して) CustomSetting および CommonSettingViewModel インスタンスへの参照を取得します。このようにして、共通設定の作成の制御を CommonSettingViewModel に渡し、そのカスタム設定 (ロード、保存) のみを処理できます。

このアプローチでは、カスタム設定と共通設定の責任を分割しています。ここで見られる欠点の 1 つは、データをクエリする必要がある場合、常に CustomSettings と CommonSettings の両方のインスタンスを渡す必要があることです。

このアプローチは正しいと思いますか、それとももっと良いアプローチがあると思いますか?

編集:これが私が設定に使用している現在の実装です

public interface ISettings {
    string ElementPath { get; set; }
    Exception Error { get; }
    object GetValue(string setting);
    bool HasError { get; }
    void Read();
    void Save();
    void SetValue(string setting, object value);
}

// base class for ScreenASettings, CommonSettings, etc
public abstract class SettingsBase : ISettings { ... }

// ancestor class only needs to add settings
public class CommonSettings : BaseSettings {
    private bool _ommonSetting1;
    [Setting]
    public bool CommonSetting1 { .... }
    // CommonSetting2, CommonSetting3, etc
}
4

1 に答える 1

0

次のように共通のインターフェースを定義してみませんか。

public interface ISettings
{
    T GetSetting<T>(string key);
    bool TryGetSetting<T>(string key, out T result);
}

ISettings次に、任意の関数へのパラメーターとして受け入れ、複合パターンCompositeSettingsを使用して、他の複数のオブジェクトを表すことができるオブジェクトを定義できISettingsます。

public class CompositeSettings : ISettings
{
    private IEnumerable<ISettings> settings;
    public CompositeSettings(params ISettings[] settings)
    {
        this.settings = settings;
    }

    public T GetSetting<T>(string key)
    {
        T result;
        if (TryGetSetting<T>(key, out result))
            return result;
        throw KeyNotFoundException(key);
    }

    public bool TryGetSetting<T>(string key, out T result)
    {
        foreach (var setting in this.settings)
        {
            T innerResult;
            if (setting.TryGetSetting<T>(out innerResult))
            {
                result = innerResult;
                return true;
            }
        }
        return false;
    }
}

(これを読み返してみると、おそらく私の命名は完全には役に立ちませんでした...おそらくISettingsProviderより明確です)

于 2013-04-11T12:55:58.127 に答える