shell constructor initialization in prism

Topics: Prism v4 - WPF 4
Oct 1, 2012 at 7:29 PM

hi i need a hint please,

in the Sample ModularityWithinUnity.Desktop the Shell Class is defined with the constructor

public Shell(IModuleManager moduleManager, IModuleTracker moduleTracker, CallbackLogger logger)

{

            if (moduleManager == null)
            {
                throw new ArgumentNullException("moduleManager");
            }

..

}

and the Initialization is done i think in the QuickStartBootstrapper class

with 

ServiceLocator.Current.GetInstance<Shell>();

without the Arguments of the Constructor. How does the ServiceLocator class know them or  which order , i try to debug but it stops on this GetInstance Level.

In prism documentation there are only 2 constructors documented

http://msdn.microsoft.com/en-us/library/hh860230%28v=pandp.51%29.aspx

1 default , 1 with a string but not with this structure above.

thnx br thomas

 

 

 

 

 

Developer
Oct 2, 2012 at 6:32 PM

Hi Thomas,

Basically, the Shell is being constructed through dependency injection. Based on my understanding, this is how it works:

  • The QuickStart uses Unity as a dependency injection container. If fact, you will find that Unity is used in several QuickStarts and that it's supported as out of the box by the Prism library (the library also supports MEF.)
  • The UnityBootstrapper, from which the QuickStartBootstrapper class inherits in the ModularityWithUnity QuickStart, registers a UnityServiceLocatorAdapter as the "locator provider" of the ServiceLocator class. Based on my understanding, by setting the aforementioned adapter, each time an instance of a type is requested to the ServiceLocator, the Unity container will be used to resolve it.
  • The corresponding types for the IModuleManager and IModuleTracker interfaces as well as an instance of CallbackLogger are registered in the container before invoking the CreateShell method. You can see this in the ConfigureContainer method of the QuickStartBootstrapper class (the type for IModuleManager is registered by its super class UnityBootstrapper.)
  • When an instance of type Shell is requested using the ServiceLocator, the "request" is resolved through Unity. As far as I know, Unity obtains the type (in this case Shell) through reflection, checks the arguments required in the Shell's constructor and, if Unity knows the arguments' types, it creates the Shell injecting the corresponding instances in the constructor automatically.

Dependency injection is an approach widely used in Prism applications. You can find more information it in the following chapter of the Prism documentation:

In this case, Unity creates an instance of the type Shell by using a capability named "constructor injection." However, this is only one of the capabilities provided by this container. If you are interested in knowing more about the Unity container, I believe you could find more information about this in its CodePlex site:

I hope you find this useful,

Damian Cherubini
http://blogs.southworks.net/dcherubini

Oct 2, 2012 at 9:05 PM

hallo Damian ,

thanks a lot :) now it´s a logical chain for me, i did not notice the registration was done in front of the CreateShell so Unity knows the types ,and the instance and could create or resolve the three attributes.

regards thomas