0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

WPFのDI(依存性注入)化

Posted at

はじめに

自分用メモですが、同じ状況の方の参考になれば幸いです。
普段は WPF と ASP.NET の API 開発を長年やっています。

前提

WPFではCommunityToolkit.Mvvmのパッケージを使用しています。

ChatGPTに聞いたこと

WPFでViewModelを単体テストしたい

結論

ViewModelをDIに登録することにしました。
コンストラクタで Ioc コンテナから取得し、DataContextに登録します。初期化処理が必要な場合は、このタイミングで行います。
なお、この方法では XAML のインテリセンスは利用できなくなります。工夫すれば回避できるようですが、複雑化して理解が追いつかなくなるのは避けたいので、割り切って切り捨てました。最もシンプルでわかりやすい形を選びました。

/// ViewModelはTransientで登録
services.AddTransient<MainViewModel>();

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        var viewModel = Ioc.Default.GetRequiredService<MainViewModel>();
        _ = viewModel.InitializeAsync();
        
        DataContext = viewModel;

        InitializeComponent();
    }
}

MessageBoxIMessageBox インターフェース経由で利用し、外部依存を排除します。
どうしても ViewModel から View を操作する必要がある場合には、CommunityToolkit.Mvvm の弱参照(WeakReferenceMessenger) を使用します。

さらに、RelayCommand は単体テストを行いやすくするために internal に変更しました。

        [RelayCommand]

        internal async Task PushAsync()
        {
            _messageBox.Show("Push!!");
        }
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?