Search code examples
c#ioc-containerfactory-pattern

Using IoC inside abstract factory pattern?


Is it a bad practice to use IoC container inside Factory Pattern? for example:

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
}

The reason that I created a Factory Method is bacause some of my IDialogViewModel implementations have a lots of parameters in their constructor and also, each dialog must have new UnitOfWork when instantiating.

Here is how I use it:

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)
        {
            //...
        }
    }
}

Solution

  • You you should try to avoid injecting the IoC container into a factory, instead look at if your IoC framework can generate the factory for you. However that is not always possible, in those cases I think it is acceptable to use the container in a factory, as long as it never escapes.

    Personally I would not put the container in a Service (as is the case above) as it feels to me like the class now has two responsibilities and the injection framework has escaped out into application logic.