0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Prism コードサンプル学習:15-FilteringEvents

Posted at

Prism コードサンプル学習:15-FilteringEvents

はじめに

以下の記事の続きです。
https://qiita.com/mngreen/items/8d5b906833a53af9669f

15-FilteringEvents

本サンプルは、互いに依存していないモジュール間でメッセージを送受し、送られたメッセージをフィルタリングして表示することを確認するサンプルとなっています。

原則、前回の記事とほとんど変わりませんが、ModuleBプロジェクトのMessageListViewModelクラスでの処理が一部変更されています。その変更部分の中に、フィルタリングする処理が追加されています。

using Prism.Events;
using Prism.Mvvm;
using System.Collections.ObjectModel;
using UsingEventAggregator.Core;

namespace ModuleB.ViewModels
{
    public class MessageListViewModel : BindableBase
    {
        IEventAggregator _ea;

        private ObservableCollection<string> _messages;
        public ObservableCollection<string> Messages
        {
            get { return _messages; }
            set { SetProperty(ref _messages, value); }
        }

        public MessageListViewModel(IEventAggregator ea)
        {
            _ea = ea;
            Messages = new ObservableCollection<string>();

            // 以下に引数が追加され、"Brian"宛のメッセージでないと処理が実行されなくなっている
            _ea.GetEvent<MessageSentEvent>().Subscribe(MessageReceived, ThreadOption.PublisherThread, false, (filter) => filter.Contains("Brian"));
        }

        private void MessageReceived(string message)
        {
            Messages.Add(message);
        }
    }
}

Subscribeの実処理では、前回の記事と同様にactionReferenceを生成しますが、その時に追加でfilterReferenceを生成します(実体はどちらもDelegateReference)。
その後、それぞれのオプションによって生成されるインスタンスが異なりますが、今回指定されているオプションがThreadOption.PublisherThreadのため、生成されるクラスであるEventSubscriptionクラスに注目します。
前回同様必要に応じてアクションを実行するか否かを決める処理(GetExecutionStrategy)が実行されます。
この中でフィルタ実行時・アクション実行時に必要となる引数を取得し、フィルタをかけてアクションを実行するか決めていました。

おわりに

今回は前回記事の引き続きで、イベントをフィルタするサンプルを読んでみました。
前回と異なる点はフィルタのみで大きくサンプルも異なっていませんでしたが、ジェネリックのサポートがひとつだけであったのが気になりました。何となく、FuncやActionのようにジェネリックとして渡せるものが多ければ多いほど(特にフレームワークとして提供するのであれば)便利なように思っていたのですが、実際にはひとつあれば充分との判断でしょうか。確かに、複数の引数をやり取りしたいのであればクラス化したほうが良い気もします。そのあたりのバランス感覚を磨いていくことが良いアーキテクチャをつくれるか否かにつながっていくのかな、と思いました。
次回、16-RegionContextについて見ていこうと思います。

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?