1

Now I'm practicing Prism with "Prism-Samples-Wpf" in Github. (https://github.com/PrismLibrary/Prism-Samples-Wpf/tree/master/06-ViewActivationDeactivation)

This code is part of MainWindow.xaml.cs

public partial class MainWindow : Window
{
    IContainerExtension _container;
    IRegionManager _regionManager;
    IRegion _region;

    ViewA _viewA;
    ViewB _viewB;

    public MainWindow(IContainerExtension container, IRegionManager regionManager)
    {
            InitializeComponent();
            _container = container;
            _regionManager = regionManager;

            this.Loaded += MainWindow_Loaded;
    }
    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
            _viewA = new ViewA();
            _viewB = _container.Resolve<ViewB>();

            _region = _regionManager.Regions["ContentRegion"];
            _region.Add(_viewB);

            _region.Add(_viewA);
    }
}

there return same type like this.

(1) new ViewA();                 // ActivationDeactivation.Views.ViewA
(2) _container.Resolve<ViewB>(); // ActivationDeactivation.Views.ViewB

What is difference between Container.Resolve() and new ViewA()?

Max Lee
  • 241
  • 1
  • 5
  • 14
  • 1
    I think the question is really *what is an IoC container and what does it do?*. Do a quick read up on DI and IoC. Depending on the setup, there could be no real difference or there could be a big difference. – Crowcoder May 01 '19 at 10:49
  • 1
    Calling `_container.Resolve()` may or may not create a new instance of `ViewB` - it depends how the container is configured - and it allows the container to do other work on the instance returned - such as injecting other dependencies. Calling `new ViewB()` definitely creates a new instance but not other work is performed on it. I suggest that you follow @Crowcoder's advice. – Enigmativity May 01 '19 at 12:03

1 Answers1

0

What is difference between Container.Resolve<ViewA>() and new ViewA()?

Nothing really (besides new always returning a new instance, while ViewA might be registered as singleton).

But imagine new SomeService( aDependency, new AnotherDependency(), () => new SomeProduct( new ThirdDependency(), aDependency ) as opposed to Container.Resolve<SomeService>()...

Unity, or any DI-container, that is, just makes life easier when creating instances. Sure, you can do everything by hand, you don't have to. Be careful, though - you should not inject the container. Rather inject a factory, if you have to create instances on the fly.

Example of what MainWindow could look like:

public partial class MainWindow : Window
{
    private readonly Func<ViewA> _viewAFactory;
    private readonly Func<ViewB> _viewABFactory;
    IRegionManager _regionManager;
    IRegion _region;

    ViewA _viewA;
    ViewB _viewB;

    public MainWindow(Func<ViewA> viewAFactory, Func<ViewB> viewBFactory, IRegionManager regionManager)
    {
        InitializeComponent();
        _viewAFactory = viewAFactory;
        _viewBFactory = viewBFactory;
        _regionManager = regionManager;

        this.Loaded += MainWindow_Loaded;
    }

    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
            _viewA = _viewAFactory();
            _viewB = _viewBFactory();

            _region = _regionManager.Regions["ContentRegion"];
            _region.Add(_viewB);

            _region.Add(_viewA);
    }
}

All of this should really happen in MainWindowViewModel, though. Have a look ViewModelLocator for a very simple way to make your views magically create their view models (properly resolved with all the dependencies).

Haukinger
  • 10,420
  • 2
  • 15
  • 28