Who is calling the module's constructors

Topics: Prism v4 - WPF 4
Feb 26, 2015 at 7:08 PM
Edited Feb 26, 2015 at 7:09 PM
public HelloWorldModule(IRegionViewRegistry registry)
        {
            this.regionViewRegistry = registry;   
        }
public OrderModule( IUnityContainer container, IRegionManager regionManager )
        {
            this.container     = container;
            this.regionManager = regionManager;
        }
These are two separate constructors of two different modules. Their signatures are different. These are taken from PRISM quickstart download. I searched the code but can't figure out who is calling these constructors as if Framework is calling them, then why two different signatures ?
Feb 27, 2015 at 11:52 PM
Edited Feb 27, 2015 at 11:54 PM
This is inversion of control using Unity at work. The framework automatically resolves those dependencies for you, and any other dependencies you ask for in a constructor as long as the container knows about them. In Prism, you should never be manually newing up objects (var myObject = new CustomObject()), the container will be creating them for you.
Feb 28, 2015 at 2:55 PM
I'll supplement what BrianL said with a little more information, because I find for people new to Prism with no Dependency Injection experience, this is one of the biggest learning barriers. You first need to get familiar with the patterns of Inversion of Control (IoC) and Dependency Injection (DI). A container does both of these patterns, and lets you be more declarative to create a type that depends on some other types (typically through an interface) by just requiring those through constructor parameters (or other annotations on properties or methods, depending on the container you use). The container will be responsible for constructing the object (the IoC part) and as part of that process, dependency injecting it dependent objects (the DI part).

Modules in Prism are created in the boostrapping process and Prism using the container to do the construction so that those modules can declare dependencies through their constructors and have them injected for them.

I wouldn't go so far as to say that you should never be manually newing up objects, some things have no need to be constructed through the container and there is a minor performance cost to doing so. But for things that compose the layers of your application like modules, view models, and services, those should be constructed through the container so that their dependencies can be resolved by the container, allowing looser coupling between the components.
Marked as answer by AnjumSKhan on 2/28/2015 at 8:36 AM
Feb 28, 2015 at 4:36 PM
brianlagunas wrote:
This is inversion of control using Unity at work. The framework automatically resolves those dependencies for you, and any other dependencies you ask for in a constructor as long as the container knows about them. In Prism, you should never be manually newing up objects (var myObject = new CustomObject()), the container will be creating them for you.
I like both the answers, but there is no upvote feature.