LoginSignup
1
0

More than 3 years have passed since last update.

プロパティの型をそのままにReactiveProertyとして参照できるようにする

Posted at

 こんな感じのプロパティがあるとします。

    public int SomeIntProperty
    {
        get;
        private set;
    }

 このプロパティを後からReactivePropertyとして参照したくなった時。
 プロパティの参照先が少なければ別にそのまま型をReactivePropertyに変えてしまってもまあいいですが、参照先が多かった場合は変更箇所も多くなり若干面倒です。
 また従来の参照箇所で、別にただのプロパティでよいはずの処理もReactivePropertyになるので、認知負荷が大きくなるかもしれません。

 以下のようにリファクタリングできます。

    // ReactivePropertyとして参照したい箇所ではこれを参照
    public IReadOnlyReactiveProperty<int> SomeIntReactiveProperty => someIntPropertyEntity;
    private readonly ReactiveProperty<int> someIntPropertyEntity = new ReactiveProperty<int>();

    public int SomeIntProperty
    {
        get => someIntPropertyEntity.Value;
        private set => someIntPropertyEntity.Value = value;
    }

 SomeIntPropertyのガワはそのままですが、中身の実態はsomeIntPropertyEntityに置き換えられています。
 そしてSomeIntReactivePropertyの実態もsomeIntPropertyEntityで共通です。
 ソースコードの通りですが、つまりSomeIntPropertyへの変更はsomeIntPropertyEntityの値の変更になり、ReactivePropertyであるsomeIntPropertyEntityの値が変更された結果SomeIntReactivePropertyとして発火することになります。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0