4

FactoryPattern内でIoCコンテナを使用するのは悪い習慣ですか?例えば:

public interface IDialogService
{
    void RegisterView<TView, TViewModel>(string viewName) 
        where TViewModel : IDialogViewModel
        where TView : Window;

    bool? ShowDialog(string viewName, IDialogViewModel viewModel);
    // factory method:
    TViewModel CreateDialogViewModel<TViewModel>(string name) where TViewModel : IDialogViewModel;
}

public class DialogService : IDialogService
{
    private readonly IUnityContainer _container;
    public DialogService(IUnityContainer container)
    {
        _container = container;
    }

    #region IDialogService Members

    public void RegisterView<TView, TViewModel>()
        where TView : Window
        where TViewModel : IDialogViewModel
    {
        RegisterView<TView, TViewModel>("");
    }

    public void RegisterView<TView, TViewModel>(string viewName)
        where TView : Window
        where TViewModel : IDialogViewModel
    {
        if (!_container.IsRegistered<TViewModel>())
            _container.RegisterType<TViewModel>();

        if (string.IsNullOrEmpty(viewName))
        {
            viewName = typeof(TView).Name;
        }
        _container.RegisterType<Window, TView>(viewName);
    }

    public bool? ShowDialog(string viewName, IDialogViewModel viewModel)
    {
        var view = _container.Resolve<Window>(viewName);
        view.DataContext = viewModel;
        view.Owner = Application.Current.MainWindow;
        return view.ShowDialog();
    }
    // factory method:
    public TViewModel CreateDialogViewModel<TViewModel>(string name) where TViewModel : IDialogViewModel
    {
        return _container.Resolve<TViewModel>(name);
    }

    #endregion
}

ファクトリメソッドを作成した理由は、IDialogViewModel実装の一部のコンストラクターに多くのパラメーターがあり、インスタンス化するときに各ダイアログに新しいUnitOfWorkが必要なためです。

これが私がそれを使う方法です:

public class PeopleMainViewModel : NotificationObject, ...
{
    private readonly IDialogService _dialogService = null;
    public PeopleMainViewModel(IDialogService dialogService)
    {
        _dialogService = dialogService;
    }

    // this method executes by a command.
    public void AddPerson()
    {
        // here is factory method usage. each time PersonDialogViewModel instantiates, a new Unit of work will be created.
        var viewModel = _dialogService.CreateDialogViewModel<PersonDialogViewModel>();

        // this shows person dialog window to user...
        var result = _dialogService.ShowDialog("PersonWindow", viewModel);
        if(result == true)
        {
            //...
        }
    }
}
4

1 に答える 1

2

IoCコンテナをファクトリに挿入することは避け、代わりにIoCフレームワークがファクトリを生成できるかどうかを確認する必要があります。しかし、それが常に可能であるとは限りません。そのような場合、容器が逃げない限り、工場で使用することは許容できると思います。

個人的には、クラスに2つの責任があり、インジェクションフレームワークがアプリケーションロジックに逃げ込んだように感じるので、コンテナをサービスに入れません(上記の場合のように)。

于 2012-11-19T12:38:12.880 に答える